quakeball/Assets/Scripts/Interface/Stopwatch.cs

39 lines
1.1 KiB
C#

using UnityEngine;
using UnityEngine.InputSystem;
using TMPro;
namespace NeonTea.Quakeball.Interface {
public class Stopwatch : MonoBehaviour {
public TMP_Text TimingText;
private InputAction ToggleTiming;
private float StartTime;
private bool CurrentlyTiming = false;
private void Awake() {
ToggleTiming = new InputAction("Toggle stopwatch", binding: "<Keyboard>/t");
ToggleTiming.performed += _ => {
if (CurrentlyTiming) {
UpdateTiming();
CurrentlyTiming = false;
} else {
StartTime = Time.fixedTime;
CurrentlyTiming = true;
}
};
ToggleTiming.Enable();
}
private void FixedUpdate() {
if (CurrentlyTiming) {
UpdateTiming();
}
}
private void UpdateTiming() {
float Duration = Time.fixedTime - StartTime;
TimingText.text = Duration.ToString("0.00") + " s";
}
}
}