Documentation
Ask or search…
K
Links
Comment on page

End game popup

Implementing a simple end game logic: When any player reaches the maximum goal points, the game will display a result popup
Create a UI for the End Game Popup as follows.
In the PlayManager.cs script, create some variables:
[Header("Popup End Game")]
[SerializeField] private GameObject _popupEndGame; // GameObject of Popup End Game content.
[SerializeField] private Transform _listPlayerEndGameTransform;
[SerializeField] public bool _isEndGame = false; // true when game has end.
[SerializeField] public int _pointEndGame; // End game when one of player reach this point.
[SerializeField] private TextMeshProUGUI[] listEndGamePlayerNameText;
// When Start(), disable popup end game.
void Start()
{
_popupEndGame.SetActive(false);
_isEndGame = false;
// ...
In the NetCodeThirdPersonController.cs script, add logic to listen for changes in the Point value and check if the points have reached the maximum goal. If the points reach the maximum, call a ServerRpc to notify that one of the players has reached the maximum point.
In the ServerRpc, broadcast this information to all clients, letting them know that one of the players has reached the maximum point and show the end game popup.
public override void OnNetworkSpawn()
{
base.OnNetworkSpawn();
...
point.OnValueChanged += OnPointChange; // Dont forget remove OnValueChanged in OnNetworkDespawn.
...
}
/* Catch event when this point has changed */
public void OnPointChange(int pre, int current)
{
if (!IsOwner) return;
if (current >= PlayManager.Instance._pointEndGame)
{
Debug.Log($"$This Player {PlayerName} has reach End Game Point. Show Endgame now...");
PlayManager.Instance.ShowPopupEndGameServerRpc();
}
}
private void Update()
{
// stop every actions when isEndGame = true
if (PlayManager.Instance._isEndGame) return;
...
}
In the PlayManager.cs script, create a ServerRpc function to check and display the End Game Popup:
[ServerRpc(RequireOwnership = false)]
public void ShowPopupEndGameServerRpc(ServerRpcParams serverRpcParams = default)
{
Debug.Log("== ShowPopupEndGameServerRpc Trigged. Broadcast event end game to all clients");
ShowPopupEndGameClientRpc();
}
[ClientRpc]
public void ShowPopupEndGameClientRpc()
{
_isEndGame = true;
_popupEndGame.SetActive(true);
/* Set active false for all item list player */
foreach (Transform child in _listPlayerEndGameTransform)
{
child.gameObject.SetActive(false);
}
/* Fill list player data and score to table list player */
int i = 0;
foreach (KeyValuePair<ulong, NetCodeThirdPersonController> player in PlayersList)
{
// Debug.LogWarning($"= Client ID {player.Key} has Name {player.Value.PlayerName}");
if (i <= listEndGamePlayerNameText.Length)
{
listEndGamePlayerNameText[i].text = string.Format("#{0}: {3} - {1} - ID : {2} - P : {4}", i + 1, player.Value.PlayerName, player.Key.ToString(), player.Value.TypeInGame.ToString(), player.Value.Point);
listEndGamePlayerNameText[i].gameObject.SetActive(true);
if (player.Value.Point >= _pointEndGame)
{
listEndGamePlayerNameText[i].fontSize = listEndGamePlayerNameText[i].fontSize + 15;
listEndGamePlayerNameText[i].fontStyle = FontStyles.Bold;
}
i++;
}
}
}