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.
90 lines
2.3 KiB
C#
90 lines
2.3 KiB
C#
3 weeks ago
|
using System;
|
||
|
using Unity.Netcode;
|
||
|
using TMPro;
|
||
|
using UnityEngine;
|
||
|
|
||
|
public class SynchronizedTimer : NetworkBehaviour
|
||
|
{
|
||
|
public event Action OnTimerEnd; // Ensure it's public
|
||
|
|
||
|
[SerializeField] private TextMeshProUGUI timerText; // Reference to the TextMeshProUGUI component
|
||
|
|
||
|
private NetworkVariable<float> timer = new NetworkVariable<float>(0f, NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Server);
|
||
|
private bool isTimerRunning = false;
|
||
|
|
||
|
void Update()
|
||
|
{
|
||
|
if (IsServer && isTimerRunning)
|
||
|
{
|
||
|
// Decrease the timer only on the server
|
||
|
timer.Value -= Time.deltaTime;
|
||
|
|
||
|
if (timer.Value <= 0)
|
||
|
{
|
||
|
timer.Value = 0;
|
||
|
isTimerRunning = false;
|
||
|
OnTimerEnd?.Invoke(); // Trigger the event on the server
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Update the UI on all clients
|
||
|
UpdateTimerUI();
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Starts the timer.
|
||
|
/// </summary>
|
||
|
/// <param name="time">The duration of the timer in seconds.</param>
|
||
|
[ServerRpc(RequireOwnership = false)]
|
||
|
public void StartTimerServerRpc(float time)
|
||
|
{
|
||
|
if (!isTimerRunning)
|
||
|
{
|
||
|
timer.Value = time;
|
||
|
isTimerRunning = true;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Gets the remaining time on the timer.
|
||
|
/// </summary>
|
||
|
/// <returns>The remaining time in seconds.</returns>
|
||
|
public float GetRemainingTime()
|
||
|
{
|
||
|
return timer.Value;
|
||
|
}
|
||
|
|
||
|
// This method ensures the timer stays synchronized for clients
|
||
|
private void OnEnable()
|
||
|
{
|
||
|
timer.OnValueChanged += OnTimerValueChanged;
|
||
|
}
|
||
|
|
||
|
private void OnDisable()
|
||
|
{
|
||
|
timer.OnValueChanged -= OnTimerValueChanged;
|
||
|
}
|
||
|
|
||
|
private void OnTimerValueChanged(float oldValue, float newValue)
|
||
|
{
|
||
|
if (!IsServer && newValue <= 0 && oldValue > 0)
|
||
|
{
|
||
|
OnTimerEnd?.Invoke(); // Trigger the event on clients when timer reaches zero
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Updates the TextMeshProUGUI with the remaining time.
|
||
|
/// </summary>
|
||
|
private void UpdateTimerUI()
|
||
|
{
|
||
|
if (timerText != null)
|
||
|
{
|
||
|
int minutes = Mathf.FloorToInt(timer.Value / 60);
|
||
|
int seconds = Mathf.FloorToInt(timer.Value % 60);
|
||
|
|
||
|
timerText.text = $"{minutes:D2}:{seconds:D2}";
|
||
|
}
|
||
|
}
|
||
|
}
|