unity實(shí)現(xiàn)延遲回調(diào)工具
一個(gè)實(shí)用的計(jì)時(shí)器,可以計(jì)時(shí)延遲調(diào)用和延遲重復(fù)次數(shù)調(diào)用。
可以自己封裝成單例模式掛在GameObject上使用,或者在另一個(gè)behavior的Update里執(zhí)行這個(gè)類的OnUpdate()方法再使用。
為了更加安全的使用,建議在銷毀MonoBehaviour時(shí)清理一下對(duì)應(yīng)的所有計(jì)時(shí)器。
或者調(diào)用時(shí)可選擇傳入回調(diào)所在的MonoBehaviour,這樣就可以自動(dòng)清理了。
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">延遲時(shí)間</param> /// <param name="repeat">重復(fù)回調(diào)次數(shù)</param> /// <param name="mn">承載回掉函數(shù)的實(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 { //重新累加時(shí)間 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; } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
深入多線程之:用Wait與Pulse模擬一些同步構(gòu)造的應(yīng)用詳解
本篇文章是對(duì)用Wait與Pulse模擬一些同步構(gòu)造的應(yīng)用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05C#網(wǎng)絡(luò)請(qǐng)求與JSON解析的示例代碼
這篇文章主要介紹了C#網(wǎng)絡(luò)請(qǐng)求與JSON解析的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03基于WPF實(shí)現(xiàn)簡(jiǎn)單的文件夾比較工具
文件比較平常都是用Beyond?Compare,可以說離不開的神器,不過Beyond?Compare平常拿它主要是用來做代碼比較,用來做一些大批量的二進(jìn)制文件比較,其實(shí)有點(diǎn)不是很方便,所以本文來用WPF做一個(gè)簡(jiǎn)單的文件夾比較的小工具2023-05-05利用C#實(shí)現(xiàn)分布式數(shù)據(jù)庫(kù)查詢
利用C#實(shí)現(xiàn)分布式數(shù)據(jù)庫(kù)查詢...2007-03-03Unity多語言轉(zhuǎn)換工具的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Unity多語言轉(zhuǎn)換工具的實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06C# Redis學(xué)習(xí)系列(一)Redis下載安裝使用
這篇文章主要為大家分享了C# Redis學(xué)習(xí)系列教程第一篇, Redis下載、安裝、使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05