您好,登錄后才能下訂單哦!
這篇文章主要介紹unity如何實現延遲回調工具,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
一個實用的計時器,可以計時延遲調用和延遲重復次數調用。
可以自己封裝成單例模式掛在GameObject上使用,或者在另一個behavior的Update里執行這個類的OnUpdate()方法再使用。
為了更加安全的使用,建議在銷毀MonoBehaviour時清理一下對應的所有計時器。
或者調用時可選擇傳入回調所在的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> /// 刪除延遲執行. /// </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> /// 添加延遲執行 /// </summary> /// <param name="call">回調方法</param> /// <param name="delayTime">延遲時間</param> /// <param name="repeat">重復回調次數</param> /// <param name="mn">承載回掉函數的實例是否存在的判斷</param> /// <param name="isUnique">是否是唯一的方法</param> /// <param name="isReplace">如果重復是否覆蓋</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; } }
以上是“unity如何實現延遲回調工具”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。