Пример #1
0
        /// <summary>
        /// Gets executed on the server every time a client has connected. And also on the client side when they themselves join.
        /// </summary>
        private void HandleClientConnected(ulong clientId)
        {
            if (clientId == NetworkManager.Singleton.LocalClientId) // We ourselves just joined
            {
                string playerName = MenuNavigator.MainMenu.PlayerNameText.text;
                Debug.Log("NETWORK: We (" + playerName + " / " + clientId + ") connected to the server");

                if (NetworkManager.Singleton.IsHost) // Host
                {
                    // Set data of host network player object
                    NetworkClient connectedClient = NetworkManager.Singleton.ConnectedClients[clientId];
                    connectedClient.PlayerObject.GetComponent <NetworkPlayer>().Init(new NetworkConnectionData(MenuNavigator.MainMenu.PlayerNameText.text, clientId));

                    // Host new game
                    MenuNavigator.Lobby.InitHostMultiplayerGame(playerName);
                    MenuNavigator.SwitchToLobbyScreen();
                }
                else // Client
                {
                    // Join existing game
                    MenuNavigator.Lobby.InitJoinMultiplayerGame();
                    MenuNavigator.SwitchToLobbyScreen();
                }
            }

            else // We are the server and someone joined
            {
                // Write connection data to the network player object
                NetworkClient         connectedClient = NetworkManager.Singleton.ConnectedClients[clientId];
                NetworkConnectionData connectionData  = ConnectionData[clientId];
                connectedClient.PlayerObject.GetComponent <NetworkPlayer>().Init(connectionData);

                MenuNavigator.Lobby.PlayerJoined(connectionData);

                Debug.Log("NETWORK: Client Connected\n" + connectionData.ToString());
            }
        }
Пример #2
0
        /// <summary>
        /// Gets executed on the server when a client wants to connect with the given connection data.
        /// </summary>
        private void HandleApprovalCheck(byte[] connectionData, ulong clientId, NetworkManager.ConnectionApprovedDelegate callback)
        {
            // Get connection data of connected client (not if we as the host just joined ourselves)
            NetworkConnectionData data = null;

            if (clientId != NetworkManager.Singleton.LocalClientId)
            {
                data          = (NetworkConnectionData)Deserialize(connectionData);
                data.ClientId = clientId;
            }

            bool approveConnection = true;

            callback(true, null, approveConnection, null, null);

            if (approveConnection && clientId != NetworkManager.Singleton.LocalClientId)
            {
                Debug.Log("NETWORK: Incoming client connection, Approval: " + approveConnection + "\n" + data.ToString());
                ConnectionData.Add(clientId, data);
            }
        }