/
ManagedUpdateSystem.cs
294 lines (247 loc) · 11.2 KB
/
ManagedUpdateSystem.cs
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
// Copyright (c) 2020-2024 dotBunny Inc.
// dotBunny licenses this file to you under the BSL-1.0 license.
// See the LICENSE file in the project root for more information.
using GDX.Collections.Generic;
using UnityEngine;
using UnityEngine.LowLevel;
using UnityEngine.PlayerLoop;
namespace GDX.Developer
{
public static class ManagedUpdateSystem
{
const int k_PreAllocatedSystems = 10;
static SimpleList<IManagedInitialization> s_ManagedInitialization =
new SimpleList<IManagedInitialization>(k_PreAllocatedSystems);
static int s_ManagedInitializationCount;
static SimpleList<IManagedUpdate> s_ManagedUpdate = new SimpleList<IManagedUpdate>(k_PreAllocatedSystems);
static int s_ManagedUpdateCount;
static SimpleList<IManagedPreUpdate> s_ManagedPreUpdate = new SimpleList<IManagedPreUpdate>(k_PreAllocatedSystems);
static int s_ManagedPreUpdateCount;
static SimpleList<IManagedFixedUpdate> s_ManagedFixedUpdate =
new SimpleList<IManagedFixedUpdate>(k_PreAllocatedSystems);
static int s_ManagedFixedUpdateCount;
static SimpleList<IManagedEarlyUpdate> s_ManagedEarlyUpdate =
new SimpleList<IManagedEarlyUpdate>(k_PreAllocatedSystems);
static int s_ManagedEarlyUpdateCount;
static SimpleList<IManagedPreLateUpdate> s_ManagedPreLateUpdate =
new SimpleList<IManagedPreLateUpdate>(k_PreAllocatedSystems);
static int s_ManagedPreLateUpdateCount;
static SimpleList<IManagedPostLateUpdate> s_ManagedPostLateUpdate =
new SimpleList<IManagedPostLateUpdate>(k_PreAllocatedSystems);
static int s_ManagedPostLateUpdateCount;
static bool s_AddedToPlayerLoop;
public static void RegisterManagedInitialization(IManagedInitialization managedInitialization)
{
s_ManagedInitialization.AddWithExpandCheck(managedInitialization);
s_ManagedInitializationCount = s_ManagedInitialization.Count;
}
public static void RemoveManagedInitialization(IManagedInitialization managedInitialization)
{
s_ManagedInitialization.RemoveFirstItem(managedInitialization);
s_ManagedInitializationCount = s_ManagedInitialization.Count;
}
public static void RegisterManagedUpdate(IManagedUpdate managedUpdate)
{
s_ManagedUpdate.AddWithExpandCheck(managedUpdate);
s_ManagedUpdateCount = s_ManagedUpdate.Count;
}
public static void UnregisterManagedUpdate(IManagedUpdate managedUpdate)
{
s_ManagedUpdate.RemoveFirstItem(managedUpdate);
s_ManagedUpdateCount = s_ManagedUpdate.Count;
}
public static void RegisterManagedPreUpdate(IManagedPreUpdate managedPreUpdate)
{
s_ManagedPreUpdate.AddWithExpandCheck(managedPreUpdate);
s_ManagedPreUpdateCount = s_ManagedPreUpdate.Count;
}
public static void UnregisterManagedPreUpdate(IManagedPreUpdate managedPreUpdate)
{
s_ManagedPreUpdate.RemoveFirstItem(managedPreUpdate);
s_ManagedPreUpdateCount = s_ManagedPreUpdate.Count;
}
static void Initialization()
{
float deltaTime = Time.deltaTime;
float unscaledDeltaTime = Time.unscaledDeltaTime;
for (int i = 0; i < s_ManagedInitializationCount; i++)
{
s_ManagedInitialization.Array[i].ManagedInitialization(deltaTime, unscaledDeltaTime);
}
}
static void Update()
{
float deltaTime = Time.deltaTime;
float unscaledDeltaTime = Time.unscaledDeltaTime;
for (int i = 0; i < s_ManagedUpdateCount; i++)
{
s_ManagedUpdate.Array[i].ManagedUpdate(deltaTime, unscaledDeltaTime);
}
}
static void PreUpdate()
{
float deltaTime = Time.deltaTime;
float unscaledDeltaTime = Time.unscaledDeltaTime;
for (int i = 0; i < s_ManagedPreUpdateCount; i++)
{
s_ManagedPreUpdate.Array[i].ManagedPreUpdate(deltaTime, unscaledDeltaTime);
}
}
public static void RegisterManagedFixedUpdate(IManagedFixedUpdate managedFixedUpdate)
{
s_ManagedFixedUpdate.AddWithExpandCheck(managedFixedUpdate);
s_ManagedFixedUpdateCount = s_ManagedFixedUpdate.Count;
}
public static void UnregisterManagedFixedUpdate(IManagedFixedUpdate managedFixedUpdate)
{
s_ManagedFixedUpdate.RemoveFirstItem(managedFixedUpdate);
s_ManagedFixedUpdateCount = s_ManagedFixedUpdate.Count;
}
static void FixedUpdate()
{
float deltaTime = Time.deltaTime;
float unscaledDeltaTime = Time.unscaledDeltaTime;
for (int i = 0; i < s_ManagedFixedUpdateCount; i++)
{
s_ManagedFixedUpdate.Array[i].ManagedFixedUpdate(deltaTime, unscaledDeltaTime);
}
}
public static void RegisterManagedEarlyUpdate(IManagedEarlyUpdate managedEarlyUpdate)
{
s_ManagedEarlyUpdate.AddWithExpandCheck(managedEarlyUpdate);
s_ManagedEarlyUpdateCount = s_ManagedEarlyUpdate.Count;
}
public static void UnregisterManagedEarlyUpdate(IManagedEarlyUpdate managedEarlyUpdate)
{
s_ManagedEarlyUpdate.RemoveFirstItem(managedEarlyUpdate);
s_ManagedEarlyUpdateCount = s_ManagedEarlyUpdate.Count;
}
static void EarlyUpdate()
{
float deltaTime = Time.deltaTime;
float unscaledDeltaTime = Time.unscaledDeltaTime;
for (int i = 0; i < s_ManagedEarlyUpdateCount; i++)
{
s_ManagedEarlyUpdate.Array[i].ManagedEarlyUpdate(deltaTime, unscaledDeltaTime);
}
}
public static void RegisterManagedPreLateUpdate(IManagedPreLateUpdate managedPreLateUpdate)
{
s_ManagedPreLateUpdate.AddWithExpandCheck(managedPreLateUpdate);
s_ManagedPreLateUpdateCount = s_ManagedPreLateUpdate.Count;
}
public static void UnregisterManagedPreLateUpdate(IManagedPreLateUpdate managedPreLateUpdate)
{
s_ManagedPreLateUpdate.RemoveFirstItem(managedPreLateUpdate);
s_ManagedPreLateUpdateCount = s_ManagedPreLateUpdate.Count;
}
static void PreLateUpdate()
{
float deltaTime = Time.deltaTime;
float unscaledDeltaTime = Time.unscaledDeltaTime;
for (int i = 0; i < s_ManagedPreLateUpdateCount; i++)
{
s_ManagedPreLateUpdate.Array[i].ManagedPreLateUpdate(deltaTime, unscaledDeltaTime);
}
}
public static void RegisterManagedPostLateUpdate(IManagedPostLateUpdate managedPostLateUpdate)
{
s_ManagedPostLateUpdate.AddWithExpandCheck(managedPostLateUpdate);
s_ManagedPostLateUpdateCount = s_ManagedPostLateUpdate.Count;
}
public static void UnregisterManagedPostLateUpdate(IManagedPostLateUpdate managedPostLateUpdate)
{
s_ManagedPostLateUpdate.RemoveFirstItem(managedPostLateUpdate);
s_ManagedPostLateUpdateCount = s_ManagedPostLateUpdate.Count;
}
static void PostLateUpdate()
{
float deltaTime = Time.deltaTime;
float unscaledDeltaTime = Time.unscaledDeltaTime;
for (int i = 0; i < s_ManagedPostLateUpdateCount; i++)
{
s_ManagedPostLateUpdate.Array[i].ManagedPostLateUpdate(deltaTime, unscaledDeltaTime);
}
}
public static void AddToPlayerLoop()
{
PlayerLoopSystem systemRoot = PlayerLoop.GetCurrentPlayerLoop();
systemRoot.AddSubSystemToFirstSubSystemOfType(
typeof(Initialization),
typeof(ManagedUpdateSystem), Initialization);
systemRoot.AddSubSystemToFirstSubSystemOfType(
typeof(EarlyUpdate),
typeof(ManagedUpdateSystem), EarlyUpdate);
systemRoot.AddSubSystemToFirstSubSystemOfType(
typeof(FixedUpdate),
typeof(ManagedUpdateSystem), FixedUpdate);
systemRoot.AddSubSystemToFirstSubSystemOfType(
typeof(PreUpdate),
typeof(ManagedUpdateSystem), PreUpdate);
systemRoot.AddSubSystemToFirstSubSystemOfType(
typeof(Update),
typeof(ManagedUpdateSystem), Update);
systemRoot.AddSubSystemToFirstSubSystemOfType(
typeof(PreLateUpdate),
typeof(ManagedUpdateSystem), PreLateUpdate);
systemRoot.AddSubSystemToFirstSubSystemOfType(
typeof(PostLateUpdate),
typeof(ManagedUpdateSystem), PostLateUpdate);
PlayerLoop.SetPlayerLoop(systemRoot);
s_AddedToPlayerLoop = true;
}
public static void RemoveFromPlayerLoop()
{
if (!s_AddedToPlayerLoop) return;
PlayerLoopSystem systemRoot = PlayerLoop.GetCurrentPlayerLoop();
systemRoot.RemoveSubSystemsOfTypeFromFirstSubSystemOfType(typeof(Initialization),
typeof(ManagedUpdateSystem));
systemRoot.RemoveSubSystemsOfTypeFromFirstSubSystemOfType(typeof(EarlyUpdate),
typeof(EarlyUpdate));
systemRoot.RemoveSubSystemsOfTypeFromFirstSubSystemOfType(typeof(FixedUpdate),
typeof(FixedUpdate));
systemRoot.RemoveSubSystemsOfTypeFromFirstSubSystemOfType(typeof(PreUpdate),
typeof(PreUpdate));
systemRoot.RemoveSubSystemsOfTypeFromFirstSubSystemOfType(typeof(Update),
typeof(Update));
systemRoot.RemoveSubSystemsOfTypeFromFirstSubSystemOfType(typeof(PreLateUpdate),
typeof(PreLateUpdate));
systemRoot.RemoveSubSystemsOfTypeFromFirstSubSystemOfType(typeof(PostLateUpdate),
typeof(PostLateUpdate));
PlayerLoop.SetPlayerLoop(systemRoot);
s_AddedToPlayerLoop = false;
}
public static bool IsRunning()
{
return s_AddedToPlayerLoop;
}
public interface IManagedInitialization
{
void ManagedInitialization(float deltaTime, float unscaledDeltaTime);
}
public interface IManagedUpdate
{
void ManagedUpdate(float deltaTime, float unscaledDeltaTime);
}
public interface IManagedPreUpdate
{
void ManagedPreUpdate(float deltaTime, float unscaledDeltaTime);
}
public interface IManagedFixedUpdate
{
void ManagedFixedUpdate(float deltaTime, float unscaledDeltaTime);
}
public interface IManagedEarlyUpdate
{
void ManagedEarlyUpdate(float deltaTime, float unscaledDeltaTime);
}
public interface IManagedPreLateUpdate
{
void ManagedPreLateUpdate(float deltaTime, float unscaledDeltaTime);
}
public interface IManagedPostLateUpdate
{
void ManagedPostLateUpdate(float deltaTime, float unscaledDeltaTime);
}
}
}