Removed NetworkScene, moved spawners to ClientScene

This commit is contained in:
vis2k 2018-12-17 13:25:36 +01:00
parent 5f3f057f1b
commit c27a9ad1bd
3 changed files with 140 additions and 198 deletions

View File

@ -14,7 +14,6 @@ public static class ClientScene
static bool s_IsReady;
static bool s_IsSpawnFinished;
static NetworkScene s_NetworkScene = new NetworkScene();
internal static void SetNotReady()
{
@ -27,13 +26,18 @@ internal static void SetNotReady()
public static bool ready { get { return s_IsReady; } }
public static NetworkConnection readyConnection { get { return s_ReadyConnection; }}
public static Dictionary<Guid, GameObject> prefabs { get { return NetworkScene.guidToPrefab; } }
public static Dictionary<Guid, GameObject> prefabs = new Dictionary<Guid, GameObject>();
// scene id to NetworkIdentity
public static Dictionary<uint, NetworkIdentity> spawnableObjects;
// spawn handlers
internal static Dictionary<Guid, SpawnDelegate> spawnHandlers = new Dictionary<Guid, SpawnDelegate>();
internal static Dictionary<Guid, UnSpawnDelegate> unspawnHandlers = new Dictionary<Guid, UnSpawnDelegate>();
internal static void Shutdown()
{
s_NetworkScene.Shutdown();
NetworkIdentity.spawned.Clear();
ClearSpawners();
s_PendingOwnerNetIds = new List<uint>();
spawnableObjects = null;
s_ReadyConnection = null;
@ -229,46 +233,167 @@ internal static void RegisterSystemHandlers(NetworkClient client, bool localClie
client.RegisterHandler(MsgType.AnimationTrigger, NetworkAnimator.OnAnimationTriggerClientMessage);
}
// ------------------------ NetworkScene pass-throughs ---------------------
// spawn handlers and prefabs //////////////////////////////////////////
internal static bool GetPrefab(Guid assetId, out GameObject prefab)
{
prefab = null;
if (assetId != Guid.Empty && prefabs.ContainsKey(assetId) && prefabs[assetId] != null)
{
prefab = prefabs[assetId];
return true;
}
return false;
}
// 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)
{
NetworkScene.RegisterPrefab(prefab, newAssetId);
NetworkIdentity view = prefab.GetComponent<NetworkIdentity>();
if (view)
{
view.SetDynamicAssetId(newAssetId);
if (LogFilter.Debug) { Debug.Log("Registering prefab '" + prefab.name + "' as asset:" + view.assetId); }
prefabs[view.assetId] = prefab;
}
else
{
Debug.LogError("Could not register '" + prefab.name + "' since it contains no NetworkIdentity component");
}
}
public static void RegisterPrefab(GameObject prefab)
{
NetworkScene.RegisterPrefab(prefab);
NetworkIdentity view = prefab.GetComponent<NetworkIdentity>();
if (view)
{
if (LogFilter.Debug) { Debug.Log("Registering prefab '" + prefab.name + "' as asset:" + view.assetId); }
prefabs[view.assetId] = prefab;
var uvs = prefab.GetComponentsInChildren<NetworkIdentity>();
if (uvs.Length > 1)
{
Debug.LogWarning("The prefab '" + prefab.name +
"' has multiple NetworkIdentity components. There can only be one NetworkIdentity on a prefab, and it must be on the root object.");
}
}
else
{
Debug.LogError("Could not register '" + prefab.name + "' since it contains no NetworkIdentity component");
}
}
public static void RegisterPrefab(GameObject prefab, SpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
{
NetworkScene.RegisterPrefab(prefab, spawnHandler, unspawnHandler);
NetworkIdentity identity = prefab.GetComponent<NetworkIdentity>();
if (identity == null)
{
Debug.LogError("Could not register '" + prefab.name + "' since it contains no NetworkIdentity component");
return;
}
if (spawnHandler == null || unspawnHandler == null)
{
Debug.LogError("RegisterPrefab custom spawn function null for " + identity.assetId);
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;
}
public static void UnregisterPrefab(GameObject prefab)
{
NetworkScene.UnregisterPrefab(prefab);
NetworkIdentity identity = prefab.GetComponent<NetworkIdentity>();
if (identity == null)
{
Debug.LogError("Could not unregister '" + prefab.name + "' since it contains no NetworkIdentity component");
return;
}
spawnHandlers.Remove(identity.assetId);
unspawnHandlers.Remove(identity.assetId);
}
public static void RegisterSpawnHandler(Guid assetId, SpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
{
NetworkScene.RegisterSpawnHandler(assetId, spawnHandler, unspawnHandler);
if (spawnHandler == null || unspawnHandler == null)
{
Debug.LogError("RegisterSpawnHandler custom spawn function null for " + assetId);
return;
}
if (LogFilter.Debug) { Debug.Log("RegisterSpawnHandler asset '" + assetId + "' " + spawnHandler.GetMethodName() + "/" + unspawnHandler.GetMethodName()); }
spawnHandlers[assetId] = spawnHandler;
unspawnHandlers[assetId] = unspawnHandler;
}
public static void UnregisterSpawnHandler(Guid assetId)
{
NetworkScene.UnregisterSpawnHandler(assetId);
spawnHandlers.Remove(assetId);
unspawnHandlers.Remove(assetId);
}
public static void ClearSpawners()
{
NetworkScene.ClearSpawners();
prefabs.Clear();
spawnHandlers.Clear();
unspawnHandlers.Clear();
}
internal static bool GetSpawnHandler(Guid assetId, out SpawnDelegate handler)
{
if (spawnHandlers.ContainsKey(assetId))
{
handler = spawnHandlers[assetId];
return true;
}
handler = null;
return false;
}
internal static bool InvokeUnSpawnHandler(Guid assetId, GameObject obj)
{
if (unspawnHandlers.ContainsKey(assetId) && unspawnHandlers[assetId] != null)
{
UnSpawnDelegate handler = unspawnHandlers[assetId];
handler(obj);
return true;
}
return false;
}
public static void DestroyAllClientObjects()
{
s_NetworkScene.DestroyAllClientObjects();
foreach (var netId in NetworkIdentity.spawned.Keys)
{
NetworkIdentity uv = NetworkIdentity.spawned[netId];
if (uv != null && uv.gameObject != null)
{
if (!InvokeUnSpawnHandler(uv.assetId, uv.gameObject))
{
if (uv.sceneId == 0)
{
Object.Destroy(uv.gameObject);
}
else
{
uv.MarkForReset();
uv.gameObject.SetActive(false);
}
}
}
}
NetworkIdentity.spawned.Clear();
}
public static GameObject FindLocalObject(uint netId)
@ -328,7 +453,7 @@ static void OnSpawnPrefab(NetworkMessage netMsg)
GameObject prefab;
SpawnDelegate handler;
if (NetworkScene.GetPrefab(msg.assetId, out prefab))
if (GetPrefab(msg.assetId, out prefab))
{
GameObject obj = Object.Instantiate(prefab, msg.position, msg.rotation);
if (LogFilter.Debug)
@ -346,7 +471,7 @@ static void OnSpawnPrefab(NetworkMessage netMsg)
ApplySpawnPayload(localObject, msg.position, msg.payload, msg.netId);
}
// lookup registered factory for type:
else if (NetworkScene.GetSpawnHandler(msg.assetId, out handler))
else if (GetSpawnHandler(msg.assetId, out handler))
{
GameObject obj = handler(msg.position, msg.assetId);
if (obj == null)
@ -436,7 +561,7 @@ static void OnObjectDestroy(NetworkMessage netMsg)
{
localObject.OnNetworkDestroy();
if (!NetworkScene.InvokeUnSpawnHandler(localObject.assetId, localObject.gameObject))
if (!InvokeUnSpawnHandler(localObject.assetId, localObject.gameObject))
{
// default handling
if (localObject.sceneId == 0)

View File

@ -70,7 +70,6 @@
<Compile Include="Messages.cs" />
<Compile Include="NetworkAnimator.cs" />
<Compile Include="NetworkProximityChecker.cs" />
<Compile Include="NetworkScene.cs" />
<Compile Include="NetworkStartPosition.cs" />
<Compile Include="NetworkTransformChild.cs" />
<Compile Include="NetworkTransform.cs" />

View File

@ -1,182 +0,0 @@
using System.Collections.Generic;
using UnityEngine;
using Guid = System.Guid;
namespace Mirror
{
// This is an internal class to allow the client and server to share scene-related functionality.
// This code (mostly) used to be in ClientScene.
internal class NetworkScene
{
internal static Dictionary<Guid, GameObject> guidToPrefab = new Dictionary<Guid, GameObject>();
internal static Dictionary<Guid, SpawnDelegate> spawnHandlers = new Dictionary<Guid, SpawnDelegate>();
internal static Dictionary<Guid, UnSpawnDelegate> unspawnHandlers = new Dictionary<Guid, UnSpawnDelegate>();
internal void Shutdown()
{
NetworkIdentity.spawned.Clear();
ClearSpawners();
}
internal static void RegisterPrefab(GameObject prefab, Guid newAssetId)
{
NetworkIdentity view = prefab.GetComponent<NetworkIdentity>();
if (view)
{
view.SetDynamicAssetId(newAssetId);
if (LogFilter.Debug) { Debug.Log("Registering prefab '" + prefab.name + "' as asset:" + view.assetId); }
guidToPrefab[view.assetId] = prefab;
}
else
{
Debug.LogError("Could not register '" + prefab.name + "' since it contains no NetworkIdentity component");
}
}
internal static void RegisterPrefab(GameObject prefab)
{
NetworkIdentity view = prefab.GetComponent<NetworkIdentity>();
if (view)
{
if (LogFilter.Debug) { Debug.Log("Registering prefab '" + prefab.name + "' as asset:" + view.assetId); }
guidToPrefab[view.assetId] = prefab;
var uvs = prefab.GetComponentsInChildren<NetworkIdentity>();
if (uvs.Length > 1)
{
Debug.LogWarning("The prefab '" + prefab.name +
"' has multiple NetworkIdentity components. There can only be one NetworkIdentity on a prefab, and it must be on the root object.");
}
}
else
{
Debug.LogError("Could not register '" + prefab.name + "' since it contains no NetworkIdentity component");
}
}
internal static bool GetPrefab(Guid assetId, out GameObject prefab)
{
prefab = null;
if (assetId != Guid.Empty && guidToPrefab.ContainsKey(assetId) && guidToPrefab[assetId] != null)
{
prefab = guidToPrefab[assetId];
return true;
}
return false;
}
internal static void ClearSpawners()
{
guidToPrefab.Clear();
spawnHandlers.Clear();
unspawnHandlers.Clear();
}
public static void UnregisterSpawnHandler(Guid assetId)
{
spawnHandlers.Remove(assetId);
unspawnHandlers.Remove(assetId);
}
internal static void RegisterSpawnHandler(Guid assetId, SpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
{
if (spawnHandler == null || unspawnHandler == null)
{
Debug.LogError("RegisterSpawnHandler custom spawn function null for " + assetId);
return;
}
if (LogFilter.Debug) { Debug.Log("RegisterSpawnHandler asset '" + assetId + "' " + spawnHandler.GetMethodName() + "/" + unspawnHandler.GetMethodName()); }
spawnHandlers[assetId] = spawnHandler;
unspawnHandlers[assetId] = unspawnHandler;
}
internal static void UnregisterPrefab(GameObject prefab)
{
NetworkIdentity identity = prefab.GetComponent<NetworkIdentity>();
if (identity == null)
{
Debug.LogError("Could not unregister '" + prefab.name + "' since it contains no NetworkIdentity component");
return;
}
spawnHandlers.Remove(identity.assetId);
unspawnHandlers.Remove(identity.assetId);
}
internal static void RegisterPrefab(GameObject prefab, SpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
{
NetworkIdentity identity = prefab.GetComponent<NetworkIdentity>();
if (identity == null)
{
Debug.LogError("Could not register '" + prefab.name + "' since it contains no NetworkIdentity component");
return;
}
if (spawnHandler == null || unspawnHandler == null)
{
Debug.LogError("RegisterPrefab custom spawn function null for " + identity.assetId);
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;
}
internal static bool GetSpawnHandler(Guid assetId, out SpawnDelegate handler)
{
if (spawnHandlers.ContainsKey(assetId))
{
handler = spawnHandlers[assetId];
return true;
}
handler = null;
return false;
}
internal static bool InvokeUnSpawnHandler(Guid assetId, GameObject obj)
{
if (unspawnHandlers.ContainsKey(assetId) && unspawnHandlers[assetId] != null)
{
UnSpawnDelegate handler = unspawnHandlers[assetId];
handler(obj);
return true;
}
return false;
}
internal void DestroyAllClientObjects()
{
foreach (var netId in NetworkIdentity.spawned.Keys)
{
NetworkIdentity uv = NetworkIdentity.spawned[netId];
if (uv != null && uv.gameObject != null)
{
if (!InvokeUnSpawnHandler(uv.assetId, uv.gameObject))
{
if (uv.sceneId == 0)
{
Object.Destroy(uv.gameObject);
}
else
{
uv.MarkForReset();
uv.gameObject.SetActive(false);
}
}
}
}
NetworkIdentity.spawned.Clear();
}
}
}