Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement deferred callback tool in unity

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report