SoFunction
Updated on 2025-03-07

Unity encapsulated delay call timer

This article shares the specific code of Unity encapsulated delay call timer for your reference. The specific content is as follows

Encapsulate a delay call timer class

using ;
using ;
using UnityEngine;
using ;

public class WaitTimeManager 
{
 private static TaskBehaviour m_Task;
 static WaitTimeManager()
 {
 GameObject go = new GameObject("#WaitTimeManager#");
 (go);
 m_Task = <TaskBehaviour> ();
 }

 //wait static public Coroutine WaitTime(float time,UnityAction callback)
 {
 return m_Task.StartCoroutine(Coroutine(time,callback));
 }
 
 //Cancel waiting static public void CancelWait(ref Coroutine coroutine)
 {
 if (coroutine != null) {
 m_Task.StopCoroutine(coroutine);
 coroutine = null;
 }
 }

 static IEnumerator Coroutine(float time, UnityAction callback) {
 yield return new WaitForSeconds (time);
 if (callback != null) {
 callback();
 }
 }
 
 //Internal class class TaskBehaviour : MonoBehaviour { }
}

test

using ;
using ;
using UnityEngine;

public class Script_04_15 : MonoBehaviour {

 void Start () {
 //Open the timer Coroutine coroutine = (5f, delegate {
 ("Callback after waiting for 5 seconds");
 });

 //Cancel it while waiting // (ref coroutine);
 }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.