You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
HighGroundRoyaleNetcode/Assets/StartingTimer.cs

76 lines
1.9 KiB
C#

using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using DG.Tweening;
using Unity.Netcode;
using Unity.Multiplayer.Samples.Utilities;
public class StartingTimer : NetworkBehaviour
{
public ServerAdditiveSceneLoader serverAdditiveSceneLoader;
private Image BlackScreen;
public TextMeshProUGUI TimerText;
private NetworkVariable<int> timerValue = new NetworkVariable<int>(0, NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Server);
private bool coroutineStartedBool = false;
private void Start()
{
BlackScreen = GetComponent<Image>();
}
private void Update()
{
if (serverAdditiveSceneLoader.m_SceneState == ServerAdditiveSceneLoader.SceneState.Loaded && coroutineStartedBool == false)
{
if (IsServer)
{
coroutineStartedBool = true;
StartCoroutine(ServerTimerCoroutine());
}
}
// Update the UI on all clients
UpdateTimerUI();
}
private IEnumerator ServerTimerCoroutine()
{
const int countdownStart = 3;
for (int i = countdownStart; i > 0; i--)
{
timerValue.Value = i;
yield return new WaitForSeconds(1);
}
timerValue.Value = -1; // Indicate "GO"
yield return new WaitForSeconds(0.5f);
// Notify clients to disable the black screen
BlackScreenAlphaDisablerClientRpc();
}
private void UpdateTimerUI()
{
if (timerValue.Value > 0)
{
TimerText.text = timerValue.Value.ToString();
}
else if (timerValue.Value == -1)
{
TimerText.text = "GO";
}
}
[ClientRpc]
private void BlackScreenAlphaDisablerClientRpc()
{
BlackScreen.DOFade(0, 0.25f).OnComplete(() =>
{
gameObject.SetActive(false);
});
}
}