unity實現(xiàn)延遲回調(diào)工具
更新時間:2021年09月26日 10:10:39 作者:騷年狠沖洞
這篇文章主要為大家詳細(xì)介紹了unity實現(xiàn)延遲回調(diào)工具,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
一個實用的計時器,可以計時延遲調(diào)用和延遲重復(fù)次數(shù)調(diào)用。
可以自己封裝成單例模式掛在GameObject上使用,或者在另一個behavior的Update里執(zhí)行這個類的OnUpdate()方法再使用。
為了更加安全的使用,建議在銷毀MonoBehaviour時清理一下對應(yīng)的所有計時器。
或者調(diào)用時可選擇傳入回調(diào)所在的MonoBehaviour,這樣就可以自動清理了。
using System.Collections; using System; using System.Collections.Generic; 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> /// 生成id /// </summary> /// <returns>The new identifier.</returns> /// <param name="call">Call.</param> 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(); } /// <summary> /// 刪除延遲執(zhí)行. /// </summary> /// <param name='call'> /// Call. /// </param> 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,Action<object> call) { 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; } /// <summary> /// 添加延遲執(zhí)行 /// </summary> /// <param name="call">回調(diào)方法</param> /// <param name="delayTime">延遲時間</param> /// <param name="repeat">重復(fù)回調(diào)次數(shù)</param> /// <param name="mn">承載回掉函數(shù)的實例是否存在的判斷</param> /// <param name="isUnique">是否是唯一的方法</param> /// <param name="isReplace">如果重復(fù)是否覆蓋</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 < calltimes.Count; i++) { 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 <= Time.realtimeSinceStartup) { if (call.isloop == false) { call.repeat--; if (call.repeat <= 0) { calltimes.RemoveAt(i); callsort.Remove(call.callid); --i; } else { //重新累加時間 call.time += call.delayTime; } } else { call.time += call.delayTime; } if (!call.isMN || call.mn != null) { try { if (call.argsCall != null) { call.argsCall.Invoke(call.arg); if (call.isloop == false) { if (call.repeat <= 0) { call.arg = null; call.argsCall = null; call.mn = null; } } } else { call.call(); } } catch (Exception e) { Debug.LogException(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; } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
深入多線程之:用Wait與Pulse模擬一些同步構(gòu)造的應(yīng)用詳解
本篇文章是對用Wait與Pulse模擬一些同步構(gòu)造的應(yīng)用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05C# Redis學(xué)習(xí)系列(一)Redis下載安裝使用
這篇文章主要為大家分享了C# Redis學(xué)習(xí)系列教程第一篇, Redis下載、安裝、使用,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05