From b4c9c6fdc8a3e7b668625e98cb027a4d754aa116 Mon Sep 17 00:00:00 2001 From: Paul Pacheco Date: Wed, 17 Apr 2019 09:00:03 -0500 Subject: [PATCH] refactor: consolidate prefab and spawn handlers (#817) --- Assets/Mirror/Runtime/ClientScene.cs | 67 ++++++---------------------- 1 file changed, 13 insertions(+), 54 deletions(-) diff --git a/Assets/Mirror/Runtime/ClientScene.cs b/Assets/Mirror/Runtime/ClientScene.cs index 481bf190a..db920d5b4 100644 --- a/Assets/Mirror/Runtime/ClientScene.cs +++ b/Assets/Mirror/Runtime/ClientScene.cs @@ -18,6 +18,7 @@ public static class ClientScene public static bool ready { get; internal set; } public static NetworkConnection readyConnection { get; private set; } + [Obsolete] public static Dictionary prefabs = new Dictionary(); // scene id to NetworkIdentity public static Dictionary spawnableObjects; @@ -175,29 +176,12 @@ static NetworkIdentity SpawnSceneObject(ulong sceneId) return null; } - // spawn handlers and prefabs - static bool GetPrefab(Guid assetId, out GameObject prefab) - { - prefab = null; - return assetId != Guid.Empty && - prefabs.TryGetValue(assetId, out prefab) && prefab != null; - } - // this assigns the newAssetId to the prefab. This is for registering dynamically created game objects for already know assetIds. public static void RegisterPrefab(GameObject prefab, Guid newAssetId) { - NetworkIdentity identity = prefab.GetComponent(); - if (identity) - { - identity.assetId = newAssetId; - - if (LogFilter.Debug) Debug.Log("Registering prefab '" + prefab.name + "' as asset:" + identity.assetId); - prefabs[identity.assetId] = prefab; - } - else - { - Debug.LogError("Could not register '" + prefab.name + "' since it contains no NetworkIdentity component"); - } + RegisterSpawnHandler(newAssetId, + (position, assetId) => Object.Instantiate(prefab), + (spawned) => Object.Destroy(spawned)); } public static void RegisterPrefab(GameObject prefab) @@ -205,8 +189,7 @@ public static void RegisterPrefab(GameObject prefab) NetworkIdentity identity = prefab.GetComponent(); if (identity) { - if (LogFilter.Debug) Debug.Log("Registering prefab '" + prefab.name + "' as asset:" + identity.assetId); - prefabs[identity.assetId] = prefab; + RegisterPrefab(prefab, identity.assetId); NetworkIdentity[] identities = prefab.GetComponentsInChildren(); if (identities.Length > 1) @@ -236,16 +219,7 @@ public static void RegisterPrefab(GameObject prefab, SpawnDelegate spawnHandler, return; } - if (identity.assetId == Guid.Empty) - { - Debug.LogError("RegisterPrefab game object " + prefab.name + " has no prefab. Use RegisterSpawnHandler() instead?"); - return; - } - - if (LogFilter.Debug) Debug.Log("Registering custom prefab '" + prefab.name + "' as asset:" + identity.assetId + " " + spawnHandler.GetMethodName() + "/" + unspawnHandler.GetMethodName()); - - spawnHandlers[identity.assetId] = spawnHandler; - unspawnHandlers[identity.assetId] = unspawnHandler; + RegisterSpawnHandler(identity.assetId, spawnHandler, unspawnHandler); } public static void UnregisterPrefab(GameObject prefab) @@ -256,8 +230,7 @@ public static void UnregisterPrefab(GameObject prefab) Debug.LogError("Could not unregister '" + prefab.name + "' since it contains no NetworkIdentity component"); return; } - spawnHandlers.Remove(identity.assetId); - unspawnHandlers.Remove(identity.assetId); + UnregisterSpawnHandler(identity.assetId); } public static void RegisterSpawnHandler(Guid assetId, SpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler) @@ -268,6 +241,11 @@ public static void RegisterSpawnHandler(Guid assetId, SpawnDelegate spawnHandler return; } + if (assetId == Guid.Empty) + { + Debug.LogError($"Registering invalid asset id {assetId}"); + return; + } if (LogFilter.Debug) Debug.Log("RegisterSpawnHandler asset '" + assetId + "' " + spawnHandler.GetMethodName() + "/" + unspawnHandler.GetMethodName()); spawnHandlers[assetId] = spawnHandler; @@ -282,7 +260,6 @@ public static void UnregisterSpawnHandler(Guid assetId) public static void ClearSpawners() { - prefabs.Clear(); spawnHandlers.Clear(); unspawnHandlers.Clear(); } @@ -380,25 +357,7 @@ internal static void OnSpawnPrefab(NetworkConnection conn, SpawnPrefabMessage ms return; } - if (GetPrefab(msg.assetId, out GameObject prefab)) - { - GameObject obj = Object.Instantiate(prefab, msg.position, msg.rotation); - if (LogFilter.Debug) - { - Debug.Log("Client spawn handler instantiating [netId:" + msg.netId + " asset ID:" + msg.assetId + " pos:" + msg.position + " rotation: " + msg.rotation + "]"); - } - - localObject = obj.GetComponent(); - if (localObject == null) - { - Debug.LogError("Client object spawned for " + msg.assetId + " does not have a NetworkIdentity"); - return; - } - localObject.Reset(); - ApplySpawnPayload(localObject, msg.position, msg.rotation, msg.scale, msg.payload, msg.netId); - } - // lookup registered factory for type: - else if (spawnHandlers.TryGetValue(msg.assetId, out SpawnDelegate handler)) + if (spawnHandlers.TryGetValue(msg.assetId, out SpawnDelegate handler)) { GameObject obj = handler(msg.position, msg.assetId); if (obj == null)