diff --git a/Assets/Mirror/Runtime/ClientScene.cs b/Assets/Mirror/Runtime/ClientScene.cs index a5b55f276..b45095aca 100644 --- a/Assets/Mirror/Runtime/ClientScene.cs +++ b/Assets/Mirror/Runtime/ClientScene.cs @@ -8,18 +8,49 @@ namespace Mirror { + /// + /// A client manager which contains static client information and functions. + /// This manager contains references to tracked static local objects such as spawner registrations. It also has the default message handlers used by clients when they registered none themselves. The manager handles adding/removing player objects to the game after a client connection has been set as ready. + /// The ClientScene is a singleton, and it has static convenience methods such as ClientScene.Ready(). + /// The ClientScene is used by the NetworkManager, but it can be used by itself. + /// As the ClientScene manages player objects on the client, it is where clients request to add players. The NetworkManager does this via the ClientScene automatically when auto-add-players is set, but it can be done through code using the function ClientScene.AddPlayer(). This sends an AddPlayer message to the server and will cause a player object to be created for this client. + /// Like NetworkServer, the ClientScene understands the concept of the local client. The function ClientScene.ConnectLocalServer() is used to become a host by starting a local client (when a server is already running). + /// public static class ClientScene { static bool isSpawnFinished; static HashSet pendingOwnerNetIds = new HashSet(); + /// + /// NetworkIdentity of the localPlayer + /// public static NetworkIdentity localPlayer { get; private set; } + + /// + /// Returns true when a client's connection has been set to ready. + /// A client that is ready recieves state updates from the server, while a client that is not ready does not. This useful when the state of the game is not normal, such as a scene change or end-of-game. + /// This is read-only. To change the ready state of a client, use ClientScene.Ready(). The server is able to set the ready state of clients using NetworkServer.SetClientReady(), NetworkServer.SetClientNotReady() and NetworkServer.SetAllClientsNotReady(). + /// This is done when changing scenes so that clients don't receive state update messages during scene loading. + /// public static bool ready { get; internal set; } + + /// + /// The NetworkConnection object that is currently "ready". This is the connection to the server where objects are spawned from. + /// This connection can be used to send messages to the server. There can only be one ready connection at a time. There can be multiple NetworkClient instances in existence, each with their own NetworkConnections, but there is only one ClientScene instance and corresponding ready connection. + /// public static NetworkConnection readyConnection { get; private set; } + /// + /// This is a dictionary of the prefabs that are registered on the client with ClientScene.RegisterPrefab(). + /// The key to the dictionary is the prefab asset Id. + /// public static Dictionary prefabs = new Dictionary(); - // scene id to NetworkIdentity + + /// + /// This is dictionary of the disabled NetworkIdentity objects in the scene that could be spawned by messages from the server. + /// The key to the dictionary is the NetworkIdentity sceneId. + /// public static Dictionary spawnableObjects; // spawn handlers @@ -57,14 +88,27 @@ internal static void InternalAddPlayer(NetworkIdentity identity) } } - // use this if already ready + /// + /// This adds a player GameObject for this client. + /// This causes an AddPlayer message to be sent to the server, and NetworkManager.OnServerAddPlayer is called. + /// + /// True if player was added. public static bool AddPlayer() => AddPlayer(null); - // use this to implicitly become ready + /// + /// This adds a player GameObject for this client. This causes an AddPlayer message to be sent to the server, and NetworkManager.OnServerAddPlayer is called. If an extra message was passed to AddPlayer, then OnServerAddPlayer will be called with a NetworkReader that contains the contents of the message. + /// + /// The connection to become ready for this client. + /// True if player was added. public static bool AddPlayer(NetworkConnection readyConn) => AddPlayer(readyConn, null); - // use this to implicitly become ready - // -> extraMessage can contain character selection, etc. + /// + /// This adds a player GameObject for this client. This causes an AddPlayer message to be sent to the server, and NetworkManager.OnServerAddPlayer is called. If an extra message was passed to AddPlayer, then OnServerAddPlayer will be called with a NetworkReader that contains the contents of the message. + /// extraMessage can contain character selection, etc. + /// + /// The connection to become ready for this client. + /// An extra message object that can be passed to the server for this player. + /// True if player was added. public static bool AddPlayer(NetworkConnection readyConn, byte[] extraData) { // ensure valid ready connection @@ -96,6 +140,10 @@ public static bool AddPlayer(NetworkConnection readyConn, byte[] extraData) return true; } + /// + /// Removes the player from the game. + /// + /// True if succcessful public static bool RemovePlayer() { if (LogFilter.Debug) Debug.Log("ClientScene.RemovePlayer() called with connection [" + readyConnection + "]"); @@ -114,6 +162,12 @@ public static bool RemovePlayer() return false; } + /// + /// Signal that the client connection is ready to enter the game. + /// This could be for example when a client enters an ongoing game and has finished loading the current scene. The server should respond to the SYSTEM_READY event with an appropriate handler which instantiates the players object for example. + /// + /// The client connection which is ready. + /// True if succcessful public static bool Ready(NetworkConnection conn) { if (ready) @@ -154,8 +208,9 @@ static bool ConsiderForSpawning(NetworkIdentity identity) identity.sceneId != 0; } - // this needs to be public. If users load/unload a scene in the client after connection - // they should call this to register the spawnable objects + /// + /// Call this after loading/unloading a scene in the client after connection to register the spawnable objects + /// public static void PrepareToSpawnSceneObjects() { // add all unspawned NetworkIdentities to spawnable objects @@ -183,7 +238,14 @@ static bool GetPrefab(Guid assetId, out GameObject prefab) 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. + /// + /// Registers a prefab with the spawning system. + /// When a NetworkIdentity object is spawned on a server with NetworkServer.SpawnObject(), and the prefab that the object was created from was registered with RegisterPrefab(), the client will use that prefab to instantiate a corresponding client object with the same netId. + /// The NetworkManager has a list of spawnable prefabs, it uses this function to register those prefabs with the ClientScene. + /// The set of current spawnable object is available in the ClientScene static member variable ClientScene.prefabs, which is a dictionary of NetworkAssetIds and prefab references. + /// + /// A Prefab that will be spawned. + /// An assetId to be assigned to this prefab. This allows a dynamically created game object to be registered for an already known asset Id. public static void RegisterPrefab(GameObject prefab, Guid newAssetId) { NetworkIdentity identity = prefab.GetComponent(); @@ -200,6 +262,13 @@ public static void RegisterPrefab(GameObject prefab, Guid newAssetId) } } + /// + /// Registers a prefab with the spawning system. + /// When a NetworkIdentity object is spawned on a server with NetworkServer.SpawnObject(), and the prefab that the object was created from was registered with RegisterPrefab(), the client will use that prefab to instantiate a corresponding client object with the same netId. + /// The NetworkManager has a list of spawnable prefabs, it uses this function to register those prefabs with the ClientScene. + /// The set of current spawnable object is available in the ClientScene static member variable ClientScene.prefabs, which is a dictionary of NetworkAssetIds and prefab references. + /// + /// A Prefab that will be spawned. public static void RegisterPrefab(GameObject prefab) { NetworkIdentity identity = prefab.GetComponent(); @@ -221,6 +290,15 @@ public static void RegisterPrefab(GameObject prefab) } } + /// + /// Registers a prefab with the spawning system. + /// When a NetworkIdentity object is spawned on a server with NetworkServer.SpawnObject(), and the prefab that the object was created from was registered with RegisterPrefab(), the client will use that prefab to instantiate a corresponding client object with the same netId. + /// The NetworkManager has a list of spawnable prefabs, it uses this function to register those prefabs with the ClientScene. + /// The set of current spawnable object is available in the ClientScene static member variable ClientScene.prefabs, which is a dictionary of NetworkAssetIds and prefab references. + /// + /// A Prefab that will be spawned. + /// A method to use as a custom spawnhandler on clients. + /// A method to use as a custom un-spawnhandler on clients. public static void RegisterPrefab(GameObject prefab, SpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler) { NetworkIdentity identity = prefab.GetComponent(); @@ -248,6 +326,10 @@ public static void RegisterPrefab(GameObject prefab, SpawnDelegate spawnHandler, unspawnHandlers[identity.assetId] = unspawnHandler; } + /// + /// Removes a registered spawn prefab that was setup with ClientScene.RegisterPrefab. + /// + /// The prefab to be removed from registration. public static void UnregisterPrefab(GameObject prefab) { NetworkIdentity identity = prefab.GetComponent(); @@ -260,6 +342,13 @@ public static void UnregisterPrefab(GameObject prefab) unspawnHandlers.Remove(identity.assetId); } + /// + /// This is an advanced spawning function that registers a custom assetId with the UNET spawning system. + /// This can be used to register custom spawning methods for an assetId - instead of the usual method of registering spawning methods for a prefab. This should be used when no prefab exists for the spawned objects - such as when they are constructed dynamically at runtime from configuration data. + /// + /// Custom assetId string. + /// A method to use as a custom spawnhandler on clients. + /// A method to use as a custom un-spawnhandler on clients. public static void RegisterSpawnHandler(Guid assetId, SpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler) { if (spawnHandler == null || unspawnHandler == null) @@ -274,12 +363,19 @@ public static void RegisterSpawnHandler(Guid assetId, SpawnDelegate spawnHandler unspawnHandlers[assetId] = unspawnHandler; } + /// + /// Removes a registered spawn handler function that was registered with ClientScene.RegisterHandler(). + /// + /// The assetId for the handler to be removed for. public static void UnregisterSpawnHandler(Guid assetId) { spawnHandlers.Remove(assetId); unspawnHandlers.Remove(assetId); } + /// + /// This clears the registered spawn prefabs and spawn handler functions for this client. + /// public static void ClearSpawners() { prefabs.Clear(); @@ -297,6 +393,10 @@ static bool InvokeUnSpawnHandler(Guid assetId, GameObject obj) return false; } + /// + /// Destroys all networked objects on the client. + /// This can be used to clean up when a network connection is closed. + /// public static void DestroyAllClientObjects() { foreach (NetworkIdentity identity in NetworkIdentity.spawned.Values) @@ -320,6 +420,9 @@ public static void DestroyAllClientObjects() NetworkIdentity.spawned.Clear(); } + /// + /// Obsolete: Use NetworkIdentity.spawned[netId] instead. + /// [EditorBrowsable(EditorBrowsableState.Never), Obsolete("Use NetworkIdentity.spawned[netId] instead.")] public static GameObject FindLocalObject(uint netId) {