-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateLoop.cs
More file actions
531 lines (470 loc) · 16.5 KB
/
UpdateLoop.cs
File metadata and controls
531 lines (470 loc) · 16.5 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.LowLevel;
using UnityEngine.PlayerLoop;
using UnityPlayerLoop = UnityEngine.LowLevel.PlayerLoop;
namespace UnityEssentials.PlayerLoop
{
/// <summary>
/// Utility class for adding subsystems to the game loop.
/// </summary>
public static class UpdateLoop
{
public enum Position
{
Before,
After
}
public struct UpdateLoopPreUpdateEvent { }
public struct UpdateLoopUpdateEvent { }
public struct UpdateLoopPreLateUpdateEvent { }
public struct UpdateLoopLateUpdateEvent { }
public struct UpdateLoopPostLateUpdateEvent { }
public struct UpdateLoopFixedUpdateEvent { }
public struct UpdateLoopLateFixedUpdateEvent { }
public struct UpdateLoopPostFixedUpdateEvent { }
internal class InvocationList
{
public class InvocationTarget
{
public readonly Action action;
public readonly Behaviour targetComponent;
public readonly bool isComponentTarget;
public bool ComponentWasDestroyed => isComponentTarget && targetComponent == null;
public InvocationTarget(Action action)
{
this.action = action;
targetComponent = action.Target as Behaviour;
isComponentTarget = targetComponent != null;
}
public bool IsActiveAndEnabled()
{
if(isComponentTarget) return targetComponent != null && targetComponent.isActiveAndEnabled;
else return true;
}
}
public readonly string name;
public readonly List<InvocationTarget> subscribers = new List<InvocationTarget>(256);
public InvocationList(string name)
{
this.name = name;
}
public void Add(Action action)
{
subscribers.Add(new InvocationTarget(action));
}
public void Remove(Action action)
{
for(int i = 0; i < subscribers.Count; i++)
{
if(subscribers[i]?.action == action)
{
subscribers.RemoveAt(i);
return;
}
}
}
public void RemoveAll(bool? componentActiveState = null)
{
if(componentActiveState == true) subscribers.RemoveAll(t => t.IsActiveAndEnabled());
else if(componentActiveState == false) subscribers.RemoveAll(t => !t.IsActiveAndEnabled());
else subscribers.Clear();
}
public void EnumerateSubscribers(List<InvocationTarget> cache, bool activeComponentsOnly = true)
{
cache.Clear();
for(int i = 0; i < subscribers.Count; i++)
{
var sub = subscribers[i];
if(sub == null || sub.ComponentWasDestroyed)
{
if(Application.isPlaying) Debug.LogWarning($"Destroyed subscriber detected in {name}. Make sure to unsubscribe from the Update Loop when the object is destroyed.");
subscribers.RemoveAt(i);
i--;
}
else
{
if(activeComponentsOnly && !sub.IsActiveAndEnabled()) continue;
cache.Add(sub);
}
}
}
}
/// <summary>
/// Event that is invoked before the regular Update period.
/// </summary>
public static event Action PreUpdate
{
add => preUpdate.Add(value);
remove => preUpdate.Remove(value);
}
/// <summary>
/// Event that is invoked during the regular Update period.
/// </summary>
public static event Action Update
{
add => update.Add(value);
remove => update.Remove(value);
}
/// <summary>
/// Event that is invoked after the Update and before the LateUpdate period.
/// </summary>
public static event Action PreLateUpdate
{
add => preLateUpdate.Add(value);
remove => preLateUpdate.Remove(value);
}
/// <summary>
/// Event that is invoked during the regular LateUpdate period.
/// </summary>
public static event Action LateUpdate
{
add => lateUpdate.Add(value);
remove => lateUpdate.Remove(value);
}
/// <summary>
/// Event that is invoked after the regular LateUpdate period.
/// </summary>
public static event Action PostLateUpdate
{
add => postLateUpdate.Add(value);
remove => postLateUpdate.Remove(value);
}
/// <summary>
/// Event that is invoked during the regular FixedUpdate period.
/// </summary>
public static event Action FixedUpdate
{
add => fixedUpdate.Add(value);
remove => fixedUpdate.Remove(value);
}
/// <summary>
/// Event that is invoked during the regular FixedUpdate period.
/// </summary>
public static event Action LateFixedUpdate
{
add => lateFixedUpdate.Add(value);
remove => lateFixedUpdate.Remove(value);
}
/// <summary>
/// Event that is invoked after the regular FixedUpdate period.
/// </summary>
public static event Action PostFixedUpdate
{
add => postFixedUpdate.Add(value);
remove => postFixedUpdate.Remove(value);
}
/// <summary>
/// Event that is invoked only once for each subscriber during the regular Update event.
/// </summary>
public static event Action UpdateOnce
{
add => updateOnce.Add(value);
remove => updateOnce.Remove(value);
}
/// <summary>
/// Event that is invoked only once for each subscriber during the fixed Update event.
/// </summary>
public static event Action FixedUpdateOnce
{
add => fixedUpdateOnce.Add(value);
remove => fixedUpdateOnce.Remove(value);
}
/// <summary>
/// Event that is invoked during the OnGUI period.
/// </summary>
public static event Action OnGUI
{
add
{
onGUI.Add(value);
UpdateLoopScriptInstance.InitIfRequired();
}
remove => onGUI.Remove(value);
}
/// <summary>
/// Event that is used to draw Gizmos in the scene view (runtime only).
/// </summary>
public static event Action OnDrawGizmosRuntime
{
add
{
onDrawGizmosRuntime.Add(value);
UpdateLoopScriptInstance.InitIfRequired();
}
remove => onDrawGizmosRuntime.Remove(value);
}
private static readonly InvocationList preUpdate = new InvocationList("PreUpdate");
private static readonly InvocationList update = new InvocationList("Update");
private static readonly InvocationList preLateUpdate = new InvocationList("PreLateUpdate");
private static readonly InvocationList lateUpdate = new InvocationList("LateUpdate");
private static readonly InvocationList postLateUpdate = new InvocationList("PostLateUpdate");
private static readonly InvocationList fixedUpdate = new InvocationList("FixedUpdate");
private static readonly InvocationList lateFixedUpdate = new InvocationList("LateFixedUpdate");
private static readonly InvocationList postFixedUpdate = new InvocationList("PostFixedUpdate");
private static readonly InvocationList updateOnce = new InvocationList("UpdateOnce");
private static readonly InvocationList fixedUpdateOnce = new InvocationList("FixedUpdateOnce");
private static readonly InvocationList onGUI = new InvocationList("OnGUI");
private static readonly InvocationList onDrawGizmosRuntime = new InvocationList("OnDrawGizmosRuntime");
private static readonly List<InvocationList.InvocationTarget> enumerationCache = new List<InvocationList.InvocationTarget>();
private static bool IsEditorPaused
{
get
{
#if UNITY_EDITOR
return !UnityEditor.EditorApplication.isPlaying || UnityEditor.EditorApplication.isPaused;
#else
return false;
#endif
}
}
private static void Invoke(InvocationList eventHandler)
{
if(IsEditorPaused) return;
eventHandler.EnumerateSubscribers(enumerationCache);
int count = enumerationCache.Count;
for(int i = 0; i < count; i++)
{
enumerationCache[i].action.Invoke();
}
}
private static void InvokeOnce(InvocationList eventHandler)
{
if(IsEditorPaused) return;
eventHandler.EnumerateSubscribers(enumerationCache);
int count = enumerationCache.Count;
for(int i = 0; i < count; i++)
{
enumerationCache[i].action.Invoke();
}
eventHandler.RemoveAll(true);
}
internal static void InvokeOnGUI()
{
onGUI.EnumerateSubscribers(enumerationCache);
int count = enumerationCache.Count;
for(int i = 0; i < count; i++)
{
enumerationCache[i].action.Invoke();
GUI.enabled = true;
GUI.depth = 0;
GUI.changed = false;
GUI.tooltip = null;
GUI.matrix = Matrix4x4.identity;
GUI.color = Color.white;
GUI.backgroundColor = Color.white;
GUI.contentColor = Color.white;
}
}
internal static void InvokeOnDrawGizmosRuntime()
{
onDrawGizmosRuntime.EnumerateSubscribers(enumerationCache);
int count = enumerationCache.Count;
for(int i = 0; i < count; i++)
{
enumerationCache[i].action.Invoke();
Gizmos.matrix = Matrix4x4.identity;
Gizmos.color = Color.white;
}
}
[RuntimeInitializeOnLoadMethod]
private static void Init()
{
var loop = UnityPlayerLoop.GetCurrentPlayerLoop();
InsertSubsystem(ref loop, typeof(PreUpdate), typeof(UpdateLoopPreUpdateEvent), () => Invoke(preUpdate), null, Position.After);
InsertSubsystem(ref loop, typeof(Update), typeof(UpdateLoopUpdateEvent), () => { Invoke(update); InvokeOnce(updateOnce); }, typeof(Update.ScriptRunBehaviourUpdate), Position.Before);
InsertSubsystem(ref loop, typeof(Update), typeof(UpdateLoopPreLateUpdateEvent), () => Invoke(preLateUpdate), typeof(Update.ScriptRunBehaviourUpdate), Position.After);
InsertSubsystem(ref loop, typeof(PreLateUpdate), typeof(UpdateLoopLateUpdateEvent), () => Invoke(lateUpdate), typeof(PreLateUpdate.ScriptRunBehaviourLateUpdate), Position.Before);
InsertSubsystem(ref loop, typeof(PostLateUpdate), typeof(UpdateLoopPostLateUpdateEvent), () => Invoke(postLateUpdate), null, Position.After);
InsertSubsystem(ref loop, typeof(FixedUpdate), typeof(UpdateLoopFixedUpdateEvent), () => { Invoke(fixedUpdate); InvokeOnce(fixedUpdateOnce); }, typeof(FixedUpdate.ScriptRunBehaviourFixedUpdate), Position.Before);
InsertSubsystem(ref loop, typeof(FixedUpdate), typeof(UpdateLoopLateFixedUpdateEvent), () => Invoke(lateFixedUpdate), typeof(FixedUpdate.ScriptRunBehaviourFixedUpdate), Position.After);
InsertSubsystem(ref loop, typeof(FixedUpdate), typeof(UpdateLoopPostFixedUpdateEvent), () => Invoke(postFixedUpdate), typeof(UpdateLoopLateFixedUpdateEvent), Position.After);
UnityPlayerLoop.SetPlayerLoop(loop);
SubscribeMethodsWithAttributes();
//LogCurrentPlayerLoop(false);
Application.quitting += Cleanup;
}
private static void Cleanup()
{
#if UNITY_EDITOR
preUpdate.RemoveAll();
update.RemoveAll();
preLateUpdate.RemoveAll();
lateUpdate.RemoveAll();
postLateUpdate.RemoveAll();
fixedUpdate.RemoveAll();
postFixedUpdate.RemoveAll();
updateOnce.RemoveAll();
onGUI.RemoveAll();
onDrawGizmosRuntime.RemoveAll();
UpdateLoopScriptInstance.Cleanup();
#endif
}
private static void SubscribeMethodsWithAttributes()
{
SubscribeAttributeToEvent<PreUpdateAttribute>(preUpdate);
SubscribeAttributeToEvent<UpdateAttribute>(update);
SubscribeAttributeToEvent<PreLateUpdateAttribute>(preLateUpdate);
SubscribeAttributeToEvent<LateUpdateAttribute>(lateUpdate);
SubscribeAttributeToEvent<PostLateUpdateAttribute>(postLateUpdate);
SubscribeAttributeToEvent<FixedUpdateAttribute>(fixedUpdate);
SubscribeAttributeToEvent<LateFixedUpdateAttribute>(lateFixedUpdate);
SubscribeAttributeToEvent<PostFixedUpdateAttribute>(postFixedUpdate);
SubscribeAttributeToEvent<UpdateOnceAttribute>(updateOnce);
SubscribeAttributeToEvent<FixedUpdateOnceAttribute>(fixedUpdateOnce);
SubscribeAttributeToEvent<OnGUIAttribute>(onGUI);
SubscribeAttributeToEvent<OnDrawGizmosRuntimeAttribute>(onDrawGizmosRuntime);
if(onDrawGizmosRuntime.subscribers.Count > 0)
{
UpdateLoopScriptInstance.InitIfRequired();
}
}
private static void SubscribeAttributeToEvent<A>(InvocationList eventHandler) where A : Attribute
{
foreach(var m in ReflectionUtility.GetMethodsWithAttribute<A>(true))
{
var action = (Action)m.CreateDelegate(typeof(Action));
eventHandler.Add(action);
}
foreach(var m in ReflectionUtility.GetMethodsWithAttribute<A>(false))
{
Debug.LogError($"The Attribute '{typeof(A)}' is only valid on static methods ({m.DeclaringType}:{m.Name}). The method will not be invoked.");
}
}
/// <summary>
/// Adds a subsystem to the given root system before all other child systems.
/// </summary>
public static void AddSubsystemFirst(Type subSystemRoot, Type add, PlayerLoopSystem.UpdateFunction invocationTarget)
{
var loop = UnityPlayerLoop.GetCurrentPlayerLoop();
InsertSubsystem(ref loop, subSystemRoot, add, invocationTarget, null, Position.Before);
UnityPlayerLoop.SetPlayerLoop(loop);
}
/// <summary>
/// Adds a subsystem to the given root system after all other child systems.
/// </summary>
public static void AddSubsystemLast(Type subSystemRoot, Type add, PlayerLoopSystem.UpdateFunction invocationTarget)
{
var loop = UnityPlayerLoop.GetCurrentPlayerLoop();
InsertSubsystem(ref loop, subSystemRoot, add, invocationTarget, null, Position.After);
UnityPlayerLoop.SetPlayerLoop(loop);
}
/// <summary>
/// Adds a subsystem to the given root system and after the given child system.
/// </summary>
public static void AddSubsystemBefore(Type subSystemRoot, Type add, PlayerLoopSystem.UpdateFunction invocationTarget, Type beforeSubSystem)
{
var loop = UnityPlayerLoop.GetCurrentPlayerLoop();
InsertSubsystem(ref loop, subSystemRoot, add, invocationTarget, beforeSubSystem, Position.Before);
UnityPlayerLoop.SetPlayerLoop(loop);
}
/// <summary>
/// Adds a subsystem to the given root system and after the given child system.
/// </summary>
public static void AddSubsystemAfter(Type subSystemRoot, Type add, PlayerLoopSystem.UpdateFunction invocationTarget, Type afterSubSystem)
{
var loop = UnityPlayerLoop.GetCurrentPlayerLoop();
InsertSubsystem(ref loop, subSystemRoot, add, invocationTarget, afterSubSystem, Position.After);
UnityPlayerLoop.SetPlayerLoop(loop);
}
private static void InsertSubsystem(ref PlayerLoopSystem root, Type subSystemRoot, Type typeToAdd, PlayerLoopSystem.UpdateFunction invocationTarget, Type referenceSubSystem, Position position)
{
int index = -1;
for(int i = 0; i < root.subSystemList.Length; i++)
{
if(root.subSystemList[i].type != null && root.subSystemList[i].type.Name == subSystemRoot.Name)
{
index = i;
break;
}
}
if(index < 0)
{
throw new NullReferenceException($"Subsystem of type '{subSystemRoot}' not found.");
}
var sub = root.subSystemList[index];
Insert(ref sub, new PlayerLoopSystem() { updateDelegate = invocationTarget, type = typeToAdd }, referenceSubSystem, position);
root.subSystemList[index] = sub;
}
private static void Insert(ref PlayerLoopSystem system, PlayerLoopSystem systemToAdd, Type reference, Position position)
{
List<PlayerLoopSystem> subsystems;
if(system.subSystemList == null)
{
subsystems = new List<PlayerLoopSystem>();
}
else
{
subsystems = new List<PlayerLoopSystem>(system.subSystemList);
}
if(reference != null)
{
var index = subsystems.FindIndex((s) => s.type == reference);
if(index < 0)
{
throw new NullReferenceException($"Subsystem of type '{reference}' not found, system not added.");
}
systemToAdd.loopConditionFunction = subsystems[index].loopConditionFunction;
if(position == Position.Before)
{
subsystems.Insert(index, systemToAdd);
}
else
{
subsystems.Insert(index + 1, systemToAdd);
}
}
else
{
if(position == Position.Before)
{
subsystems.Insert(0, systemToAdd);
}
else
{
subsystems.Add(systemToAdd);
}
}
system.subSystemList = subsystems.ToArray();
}
/// <summary>
/// Logs the entire hierarchy of player loop systems to the console.
/// </summary>
public static void LogCurrentPlayerLoop(bool includeFunctionPtrs)
{
var sb = new StringBuilder();
PrintLoopRecursive(sb, UnityPlayerLoop.GetCurrentPlayerLoop(), 0, includeFunctionPtrs);
Debug.Log(sb.ToString());
}
private static void PrintLoopRecursive(StringBuilder sb, PlayerLoopSystem root, int indentLevel, bool includeFunctionPtrs)
{
for(int i = 0; i < indentLevel; i++) sb.Append("\t");
string name = root.type?.Name ?? (indentLevel == 0 ? "PlayerLoop" : "<NULL>");
sb.Append(name);
if(includeFunctionPtrs)
{
int loopConditionPtr = (int)root.loopConditionFunction;
int updatePtr = (int)root.updateFunction;
if(loopConditionPtr != 0 || updatePtr != 0)
{
sb.Append(" [");
if(loopConditionPtr != 0) sb.Append($"LoopCondition: 0x{loopConditionPtr:X}");
if(loopConditionPtr != 0 && updatePtr != 0) sb.Append(" ");
if(updatePtr != 0) sb.Append($"Update: 0x{updatePtr:X}");
sb.Append("]");
}
}
sb.AppendLine();
if(root.subSystemList != null)
{
indentLevel++;
foreach(var s in root.subSystemList)
{
PrintLoopRecursive(sb, s, indentLevel, includeFunctionPtrs);
}
}
}
}
}