I built a timer mm:ss:ms - do you see any optimizations that could be added?
I've built a timer, scraped from a bunch of others that I looked over but didn't seem to do exactly what I wanted.. which was pretty simple. Does anyone see any issues or ways to optimize?
- using TextMeshPro for UI.
using UnityEngine;
using TMPro;
public class Timer : MonoBehaviour
{
[SerializeField]
private TextMeshProUGUI timerUILabel; //drag UI Text object here via Inspector
private float t_offset = 0f; // set to nothing, can be set to an offset if needed.
private int t_minutes;
private int t_seconds;
private int t_milliseconds;
private void Update()
{
float t = Time.time - t_offset;
//Debug.Log("currentTime in seconds = " + t);
t_minutes = ((int)t / 60); // t(seconds) / 60 = total minutes
t_seconds = ((int)t % 60); // t(seconds) % 60 = remaining seconds
t_milliseconds = ((int)(t * 100)) % 100; // (total seconds * 1000) % 1000 = remaining milliseconds
//display the text in a 00:00:00 format
timerUILabel.text = string.Format("{0:00}:{1:00}:{2:00}", t_minutes, t_seconds, t_milliseconds);
}
}
Comment
Your answer
Follow this Question
Related Questions
I need my timer to only start after it hits a trigger. Please help! 0 Answers
Load scene after time 1 Answer
How to make an App that only works for a week? 1 Answer
Clock doesn't stop when reaches 0.0. Can you help me with this. 1 Answer
How is it possible to verify values and is they are correct activate objects ? 0 Answers