How can I use TimeSpan inside OnGUI to show a simple countdown timer with hours minutes seconds and milliseconds?
Asked Answered
K

1

0
void OnGUI()
{
    TimeSpan timer = new TimeSpan(1, 1, 50);
    TimeSpan deltaTimeSpan = TimeSpan.FromSeconds(Time.deltaTime);
    timer = timer.Subtract(deltaTimeSpan);
    string t = "Time left: " + timer.ToString("hh\\:mm\\:ss");
    EditorGUILayout.LabelField("Next: ", t);
}

The problem is that ToString have not arguments. I’m getting error on ToString:

No overload for method ‘ToString’ takes 1 arguments

What I want to do is to display a simple countdown timer with hours minutes seconds milliseconds on a LabelField. But can’t do it.

Kirkwall answered 16/1 at 10:54 Comment(0)
D
0

@Kirkwall
If I remember correctly it is because you do not have the input for the ToString method in a correct format. I think it should be like this:

void OnGUI()
{
    TimeSpan timer = new TimeSpan(1, 1, 50);
    TimeSpan deltaTimeSpan = TimeSpan.FromSeconds(Time.deltaTime);
    timer = timer.Subtract(deltaTimeSpan);
      
    //I added @ in front of the quotation marks in ToString 
    string t = "Time left: " + timer.ToString(@"hh\\:mm\\:ss");
      
    EditorGUILayout.LabelField("Next: ", t);
}
Diligent answered 16/1 at 10:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.