Stopwatch in Unity

public class StopWatch: MonoBehaviour
{
    public float initialTime;
    public Text textBox;
    public Text startBtnText;
    bool isTimerActive = false;
    void Start()
    {
        initialTime = 0f;
        textBox.text = initialTime.ToString("F2");
    }
    void Update()
    {
        if (isTimerActive == true)
        {
            initialTime += Time.deltaTime;
            textBox.text = initialTime.ToString("F2");
        }
    }
    public void ActivateTimer()
    {
        isTimerActive = !isTimerActive;
        startBtnText.text = isTimerActive ? "STOP" : "START";
    }
    public void ResetTimer()
    {
        initialTime = 0f;
        textBox.text = initialTime.ToString("F2");
        isTimerActive = false;
        startBtnText.text = "START";
    }
}

Dodaj komentarz

Twój adres email nie zostanie opublikowany.