using System; using JoeSiu.Common.Serialization.Runtime; using TMPro; using UnityEditor; using UnityEngine; namespace Common.Timer.Runtime.UI { public class TimerDebugText : MonoBehaviour { [SerializeField] private SerializableInterface _timer = new(); public TMP_Text text; public string prefix; public int decimalPlaces = 2; public ITimer Timer { get => _timer.Value; set => _timer.Value = value; } private void Reset() { text = GetComponent(); } private void OnValidate() { #if UNITY_EDITOR if (!EditorApplication.isPlaying && Selection.Contains(gameObject)) UpdateText(Timer); #endif } private void Update() { UpdateText(Timer); } public void UpdateText(ITimer timer) { if (!text) return; if (timer == null) return; var format = $"F{decimalPlaces}"; var info = string.IsNullOrEmpty(prefix) ? "" : $"{prefix}\n"; if (timer is Behaviour behaviour) info += $"enabled: {behaviour.enabled}\n"; info += $"active: {timer.IsActive}\n"; switch (timer) { // Check if timer implements IStopwatch case IStopwatch stopwatch: info += $"isTimeReached: {stopwatch.IsTimeReached}\n"; info += $"{Math.Round(stopwatch.CurrentTime, decimalPlaces).ToString(format)}s / " + $"{Math.Round(stopwatch.Duration, decimalPlaces).ToString(format)}s " + $"(remain: {Math.Round(stopwatch.RemainingTime, decimalPlaces).ToString(format)}s)\n"; break; // Fallback for base Timer class default: info += $"{Math.Round(timer.CurrentTime, decimalPlaces).ToString(format)}s\n"; break; } text.text = info; } } }