private GameObject NetworkCreatePlayer(int connectionID, Vector3 position, Vector3 eulerAngles) { if (!ServerValidateInstantiate(Settings.m_PlayerObject)) { return(null); } CreateRegisteredObject(Settings.m_PlayerObject, position, eulerAngles, NetworkController.Instance.GetConnection(connectionID), out GameObject inst, out NetworkIdentity instIdentity); NetworkWriter writer = GetCreatePlayerWriter(connectionID, inst, instIdentity); NetworkController.Instance.SendToAll(NetworkController.ReliableSequencedChannel, CreatePlayerMsg, writer.ToArray()); if (PlayerObjects == null) { PlayerObjects = new List <NetworkPlayerObject>(); } if (m_PlayerObjectCache == null) { m_PlayerObjectCache = new Dictionary <int, NetworkPlayerObject>(); } NetworkPlayerObject netPlayer = new NetworkPlayerObject(connectionID, inst); PlayerObjects.Add(netPlayer); m_PlayerObjectCache[connectionID] = netPlayer; return(inst); }
private void OnDestroyPlayer(NetworkConnection conn) { NetworkPlayerObject playerObject = PlayerObjects?.Find(x => x.ConnectionID == conn.ConnectionID); if (playerObject != null) { if (SpawnedObjects?.Contains(playerObject.GameObject) ?? false) { SpawnedObjects.Remove(playerObject.GameObject); } DestroyedGameObject?.Invoke(playerObject.NetworkIdentity); Object.Destroy(playerObject.GameObject); PlayerObjects.Remove(playerObject); m_PlayerObjectCache?.Remove(conn.ConnectionID); } if (m_BufferedPlayersCreations?.ContainsKey(conn.ConnectionID) ?? false) { m_BufferedPlayersCreations.Remove(conn.ConnectionID); } if (m_BufferedOwnerCreations?.ContainsKey(conn.ConnectionID) ?? false) { m_BufferedOwnerCreations.Remove(conn.ConnectionID); } if (m_AuthroityObjects?.ContainsKey(conn) ?? false) { foreach (NetworkIdentity identity in m_AuthroityObjects[conn]) { if (identity != null) { Destroy(identity.gameObject); } } m_AuthroityObjects.Remove(conn); } m_BufferedPlayerConnections?.Remove(conn); }
private void OnNetworkSpawnPlayer(NetworkWriter writer) { if (IsServer) { return; } ReadInstantiationMessage(writer, out int connectionID, out int netID, out Vector3 position, out Quaternion rotation, out GameObject prefab, out _, out _); NetworkConnection conn = NetworkController.Instance.GetConnection(connectionID); if (conn == null || !NetworkController.Instance.IsOnlineScene) { if (m_BufferedPlayersCreations == null) { m_BufferedPlayersCreations = new Dictionary <int, NetworkWriter>(); } m_BufferedPlayersCreations[connectionID] = new NetworkWriter(writer.ToArray()); return; } GameObject inst = ClientInstantiateServerObject(netID, position, rotation, prefab, conn, null); inst.name += " [Connection " + connectionID + "]"; if (PlayerObjects == null) { PlayerObjects = new List <NetworkPlayerObject>(); } if (m_PlayerObjectCache == null) { m_PlayerObjectCache = new Dictionary <int, NetworkPlayerObject>(); } NetworkPlayerObject netPlayer = new NetworkPlayerObject(connectionID, inst); PlayerObjects.Add(netPlayer); m_PlayerObjectCache[connectionID] = netPlayer; }