NetworkClient - Use TryGetComponent (#3331)

This eliminates allocations, at least in the editor, per Unity docs.
This commit is contained in:
MrGadget 2022-12-31 07:59:57 -05:00 committed by GitHub
parent 82a7e752a8
commit 993caf3b36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -571,8 +571,7 @@ public static void RegisterPrefab(GameObject prefab, uint newAssetId)
return;
}
NetworkIdentity identity = prefab.GetComponent<NetworkIdentity>();
if (identity == null)
if (!prefab.TryGetComponent<NetworkIdentity>(out NetworkIdentity identity))
{
Debug.LogError($"Could not register '{prefab.name}' since it contains no NetworkIdentity component");
return;
@ -598,8 +597,7 @@ public static void RegisterPrefab(GameObject prefab)
return;
}
NetworkIdentity identity = prefab.GetComponent<NetworkIdentity>();
if (identity == null)
if (!prefab.TryGetComponent<NetworkIdentity>(out NetworkIdentity identity))
{
Debug.LogError($"Could not register '{prefab.name}' since it contains no NetworkIdentity component");
return;
@ -635,8 +633,7 @@ public static void RegisterPrefab(GameObject prefab, SpawnDelegate spawnHandler,
return;
}
NetworkIdentity identity = prefab.GetComponent<NetworkIdentity>();
if (identity == null)
if (!prefab.TryGetComponent<NetworkIdentity>(out NetworkIdentity identity))
{
Debug.LogError($"Could not register handler for '{prefab.name}' since it contains no NetworkIdentity component");
return;
@ -683,8 +680,7 @@ public static void RegisterPrefab(GameObject prefab, uint newAssetId, SpawnHandl
return;
}
NetworkIdentity identity = prefab.GetComponent<NetworkIdentity>();
if (identity == null)
if (!prefab.TryGetComponent<NetworkIdentity>(out NetworkIdentity identity))
{
Debug.LogError($"Could not register handler for '{prefab.name}' since it contains no NetworkIdentity component");
return;
@ -750,8 +746,7 @@ public static void RegisterPrefab(GameObject prefab, SpawnHandlerDelegate spawnH
return;
}
NetworkIdentity identity = prefab.GetComponent<NetworkIdentity>();
if (identity == null)
if (!prefab.TryGetComponent<NetworkIdentity>(out NetworkIdentity identity))
{
Debug.LogError($"Could not register handler for '{prefab.name}' since it contains no NetworkIdentity component");
return;
@ -815,8 +810,7 @@ public static void UnregisterPrefab(GameObject prefab)
return;
}
NetworkIdentity identity = prefab.GetComponent<NetworkIdentity>();
if (identity == null)
if (!prefab.TryGetComponent<NetworkIdentity>(out NetworkIdentity identity))
{
Debug.LogError($"Could not unregister '{prefab.name}' since it contains no NetworkIdentity component");
return;
@ -1101,12 +1095,13 @@ static NetworkIdentity SpawnPrefab(SpawnMessage message)
Debug.LogError($"Spawn Handler returned null, Handler assetId '{message.assetId}'");
return null;
}
NetworkIdentity identity = obj.GetComponent<NetworkIdentity>();
if (identity == null)
if (!obj.TryGetComponent<NetworkIdentity>(out NetworkIdentity identity))
{
Debug.LogError($"Object Spawned by handler did not have a NetworkIdentity, Handler assetId '{message.assetId}'");
return null;
}
return identity;
}