diff --git a/Mirror/Runtime/ClientScene.cs b/Mirror/Runtime/ClientScene.cs index c9ee7cf99..5fd25108d 100644 --- a/Mirror/Runtime/ClientScene.cs +++ b/Mirror/Runtime/ClientScene.cs @@ -1,7 +1,9 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using UnityEngine; using Guid = System.Guid; +using Object = UnityEngine.Object; namespace Mirror { @@ -25,9 +27,6 @@ internal static void SetNotReady() public static bool ready { get { return s_IsReady; } } public static NetworkConnection readyConnection { get { return s_ReadyConnection; }} - //NOTE: spawn handlers, prefabs and local objects now live in NetworkScene - // objects by net id - public static Dictionary objects { get { return s_NetworkScene.localObjects; } } public static Dictionary prefabs { get { return NetworkScene.guidToPrefab; } } // scene id to NetworkIdentity public static Dictionary spawnableObjects; @@ -421,7 +420,7 @@ static void OnObjectSpawnFinished(NetworkMessage netMsg) // paul: Initialize the objects in the same order as they were initialized // in the server. This is important if spawned objects // use data from scene objects - foreach (var uv in objects.Values.OrderBy(uv => uv.netId)) + foreach (var uv in NetworkIdentity.spawned.Values.OrderBy(uv => uv.netId)) { if (!uv.isClient) { diff --git a/Mirror/Runtime/NetworkIdentity.cs b/Mirror/Runtime/NetworkIdentity.cs index ac7063c76..aabc83301 100644 --- a/Mirror/Runtime/NetworkIdentity.cs +++ b/Mirror/Runtime/NetworkIdentity.cs @@ -50,6 +50,9 @@ public sealed class NetworkIdentity : MonoBehaviour public bool localPlayerAuthority { get { return m_LocalPlayerAuthority; } set { m_LocalPlayerAuthority = value; } } public NetworkConnection clientAuthorityOwner { get { return m_ClientAuthorityOwner; }} + // all spawned NetworkIdentities by netId. needed on server and client. + public static Dictionary spawned = new Dictionary(); + public NetworkBehaviour[] NetworkBehaviours { get diff --git a/Mirror/Runtime/NetworkScene.cs b/Mirror/Runtime/NetworkScene.cs index 29b75eff3..66abd1da8 100644 --- a/Mirror/Runtime/NetworkScene.cs +++ b/Mirror/Runtime/NetworkScene.cs @@ -8,11 +8,6 @@ namespace Mirror // This code (mostly) used to be in ClientScene. internal class NetworkScene { - // localObjects is NOT static. For the Host, even though there is one scene and gameObjects are - // shared with the localClient, the set of active objects for each must be separate to prevent - // out-of-order object initialization problems. - internal Dictionary localObjects = new Dictionary(); - internal static Dictionary guidToPrefab = new Dictionary(); internal static Dictionary spawnHandlers = new Dictionary(); internal static Dictionary unspawnHandlers = new Dictionary(); @@ -29,20 +24,20 @@ internal void SetLocalObject(uint netId, GameObject obj, bool isClient, bool isS if (obj == null) { - localObjects[netId] = null; + NetworkIdentity.spawned[netId] = null; return; } NetworkIdentity foundNetworkIdentity = null; - if (localObjects.ContainsKey(netId)) + if (NetworkIdentity.spawned.ContainsKey(netId)) { - foundNetworkIdentity = localObjects[netId]; + foundNetworkIdentity = NetworkIdentity.spawned[netId]; } if (foundNetworkIdentity == null) { foundNetworkIdentity = obj.GetComponent(); - localObjects[netId] = foundNetworkIdentity; + NetworkIdentity.spawned[netId] = foundNetworkIdentity; } foundNetworkIdentity.UpdateClientServer(isClient, isServer); @@ -63,17 +58,17 @@ internal GameObject FindLocalObject(uint netId) internal bool GetNetworkIdentity(uint netId, out NetworkIdentity uv) { - return localObjects.TryGetValue(netId, out uv) && uv != null; + return NetworkIdentity.spawned.TryGetValue(netId, out uv) && uv != null; } internal bool RemoveLocalObject(uint netId) { - return localObjects.Remove(netId); + return NetworkIdentity.spawned.Remove(netId); } internal void ClearLocalObjects() { - localObjects.Clear(); + NetworkIdentity.spawned.Clear(); } internal static void RegisterPrefab(GameObject prefab, Guid newAssetId) @@ -214,9 +209,9 @@ internal static bool InvokeUnSpawnHandler(Guid assetId, GameObject obj) internal void DestroyAllClientObjects() { - foreach (var netId in localObjects.Keys) + foreach (var netId in NetworkIdentity.spawned.Keys) { - NetworkIdentity uv = localObjects[netId]; + NetworkIdentity uv = NetworkIdentity.spawned[netId]; if (uv != null && uv.gameObject != null) { diff --git a/Mirror/Runtime/NetworkServer.cs b/Mirror/Runtime/NetworkServer.cs index ba892bd63..c89561fca 100644 --- a/Mirror/Runtime/NetworkServer.cs +++ b/Mirror/Runtime/NetworkServer.cs @@ -30,7 +30,6 @@ public sealed class NetworkServer public static Dictionary connections = new Dictionary(); public static Dictionary handlers = new Dictionary(); - public static Dictionary objects { get { return s_NetworkScene.localObjects; } } public static bool dontListen; public static bool useWebSockets; @@ -201,7 +200,7 @@ internal static void ActivateLocalClientScene() // ClientScene for a local connection is becoming active. any spawned objects need to be started as client objects s_LocalClientActive = true; - foreach (var uv in objects.Values) + foreach (var uv in NetworkIdentity.spawned.Values) { if (!uv.isClient) { @@ -322,7 +321,7 @@ static void UpdateServerObjects() // vis2k: original code only removed null entries every 100 frames. this was unnecessarily complicated and // probably even slower than removing null entries each time (hence less iterations next time). List removeNetIds = new List(); - foreach (var kvp in objects) + foreach (var kvp in NetworkIdentity.spawned) { if (kvp.Value != null && kvp.Value.gameObject != null) { @@ -337,7 +336,7 @@ static void UpdateServerObjects() // now remove foreach (uint netId in removeNetIds) { - objects.Remove(netId); + NetworkIdentity.spawned.Remove(netId); } } @@ -748,7 +747,7 @@ internal static void SetClientReadyInternal(NetworkConnection conn) // Setup spawned objects for local player // Only handle the local objects for the first player (no need to redo it when doing more local players) // and don't handle player objects here, they were done above - foreach (NetworkIdentity uv in objects.Values) + foreach (NetworkIdentity uv in NetworkIdentity.spawned.Values) { // Need to call OnStartClient directly here, as it's already been added to the local object dictionary // in the above SetLocalPlayer call @@ -770,13 +769,13 @@ internal static void SetClientReadyInternal(NetworkConnection conn) } // Spawn/update all current server objects - if (LogFilter.Debug) { Debug.Log("Spawning " + objects.Count + " objects for conn " + conn.connectionId); } + if (LogFilter.Debug) { Debug.Log("Spawning " + NetworkIdentity.spawned.Count + " objects for conn " + conn.connectionId); } ObjectSpawnFinishedMessage msg = new ObjectSpawnFinishedMessage(); msg.state = 0; conn.Send((short)MsgType.SpawnFinished, msg); - foreach (NetworkIdentity uv in objects.Values) + foreach (NetworkIdentity uv in NetworkIdentity.spawned.Values) { if (uv == null) { @@ -1041,9 +1040,9 @@ static void DestroyObject(GameObject obj) static void DestroyObject(NetworkIdentity uv, bool destroyServerObject) { if (LogFilter.Debug) { Debug.Log("DestroyObject instance:" + uv.netId); } - if (objects.ContainsKey(uv.netId)) + if (NetworkIdentity.spawned.ContainsKey(uv.netId)) { - objects.Remove(uv.netId); + NetworkIdentity.spawned.Remove(uv.netId); } if (uv.clientAuthorityOwner != null) @@ -1072,7 +1071,7 @@ static void DestroyObject(NetworkIdentity uv, bool destroyServerObject) public static void ClearLocalObjects() { - objects.Clear(); + NetworkIdentity.spawned.Clear(); } public static void Spawn(GameObject obj)