我是靠谱客的博主 强健汉堡,这篇文章主要介绍unity实现延迟回调工具,现在分享给大家,希望可以做个参考。

一个实用的计时器,可以计时延迟调用和延迟重复次数调用。

可以自己封装成单例模式挂在GameObject上使用,或者在另一个behavior的Update里执行这个类的OnUpdate()方法再使用。

为了更加安全的使用,建议在销毁MonoBehaviour时清理一下对应的所有计时器。

或者调用时可选择传入回调所在的MonoBehaviour,这样就可以自动清理了。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
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实现延迟回调工具的全部内容,更多相关unity实现延迟回调工具内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(96)

评论列表共有 0 条评论

立即
投稿
返回
顶部