Skip to content

Commit

Permalink
fix: Revert "refactor: consolidate prefab and spawn handlers (#817)" …
Browse files Browse the repository at this point in the history
…to fix a bug where if editor=host, build=client, we receive scene object not found when walking out of and back into an observer's range

This reverts commit b4c9c6f.
  • Loading branch information
miwarnec committed Apr 24, 2019
1 parent 5480548 commit 1f07af0
Showing 1 changed file with 54 additions and 13 deletions.
67 changes: 54 additions & 13 deletions Assets/Mirror/Runtime/ClientScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public static class ClientScene
public static bool ready { get; internal set; }
public static NetworkConnection readyConnection { get; private set; }

[Obsolete]
public static Dictionary<Guid, GameObject> prefabs = new Dictionary<Guid, GameObject>();
// scene id to NetworkIdentity
public static Dictionary<ulong, NetworkIdentity> spawnableObjects;
Expand Down Expand Up @@ -176,20 +175,38 @@ 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)
{
RegisterSpawnHandler(newAssetId,
(position, assetId) => Object.Instantiate(prefab),
(spawned) => Object.Destroy(spawned));
NetworkIdentity identity = prefab.GetComponent<NetworkIdentity>();
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");
}
}

public static void RegisterPrefab(GameObject prefab)
{
NetworkIdentity identity = prefab.GetComponent<NetworkIdentity>();
if (identity)
{
RegisterPrefab(prefab, identity.assetId);
if (LogFilter.Debug) Debug.Log("Registering prefab '" + prefab.name + "' as asset:" + identity.assetId);
prefabs[identity.assetId] = prefab;

NetworkIdentity[] identities = prefab.GetComponentsInChildren<NetworkIdentity>();
if (identities.Length > 1)
Expand Down Expand Up @@ -219,7 +236,16 @@ public static void RegisterPrefab(GameObject prefab, SpawnDelegate spawnHandler,
return;
}

RegisterSpawnHandler(identity.assetId, spawnHandler, unspawnHandler);
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;
}

public static void UnregisterPrefab(GameObject prefab)
Expand All @@ -230,7 +256,8 @@ public static void UnregisterPrefab(GameObject prefab)
Debug.LogError("Could not unregister '" + prefab.name + "' since it contains no NetworkIdentity component");
return;
}
UnregisterSpawnHandler(identity.assetId);
spawnHandlers.Remove(identity.assetId);
unspawnHandlers.Remove(identity.assetId);
}

public static void RegisterSpawnHandler(Guid assetId, SpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
Expand All @@ -241,11 +268,6 @@ 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;
Expand All @@ -260,6 +282,7 @@ public static void UnregisterSpawnHandler(Guid assetId)

public static void ClearSpawners()
{
prefabs.Clear();
spawnHandlers.Clear();
unspawnHandlers.Clear();
}
Expand Down Expand Up @@ -357,7 +380,25 @@ internal static void OnSpawnPrefab(NetworkConnection conn, SpawnPrefabMessage ms
return;
}

if (spawnHandlers.TryGetValue(msg.assetId, out SpawnDelegate handler))
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<NetworkIdentity>();
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))
{
GameObject obj = handler(msg.position, msg.assetId);
if (obj == null)
Expand Down

0 comments on commit 1f07af0

Please sign in to comment.