NetworkIdentity: NetworkBehaviours[] caching simplified

This commit is contained in:
vis2k 2021-02-18 14:28:28 +08:00
parent b4fa4f2d1c
commit 6374c4b88b

View File

@ -243,26 +243,22 @@ internal set
// get all NetworkBehaviour components // get all NetworkBehaviour components
// => currently lazily initialized so tests can add components after // => currently lazily initialized so tests can add components after
// creating a NetworkIdentity. // creating a NetworkIdentity.
NetworkBehaviour[] _NetworkBehaviours;
public NetworkBehaviour[] NetworkBehaviours public NetworkBehaviour[] NetworkBehaviours
{ {
get get
{ {
if (networkBehavioursCache == null) if (_NetworkBehaviours == null)
{ {
CreateNetworkBehavioursCache(); _NetworkBehaviours = GetComponents<NetworkBehaviour>();
} if (_NetworkBehaviours.Length > byte.MaxValue)
return networkBehavioursCache;
}
}
void CreateNetworkBehavioursCache()
{
networkBehavioursCache = GetComponents<NetworkBehaviour>();
if (networkBehavioursCache.Length > byte.MaxValue)
{ {
Debug.LogError($"Only {byte.MaxValue} NetworkBehaviour components are allowed for NetworkIdentity: {name} because we send the index as byte.", this); Debug.LogError($"Only {byte.MaxValue} NetworkBehaviour components are allowed for NetworkIdentity: {name} because we send the index as byte.", this);
// Log error once then resize array so that NetworkIdentity does not throw exceptions later // Log error once then resize array so that NetworkIdentity does not throw exceptions later
Array.Resize(ref networkBehavioursCache, byte.MaxValue); Array.Resize(ref _NetworkBehaviours, byte.MaxValue);
}
}
return _NetworkBehaviours;
} }
} }
@ -1286,7 +1282,7 @@ internal void Reset()
netId = 0; netId = 0;
connectionToServer = null; connectionToServer = null;
connectionToClient = null; connectionToClient = null;
networkBehavioursCache = null; _NetworkBehaviours = null;
ClearObservers(); ClearObservers();