From 7a8ff466f2fb227a78c2f8e98e70a80ec66d0f2c Mon Sep 17 00:00:00 2001 From: GeekBrony Date: Thu, 15 Feb 2024 18:25:37 -0600 Subject: [PATCH] fix(ClientObjectManager): add prefab to SpawnHandler when registering (#1171) --- Assets/Mirage/Runtime/ClientObjectManager.cs | 26 +++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/Assets/Mirage/Runtime/ClientObjectManager.cs b/Assets/Mirage/Runtime/ClientObjectManager.cs index 9055898438..112543415e 100644 --- a/Assets/Mirage/Runtime/ClientObjectManager.cs +++ b/Assets/Mirage/Runtime/ClientObjectManager.cs @@ -301,9 +301,10 @@ public void UnregisterPrefab(NetworkIdentity identity) /// A method to use as a custom un-spawnhandler on clients. public void RegisterSpawnHandler(NetworkIdentity identity, SpawnHandlerDelegate spawnHandler, UnSpawnDelegate unspawnHandler) { - ThrowIfZeroHash(identity); var prefabHash = identity.PrefabHash; - RegisterSpawnHandler(prefabHash, spawnHandler, unspawnHandler); + ValidateRegisterSpawnHandler(prefabHash, spawnHandler, unspawnHandler); + + _handlers[prefabHash] = new SpawnHandler(identity, spawnHandler, unspawnHandler); } /// @@ -322,9 +323,10 @@ public void RegisterSpawnHandler(int prefabHash, SpawnHandlerDelegate spawnHandl public void RegisterSpawnHandler(NetworkIdentity identity, SpawnHandlerAsyncDelegate spawnHandler, UnSpawnDelegate unspawnHandler) { - ThrowIfZeroHash(identity); var prefabHash = identity.PrefabHash; - RegisterSpawnHandler(prefabHash, spawnHandler, unspawnHandler); + ValidateRegisterSpawnHandler(prefabHash, spawnHandler, unspawnHandler); + + _handlers[prefabHash] = new SpawnHandler(identity, spawnHandler, unspawnHandler); } public void RegisterSpawnHandler(int prefabHash, SpawnHandlerAsyncDelegate spawnHandler, UnSpawnDelegate unspawnHandler) @@ -784,6 +786,14 @@ public SpawnHandler(SpawnHandlerDelegate spawnHandler, UnSpawnDelegate unspawnHa UnspawnHandler = unspawnHandler; } + public SpawnHandler(NetworkIdentity prefab, SpawnHandlerDelegate spawnHandler, UnSpawnDelegate unspawnHandler) + { + Prefab = prefab ?? throw new ArgumentNullException(nameof(prefab)); + Handler = spawnHandler ?? throw new ArgumentNullException(nameof(spawnHandler)); + // unspawn is allowed to be null + UnspawnHandler = unspawnHandler; + } + public SpawnHandler(SpawnHandlerAsyncDelegate spawnHandlerAsync, UnSpawnDelegate unspawnHandler) { HandlerAsync = spawnHandlerAsync ?? throw new ArgumentNullException(nameof(spawnHandlerAsync)); @@ -791,6 +801,14 @@ public SpawnHandler(SpawnHandlerAsyncDelegate spawnHandlerAsync, UnSpawnDelegate UnspawnHandler = unspawnHandler; } + public SpawnHandler(NetworkIdentity prefab, SpawnHandlerAsyncDelegate spawnHandlerAsync, UnSpawnDelegate unspawnHandler) + { + Prefab = prefab ?? throw new ArgumentNullException(nameof(prefab)); + HandlerAsync = spawnHandlerAsync ?? throw new ArgumentNullException(nameof(spawnHandlerAsync)); + // unspawn is allowed to be null + UnspawnHandler = unspawnHandler; + } + public void AddUnspawnHandler(UnSpawnDelegate unspawnHandler) { if (Prefab == null)