public void JoinGame() { NetworkConnectionData connectionData = new NetworkConnectionData(MenuNavigator.MainMenu.PlayerNameText.text); NetworkManager.Singleton.NetworkConfig.ConnectionData = Serialize(connectionData); NetworkManager.Singleton.StartClient(); }
/// <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); } }
/// <summary> /// Gets called on the server when a player joins /// </summary> public void PlayerJoined(NetworkConnectionData connectionData) { Color playerColor = PartyNameGenerator.GetPartyColor(connectionData.Name, UsedColors); UsedColors.Add(playerColor); FillNextFreeSlot(connectionData.Name, playerColor, LobbySlotType.Human, connectionData.ClientId); OrganizeSlots(); // Send lobby data (slots and rules) to newly connected client if (Type == GameType.MultiplayerHost) { NetworkPlayer.Server.UpdateLobbySlotsServerRpc(); for (int i = 0; i < Rules.Count; i++) { if (Rules[i].value != 0) { NetworkPlayer.Server.LobbyRuleChangedServerRpc(i, Rules[i].value); } } } }
/// <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()); } }
public void Init(NetworkConnectionData connectionData) { ClientId = connectionData.ClientId; Name = connectionData.Name; }