Use TryGetComponent (#3332)

This eliminates allocations, at least in the editor, per Unity docs.
This commit is contained in:
MrGadget 2022-12-31 07:59:42 -05:00 committed by GitHub
parent fe4aa931e1
commit 82a7e752a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 20 deletions

View File

@ -565,8 +565,7 @@ public static bool SyncVarGameObjectEqual(GameObject newGameObject, uint netIdFi
uint newNetId = 0;
if (newGameObject != null)
{
NetworkIdentity identity = newGameObject.GetComponent<NetworkIdentity>();
if (identity != null)
if (newGameObject.TryGetComponent<NetworkIdentity>(out NetworkIdentity identity))
{
newNetId = identity.netId;
if (newNetId == 0)
@ -589,8 +588,7 @@ protected void SetSyncVarGameObject(GameObject newGameObject, ref GameObject gam
uint newNetId = 0;
if (newGameObject != null)
{
NetworkIdentity identity = newGameObject.GetComponent<NetworkIdentity>();
if (identity != null)
if (newGameObject.TryGetComponent<NetworkIdentity>(out NetworkIdentity identity))
{
newNetId = identity.netId;
if (newNetId == 0)

View File

@ -150,7 +150,7 @@ public virtual void OnValidate()
// always >= 0
maxConnections = Mathf.Max(maxConnections, 0);
if (playerPrefab != null && playerPrefab.GetComponent<NetworkIdentity>() == null)
if (playerPrefab != null && !playerPrefab.TryGetComponent<NetworkIdentity>(out _))
{
Debug.LogError("NetworkManager - Player Prefab must have a NetworkIdentity.");
playerPrefab = null;
@ -1130,7 +1130,7 @@ void OnServerAddPlayerInternal(NetworkConnectionToClient conn, AddPlayerMessage
return;
}
if (autoCreatePlayer && playerPrefab.GetComponent<NetworkIdentity>() == null)
if (autoCreatePlayer && !playerPrefab.TryGetComponent<NetworkIdentity>(out _))
{
Debug.LogError("The PlayerPrefab does not have a NetworkIdentity. Please add a NetworkIdentity to the player prefab.");
return;

View File

@ -698,8 +698,7 @@ public static void UnregisterHandler<T>()
internal static bool GetNetworkIdentity(GameObject go, out NetworkIdentity identity)
{
identity = go.GetComponent<NetworkIdentity>();
if (identity == null)
if (!go.TryGetComponent<NetworkIdentity>(out identity))
{
Debug.LogError($"GameObject {go.name} doesn't have NetworkIdentity.");
return false;
@ -756,8 +755,7 @@ public static void DisconnectAll()
// on this playerControllerId for this connection, this will fail.
public static bool AddPlayerForConnection(NetworkConnectionToClient conn, GameObject player)
{
NetworkIdentity identity = player.GetComponent<NetworkIdentity>();
if (identity == null)
if (!player.TryGetComponent<NetworkIdentity>(out NetworkIdentity identity))
{
Debug.LogWarning($"AddPlayer: playerGameObject has no NetworkIdentity. Please add a NetworkIdentity to {player}");
return false;
@ -814,8 +812,7 @@ public static bool AddPlayerForConnection(NetworkConnectionToClient conn, GameOb
// safely be used while changing scenes.
public static bool ReplacePlayerForConnection(NetworkConnectionToClient conn, GameObject player, bool keepAuthority = false)
{
NetworkIdentity identity = player.GetComponent<NetworkIdentity>();
if (identity == null)
if (!player.TryGetComponent<NetworkIdentity>(out NetworkIdentity identity))
{
Debug.LogError($"ReplacePlayer: playerGameObject has no NetworkIdentity. Please add a NetworkIdentity to {player}");
return false;
@ -1150,8 +1147,7 @@ static void SpawnObject(GameObject obj, NetworkConnection ownerConnection)
return;
}
NetworkIdentity identity = obj.GetComponent<NetworkIdentity>();
if (identity == null)
if (!obj.TryGetComponent<NetworkIdentity>(out NetworkIdentity identity))
{
Debug.LogError($"SpawnObject {obj} has no NetworkIdentity. Please add a NetworkIdentity to {obj}", obj);
return;
@ -1228,8 +1224,7 @@ public static void Spawn(GameObject obj, NetworkConnection ownerConnection = nul
// This is the same as calling NetworkIdentity.AssignClientAuthority on the spawned object.
public static void Spawn(GameObject obj, GameObject ownerPlayer)
{
NetworkIdentity identity = ownerPlayer.GetComponent<NetworkIdentity>();
if (identity == null)
if (!ownerPlayer.TryGetComponent<NetworkIdentity>(out NetworkIdentity identity))
{
Debug.LogError("Player object has no NetworkIdentity");
return;

View File

@ -239,8 +239,7 @@ public static void WriteTransform(this NetworkWriter writer, Transform value)
writer.WriteUInt(0);
return;
}
NetworkIdentity identity = value.GetComponent<NetworkIdentity>();
if (identity != null)
if (value.TryGetComponent<NetworkIdentity>(out NetworkIdentity identity))
{
writer.WriteUInt(identity.netId);
}
@ -260,8 +259,7 @@ public static void WriteGameObject(this NetworkWriter writer, GameObject value)
}
// warn if the GameObject doesn't have a NetworkIdentity,
NetworkIdentity identity = value.GetComponent<NetworkIdentity>();
if (identity == null)
if (!value.TryGetComponent<NetworkIdentity>(out NetworkIdentity identity))
Debug.LogWarning($"NetworkWriter {value} has no NetworkIdentity");
// serialize the correct amount of data in any case to make sure