Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement deferred callback tool in unity

Shulou Source: shulou.com Published: 2022-06-03 09:53:26 10月20日 Update

This article mainly introduces how to implement the deferred callback tool in unity, which is very detailed and has certain reference value. Friends who are interested must finish reading it!

A practical timer that can delay calls and delay repeated calls.

You can encapsulate it as a singleton pattern and hang it on GameObject, or execute the OnUpdate () method of this class in the Update of another behavior.

For more secure use, it is recommended that you clean up all corresponding timers when destroying the MonoBehaviour.

Or when calling, you can choose to pass in the MonoBehaviour where the callback is located, so that it can be cleaned automatically.

Using System.Collections;using System;using System.Collections.Generic;using UnityEngine;public static class DelayCall {private static List calltimes = new List (); private static Dictionary callsort = new Dictionary (); private static int countid = 0; / generate id / The new identifier. / Call. Private static int getNewId (CallObj call) {countid++; if (countid > = int.MaxValue) {countid = 1;} while (callsort.ContainsKey (countid)) countid++; call.callid = countid; callsort.Add (countid, call); return countid;} public static void ClearAll () {calltimes.Clear (); callsort.Clear () } / deletion delayed execution. / Call. / / public static void remove (int callid) {if (callid > 0 & & callsort.ContainsKey (callid)) {CallObj call = callsort [callid]; callsort.Remove (callid); if (call! = null) {calltimes.Remove ((CallTimeObj) call) } public static int AddTime (float delayTime, object arg, int repeat = 1) {var callobj = new CallTimeObj (); callobj.argsCall = call; callobj.arg = arg; callobj.repeat = repeat; callobj.time = Time.realtimeSinceStartup + delayTime; callobj.delayTime = delayTime; if (repeat = = 0) {callobj.isloop = true } calltimes.Add (callobj); getNewId (callobj); return callobj.callid } / add delayed execution / callback method / delay time / repeated callback times / determine whether the instance of the callback function exists or not / / is the only way / if the repetition covers / 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 < calltimes.Count; iTunes +) {CallTimeObj call2 = calltimes [I]; if (call2.mn = = mn & & call2.call = = call) {if (isReplace) {call2.time = Time.realtimeSinceStartup + delayTime } return call2.callid;} var callobj = new CallTimeObj (); callobj.call = call; callobj.isMN = (mn! = null); callobj.mn = mn; callobj.repeat = repeat; callobj.time = Time.realtimeSinceStartup + delayTime; callobj.delayTime = delayTime If (repeat = = 0) {callobj.isloop = true;} calltimes.Add (callobj); getNewId (callobj); return callobj.callid;} public static void OnUpdate () {/ / time call if (calltimes.Count! = 0) for (int I = 0; I < calltimes.Count) + + I) {CallTimeObj call = calltimes [I]; if (call.time

Tags: Delays methods tools content time times articles timers utility security value interests functions examples guys buddies suggestions locations more patterns Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Redmi NVidia Xiaomi MariaDB