Skip to content

Commit c6e8a6e

Browse files
committed
Changed OnEnable, OnDisable and OnDestroy behaviour
1 parent e0af985 commit c6e8a6e

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ private void OnEnable()
110110
_networkedObject = GetComponentInParent<NetworkedObject>();
111111

112112
NetworkedObject.NetworkedBehaviours.Add(this);
113+
OnEnabled();
113114
}
114115

115116
internal bool networkedStartInvoked = false;
@@ -129,7 +130,44 @@ public virtual void NetworkStart(BitReader payloadReader)
129130
internal void InternalNetworkStart()
130131
{
131132
CacheAttributedMethods();
133+
WarnUnityReflectionMethodUse();
132134
}
135+
136+
private void WarnUnityReflectionMethodUse()
137+
{
138+
MethodInfo[] methods = GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
139+
for (int i = 0; i < methods.Length; i++)
140+
{
141+
if (methods[i].Name == "OnDestroy")
142+
{
143+
throw new Exception("The method \"OnDestroy\" is not allowed to be defined in classes that inherit NetworkedBehaviour. Please override the \"OnDestroyed\" method instead");
144+
}
145+
else if (methods[i].Name == "OnDisable")
146+
{
147+
throw new Exception("The method \"OnDisable\" is not allowed to be defined in classes that inherit NetworkedBehaviour. Please override the \"OnDisabled\" method instead");
148+
}
149+
else if (methods[i].Name == "OnEnable")
150+
{
151+
throw new Exception("The method \"OnEnable\" is not allowed to be defined in classes that inherit NetworkedBehaviour. Please override the \"OnEnable\" method instead");
152+
}
153+
}
154+
}
155+
156+
public virtual void OnDisabled()
157+
{
158+
159+
}
160+
161+
public virtual void OnDestroyed()
162+
{
163+
164+
}
165+
166+
public virtual void OnEnabled()
167+
{
168+
169+
}
170+
133171
/// <summary>
134172
/// Gets called when SyncedVars gets updated
135173
/// </summary>
@@ -446,6 +484,7 @@ protected NetworkedBehaviour GetBehaviour(ushort id)
446484
private void OnDisable()
447485
{
448486
NetworkedObject.NetworkedBehaviours.Remove(this);
487+
OnDisabled();
449488
}
450489

451490
private void OnDestroy()
@@ -454,6 +493,7 @@ private void OnDestroy()
454493
{
455494
DeregisterMessageHandler(pair.Key, pair.Value);
456495
}
496+
OnDestroyed();
457497
}
458498

459499
#region SYNC_VAR

0 commit comments

Comments
 (0)