A practical timer that can time delay calls and delay repetition calls.
You can encapsulate it into a singleton mode and hang it on the GameObject for use, or execute the OnUpdate() method of this class in the Update of another behavior and then use it.
For safer use, it is recommended to clean up all corresponding timers when destroying MonoBehaviour.
Or when calling, you can choose to pass the MonoBehaviour where the incoming callback is located, so that it can be cleaned up automatically.
using ; using System; using ; using UnityEngine; public static class DelayCall { private static List<CallTimeObj> calltimes = new List<CallTimeObj>(); private static Dictionary<int, CallObj> callsort = new Dictionary<int, CallObj>(); private static int countid = 0; /// <summary> /// Generate id /// </summary> /// <returns>The new identifier.</returns> /// <param name="call">Call.</param> private static int getNewId(CallObj call) { countid++; if (countid >= ) { countid = 1; } while ((countid)) countid++; = countid; (countid, call); return countid; } public static void ClearAll() { (); (); } /// <summary> /// Delete delayed execution. /// </summary> /// <param name='call'> /// Call. /// </param> public static void remove(int callid) { if (callid > 0 && (callid)) { CallObj call = callsort[callid]; (callid); if (call != null) { ((CallTimeObj)call); } } } public static int AddTime(float delayTime, object arg, int repeat = 1,Action<object> call) { var callobj = new CallTimeObj(); = call; = arg; = repeat; = + delayTime; = delayTime; if (repeat == 0) { = true; } (callobj); getNewId(callobj); return ; } /// <summary> /// Add delayed execution /// </summary> /// <param name="call">Callback method</param> /// <param name="delayTime">Delay time</param> /// <param name="repeat">Repeat callbacks</param> /// <param name="mn">Judgement on whether the instance that bears the function back exists</param> /// <param name="isUnique">Is it the only way</param> /// <param name="isReplace">If duplicate does it overwrite</param> /// <returns></returns> public static int AddTime(float delayTime, int repeat = 1, MonoBehaviour mn = null, bool isUnique = false, bool isReplace = false,Action call) { if (isUnique) { for (int i = 0; i < ; i++) { CallTimeObj call2 = calltimes[i]; if ( == mn && == call) { if (isReplace) { = + delayTime; } return ; } } } var callobj = new CallTimeObj(); = call; = (mn != null); = mn; = repeat; = + delayTime; = delayTime; if (repeat == 0) { = true; } (callobj); getNewId(callobj); return ; } public static void OnUpdate() { //time call if ( != 0) for (int i = 0; i < ; ++i) { CallTimeObj call = calltimes[i]; if ( <= ) { if ( == false) { --; if ( <= 0) { (i); (); --i; } else { //Re-accumulate time += ; } } else { += ; } if (! || != null) { try { if ( != null) { (); if ( == false) { if ( <= 0) { = null; = null; = null; } } } else { (); } } catch (Exception e) { (e); } } } } } private class CallObj { public Action call = null; public int frame; public bool isMN; public MonoBehaviour mn; public int callid = 0; } private class CallTimeObj : CallObj { public Action<object> argsCall = null; public float time; public int repeat = 1; public float delayTime = 0; public object arg; public bool isloop = false; } }
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.