Deprecations

Room Player Spawn

The number of scripts required to create players in the room has been reduced. It was made more useful by writing a utility class.

New Usage:

public class RoomPlayerSpawn : MonoBehaviour
{
    [SerializeField] private GameObject _gamePlayerPrefab;
    [SerializeField] private GameObject _lobbyPlayerPrefab;

    private int _playerCount;

    [SerializeField] private bool _useLobby;
    
    private void Start()
    {
        RoomManagerBase.Singleton.Events.OnServerJoinedRoom += CreateGamePlayer;
        
        if (_useLobby)
            RoomManagerBase.Singleton.Events.OnServerExitedRoom += CreateLobbyPlayer;
        else
            RoomManagerBase.Singleton.Events.OnServerExitedRoom += PlayerCreatorUtilities.RemovePlayer;
    }
    
    private void CreateGamePlayer(NetworkConnection conn)
    {
        var player = PlayerCreatorUtilities.TryCreatePlayerOrReplace(conn, _gamePlayerPrefab);

        if (player.TryGetComponent(out SimpleCharacterController controller))
        {
            controller.ID = conn.connectionId;
        }
    }
    
    private void CreateLobbyPlayer(NetworkConnection conn)
    {
        PlayerCreatorUtilities.ReplacePlayer(conn, _lobbyPlayerPrefab);
    }
}

Old Usage:

 public class RoomPlayerSpawn : MonoBehaviour
    {
        [SerializeField] private GameObject _gamePlayerPrefab;
        [SerializeField] private GameObject _lobbyPlayerPrefab;

        private int _playerCount;

        [SerializeField] private bool _useLobby;

        private void Start()
        {
            RoomManagerBase.Singleton.Events.OnServerJoinedRoom += CreateGamePlayer;

            if (_useLobby)
                RoomManagerBase.Singleton.Events.OnServerExitedRoom += CreateLobbyPlayer;
            else
                RoomManagerBase.Singleton.Events.OnServerExitedRoom += RemovePlayer;
        }


        private void CreateGamePlayer(NetworkConnection conn)
        {
            if (conn.identity != null)
            {
                StartCoroutine(ReplacePlayer(conn, _gamePlayerPrefab));

                return;
            }

            CreatePlayer(conn, _gamePlayerPrefab);
        }

        private void CreateLobbyPlayer(NetworkConnection conn)
        {
            if (conn.identity == null) return;

            StartCoroutine(ReplacePlayer(conn, _lobbyPlayerPrefab));
        }

        private IEnumerator ReplacePlayer(NetworkConnection conn, GameObject prefab)
        {
            var oldPlayer = conn.identity.gameObject;
            
            var newPlayer = SpawnPlayer(conn, prefab);

            NetworkServer.ReplacePlayerForConnection(conn.identity.connectionToClient, newPlayer, true);

            yield return new WaitForEndOfFrame();
            
            if (newPlayer.TryGetComponent(out SimpleCharacterController controller))
            {
                controller.ID = conn.connectionId;
            }
            
            if(oldPlayer != null)
                NetworkServer.Destroy(oldPlayer);
        }

        private void CreatePlayer(NetworkConnection conn, GameObject prefab)
        {
            if (conn is not NetworkConnectionToClient connectionToClient) return;
            
            var player = SpawnPlayer(conn, prefab);

            NetworkServer.AddPlayerForConnection(connectionToClient, player);
            
            if (player.TryGetComponent(out SimpleCharacterController controller))
            {
                controller.ID = conn.connectionId;
            }
        }

        private void RemovePlayer(NetworkConnection conn)
        {
            if (conn is not NetworkConnectionToClient connectionToClient) return;

            var player = connectionToClient.identity.gameObject;
            
            NetworkServer.RemovePlayerForConnection(conn, false);
            
            NetworkServer.Destroy(player);
        }
        
        private GameObject SpawnPlayer(NetworkConnection conn, GameObject prefab)
        {
            var room = RoomManagerBase.Singleton.GetRoomOfPlayer(conn);

            var scene = room?.Scene ?? gameObject.scene;
            
            var player = NetworkSpawnUtilities.SpawnObjectForScene(scene, prefab, Vector3.zero, Quaternion.identity);

            return player;
        }
    }

Last updated