Unity has a lot of timing functions.
There are several ways to implement:
Manual timing:
float persistTime = 10f
float startTime = Time.time;
if(Time.time - startTime > persistTime)
{
Debug.Log("Time out");
}
float curTime = 0;
curTime += Time.deltaTime;
if(curTime > persistTime)
{
Debug.Log("Time out");
}
Coroutines:
private float persistTime = 10f;
IEnumerator DelayFunc()
{
yield return persistTime;
Debug.Log("Time out");
}
private void Start()
{
StartCoroutine(DelayFunc());
}
Invoke callback:
private void Start()
{
Invoke("DelayFunc", persistTime);
}
Timer: timer pause, timer reset, timer start method, timer method, timer end method, fixed interval call method. The timer can be set to be multiplexed or single.
Timer Manager (TimerMa): Its function is responsible for counting down and executing timer methods.
using System;
using System.Collections.Generic;
using UnityEngine;
using Object = System.Object;
public class Timer
{
public delegate void IntervalAct(Object args);
//Total time and current duration
private float curtime = 0;
private float totalTime = 0;
//activation
public bool isActive;
//Whether the timer is destroyed
public bool isDestroy;
//whether to pause
public bool isPause;
//Interval events and interval events - Dot
private float intervalTime = 0;
private float curInterval = 0;
private IntervalAct onInterval;
private Object args;
// enter event
public Action onEnter;
private bool isOnEnter = false;
// persistent event
public Action onStay;
// exit event
public Action onEnd;
public Timer(float totalTime, bool isDestroy = true, bool isPause = false)
{
curtime = 0;
this.totalTime = totalTime;
isActive = true;
this.isDestroy = isDestroy;
this.isPause = isPause;
TimerMa.I.AddTimer(this);
}
public void Run()
{
//pause timing
if (isPause || !isActive)
return;
if (onEnter != null)
{
if (!isOnEnter)
{
isOnEnter = true;
onEnter();
}
}
// persistent event
if (onStay != null)
onStay();
curtime += Time.deltaTime;
//interval event
if (onInterval != null)
{
curInterval += Time.deltaTime;
if (curInterval > intervalTime)
{
onInterval(args);
curInterval = 0;
}
}
// timer ends
if (curtime > totalTime)
{
curtime = 0;
isActive = false;
if (onEnd != null)
{
onEnd();
}
}
}
// set interval event
public void SetInterval(float interval, IntervalAct intervalFunc, Object args = null)
{
this.intervalTime = interval;
onInterval = intervalFunc;
curInterval = 0;
this.args = args;
}
// reset the timer
public void Reset()
{
curtime = 0;
isActive = true;
isPause = false;
curInterval = 0;
isOnEnter = false;
}
//get the remaining time
public float GetRemainTime()
{
return totalTime - curtime;
}
}
Timing Manager Code:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TimerMa : MonoBehaviour
{
#region singleton
private static TimerMa instance;
TimerMa() {}
public static TimerMa I
{
get
{
if (instance == null)
instance = new TimerMa();
return instance;
}
}
#endregion
private List<Timer> timerList;
private void Awake()
{
instance = this;
timerList = new List<Timer>();
}
public void AddTimer(Timer t)
{
timerList.Add(t);
}
void Update()
{
for (int i = 0; i < timerList.Count;)
{
timerList[i].Run();
//The timer is over and needs to be destroyed
if(!timerList[i].isActive && timerList[i].isDestroy)
timerList.RemoveAt(i);
else
++i;
}
}
}
Test timer:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Object = System.Object;
public class Test : MonoBehaviour
{
public Text mText1;
public Text mText2;
private Timer timer;
private int count = 0;
void Start()
{
timer = new Timer(5f,false);
timer.SetInterval(1f, OnInterval);
timer.onEnter = OnStart;
timer.onEnd = OnExit;
}
void Update()
{
Debug.Log(count);
mText1.text = timer.GetRemainTime().ToString("f2");
if (Input.GetKeyDown(KeyCode.A))
{
if (!timer.isPause)
{
timer.isPause = true;
mText2.text = "Pause timer";
}
}
if (Input.GetKeyDown(KeyCode.S))
{
if (timer.isPause)
{
timer.isPause = false;
mText2.text = "Cancel Pause Timer";
}
}
if (Input.GetKeyDown(KeyCode.D))
{
timer.Reset();
mText2.text = "Reset timer";
}
}
private void OnStart()
{
mText2.text = "Start timing";
}
private void OnExit()
{
mText2.text = "End timer";
}
private void OnInterval(Object value)
{
count++;
mText2.text = $"Interval event call {count}";
}
}