timer stopwatch
hi i was just wandering how i would get an accurate timer to seconds?
it has to have milliseconds seconds minutes and hours, but time.time cant be used as it needs to reset sometimes back to zero. invokerepeating is good for it if i dont use milliseconds,
You can use Time.deltaTime in the Update function to calculate the time that has passed since the last frame. You can then add this to the value of a variable and display the time using a gui element. Here is an example which will display $$anonymous$$utes and seconds but hours and milliseconds could also be added:
var sec : float = 0;
var intsec : int;
var $$anonymous$$utes = 0;
var seconds;
var time;
function Update() {
sec = sec + Time.deltaTime;
intsec = sec;
if(intsec < 10)
{
seconds = "0"+intsec;
}
else
{
seconds = intsec;
}
if(intsec >= 60)
{
$$anonymous$$utes++;
sec=0;
}
time = $$anonymous$$utes + ":" + seconds;
}
function OnGUI() {
GUI.Label (Rect (10, 10, 100, 20), time);
}
it needs to be in a fixed update, well it doesnt it just makes my life simpler when pausing and stuff, ill give this a go in the morning when i can think clearly.
Yeah It makes more sense to put the rest of the code into the update and just leave the GUI.Label in the OnGUI() function, but it will work the same like this since the sec variable is only being updated in the Update() function. I'll change it though.. thanks for pointing that out :)
I will note im using ngui as its fir iphone, and i really like the one draw call feature without manually doing it all my self. I tend to stay away from ongui as i have heard bad things about it. Tend to use rays or hittest on gui.
If this is for real ti$$anonymous$$g vs. game ti$$anonymous$$g, take a look at the Stopwatch class:
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch(v=vs.110).aspx
Answer by freezing_ · Mar 04, 2014 at 05:46 PM
using System.Diagnostics;
public Stopwatch timer;
//ctr
timer = new StopWatch();
timer.Start();
// delay
timer.End();
use timer.Elapsed ( its a TimeSpan instance)
if you use debug in the same class it will interfere with the Debug in class in c# so just use it a class you do not Debug :)
Or you can clarify the Debug class you want to use thus:
using System.Diagnostics;
using Debug=UnityEngine.Debug;
I'm having trouble with the stopwatch class in system.diagnostics. it isn't working on builds But works in the editor are you having this issue at all?
System.Diagnostics doesn't get included in a final build, and IIRC you need to change from .NET 2.0 Subset to the full .NET 2.0 runtime.
i believe you are correct setting to .net did seem to work even in builds
The System Diagnostics timer may cause problems if you want to target stuff other than windows. (That may change with .net core... depending on the target.)
Answer by Danief_ · Jun 06, 2019 at 06:02 PM
If anybody else comes across this and doesn't want to use the diagnostics one, here's a custom class I wrote.
//Written by Daniel Keele - 6/6/2019
using System;
using UnityEngine;
public class Stopwatch : MonoBehaviour
{
private float elapsedRunningTime = 0f;
private float runningStartTime = 0f;
private float pauseStartTime = 0f;
private float elapsedPausedTime = 0f;
private float totalElapsedPausedTime = 0f;
private bool running = false;
private bool paused = false;
void Update()
{
if (running)
{
elapsedRunningTime = Time.time - runningStartTime - totalElapsedPausedTime;
}
else if (paused)
{
elapsedPausedTime = Time.time - pauseStartTime;
}
}
public void Begin()
{
if (!running && !paused)
{
runningStartTime = Time.time;
running = true;
}
}
public void Pause()
{
if (running && !paused)
{
running = false;
pauseStartTime = Time.time;
paused = true;
}
}
public void Unpause()
{
if (!running && paused)
{
totalElapsedPausedTime += elapsedPausedTime;
running = true;
paused = false;
}
}
public void Reset()
{
elapsedRunningTime = 0f;
runningStartTime = 0f;
pauseStartTime = 0f;
elapsedPausedTime = 0f;
totalElapsedPausedTime = 0f;
running = false;
paused = false;
}
public int GetMinutes()
{
return (int)(elapsedRunningTime / 60f);
}
public int GetSeconds()
{
return (int)(elapsedRunningTime);
}
public float GetMilliseconds()
{
return (float)(elapsedRunningTime - System.Math.Truncate(elapsedRunningTime));
}
public float GetRawElapsedTime()
{
return elapsedRunningTime;
}
}
Note on limitation of this answer: This won't work for anyone needing times updated between FixedUpdate and you can't just change Update to FixedUpdate, cuz Time.time represents the time of the start of the frame. Also, due to Time.time being a float, the resolution of Time.time is only accurate to the nearest second for 97 days. $$anonymous$$illiseconds, as the OP asks for, will be lost even sooner.