/// <summary> /// Update the UI when a player joins the game. /// </summary> private void NetworkManager_OnClientJoin(PlayerConnection player) { switch (GameManager.NetworkManager.AllClients.Count) { case 1: names[0] = player.PlayerInfo.Name; playerOneName.SetText(names[0]); playerOneName.SetText(names[0]); colors[0] = player.PlayerInfo.Color; playerOneColor.GetComponent <Image>().color = colors[0]; break; case 2: names[1] = player.PlayerInfo.Name; playerTwoName.SetText(names[1]); colors[1] = player.PlayerInfo.Color; playerTwoColor.GetComponent <Image>().color = colors[1]; break; case 3: names[2] = player.PlayerInfo.Name; playerThreeName.SetText(names[2]); colors[2] = player.PlayerInfo.Color; playerThreeColor.GetComponent <Image>().color = colors[2]; break; case 4: names[3] = player.PlayerInfo.Name; playerFourName.SetText(names[3]); colors[3] = player.PlayerInfo.Color; playerFourColor.GetComponent <Image>().color = colors[3]; break; } }
/// <summary> /// Unregisters a player connection. /// Called when a <see cref="PlayerConnection"/> object is destroyed. /// Usually happens when a player client disconnects from the server. /// Fires <see cref="OnClientLeave"/>. /// </summary> /// <param name="connection">The connection to unregister.</param> public void UnregisterClient(PlayerConnection connection) { if (this.AllClients.Contains(connection)) { this.AllClients.Remove(connection); this.OnClientLeave?.Invoke(connection); } if (this.isWaitingForClientsToLoad) { this.CheckIfClientsReady(); } }
/// <summary> /// Registers a new playerr connection for this network manager. /// Called when a new <see cref="PlayerConnection"/> object is spawned. /// Usually happens when a new player client connects to the server. /// Fires <see cref="OnClientJoin"/>. /// </summary> /// <param name="connection">The new connection to register.</param> public void RegisterClient(PlayerConnection connection) { connection.transform.SetParent(this.clientsContainer); if (connection.IsOwn) { this.ConnectionToServer = connection; } if (!this.AllClients.Contains(connection)) { this.AllClients.Add(connection); this.OnClientJoin?.Invoke(connection); } }
/// <summary> /// Sets the player client this player belongs to. /// Synchronizes the value with the clients. /// </summary> /// <param name="client">The owner client.</param> public void SetClient(PlayerConnection client) { this.Client = client; if (client != null) { this.playerNameText.text = client.PlayerInfo.Name; this.playerModelRenderer.material.color = client.PlayerInfo.Color; } if (this.isServer) { this.RpcSetClient(client != null ? client.GetComponent <NetworkIdentity>() : null); } }
private void AddPlayer(PlayerConnection client) { foreach (Player p in this.allPlayers) { if (p.Client == client) { return; } } Transform spawnPos = this.GetSpawnForPlayer(this.allPlayers.Count); GameObject playerGO = GameObject.Instantiate(GameManager.PlayerPrefab, spawnPos.position, spawnPos.rotation, this.transform); Player player = playerGO.GetComponent <Player>(); NetworkServer.Spawn(playerGO, client.connectionToClient); this.RegisterPlayer(player); player.SetClient(client); }
private void RemovePlayer(PlayerConnection client) { Player targetPlayer = null; foreach (Player p in this.allPlayers) { if (p.Client == client) { targetPlayer = p; break; } } if (targetPlayer != null) { this.UnregisterPlayer(targetPlayer); NetworkServer.Destroy(targetPlayer.gameObject); } }
public override void OnServerAddPlayer(NetworkConnection conn) { if (this.pendingConnections.ContainsKey(conn.connectionId)) { GameObject connectionGO = GameObject.Instantiate(this.playerPrefab, this.clientsContainer); PlayerConnection clientConnection = connectionGO.GetComponent <PlayerConnection>(); clientConnection.SetPlayerInfo(this.pendingConnections[conn.connectionId]); // Register server-side events for the connected client if (clientConnection != null) { clientConnection.OnLevelLoaded += this.PlayerConnection_OnLevelLoaded; } this.pendingConnections.Remove(conn.connectionId); NetworkServer.AddPlayerForConnection(conn, connectionGO); } else { conn.Disconnect(); // No player info was sent before requesting to add a player -> disconnect and let the client retry } }
/// <summary> /// Update the UI when a player leaves the game. /// </summary> private void NetworkManager_OnClientLeave(PlayerConnection player) { playerFourName.SetText(" "); playerFourColor.GetComponent <Image>().color = new Color(0f, 0f, 0f, 0f); if (player.PlayerInfo.Name == names[0]) //player one leaves { colors[0] = new Color(0f, 0f, 0f, 0f); for (int i = 3; i >= GameManager.NetworkManager.AllClients.Count; i--) { names[i + 1] = ""; colors[i + 1] = new Color(0f, 0f, 0f, 0f); } playerOneName.SetText(names[1]); playerOneColor.GetComponent <Image>().color = colors[1]; names[0] = names[1]; colors[0] = colors[1]; playerTwoName.SetText(names[2]); playerTwoColor.GetComponent <Image>().color = colors[2]; names[1] = names[2]; colors[1] = colors[2]; playerThreeName.SetText(names[3]); playerThreeColor.GetComponent <Image>().color = colors[3]; names[2] = names[3]; colors[2] = colors[3]; } else { if (player.PlayerInfo.Name == names[1]) //player two leaves { for (int i = 3; i >= GameManager.NetworkManager.AllClients.Count; i--) { names[i + 1] = ""; colors[i + 1] = new Color(0f, 0f, 0f, 0f); } playerTwoName.SetText(names[2]); playerTwoColor.GetComponent <Image>().color = colors[2]; names[1] = names[2]; colors[1] = colors[2]; playerThreeName.SetText(names[3]); playerThreeColor.GetComponent <Image>().color = colors[3]; names[2] = names[3]; colors[2] = colors[3]; } else { if (player.PlayerInfo.Name == names[2]) //player three leaves { names[2] = ""; colors[2] = new Color(0f, 0f, 0f, 0f); playerThreeName.SetText(names[3]); playerThreeColor.GetComponent <Image>().color = colors[3]; names[2] = names[3]; colors[2] = colors[3]; } } } names[3] = ""; colors[3] = new Color(0f, 0f, 0f, 0f); }
/// <summary> /// Called when a player client has finished loading a level. /// Calls <see cref="CheckIfClientsReady"/> to check if all clients are ready and the server can start the level. /// </summary> /// <param name="connection">The client that has finished loading a requested level.</param> private void PlayerConnection_OnLevelLoaded(PlayerConnection connection) => this.CheckIfClientsReady();