| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using GDX.Collections.Generic; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.LowLevel; |
| | 8 | | using UnityEngine.PlayerLoop; |
| | 9 | |
|
| | 10 | | namespace GDX.Developer |
| | 11 | | { |
| | 12 | | public static class ManagedUpdateSystem |
| | 13 | | { |
| | 14 | | const int k_PreAllocatedSystems = 10; |
| 0 | 15 | | static SimpleList<IManagedInitialization> s_ManagedInitialization = |
| | 16 | | new SimpleList<IManagedInitialization>(k_PreAllocatedSystems); |
| | 17 | | static int s_ManagedInitializationCount; |
| 0 | 18 | | static SimpleList<IManagedUpdate> s_ManagedUpdate = new SimpleList<IManagedUpdate>(k_PreAllocatedSystems); |
| | 19 | | static int s_ManagedUpdateCount; |
| 0 | 20 | | static SimpleList<IManagedPreUpdate> s_ManagedPreUpdate = new SimpleList<IManagedPreUpdate>(k_PreAllocatedSystem |
| | 21 | | static int s_ManagedPreUpdateCount; |
| 0 | 22 | | static SimpleList<IManagedFixedUpdate> s_ManagedFixedUpdate = |
| | 23 | | new SimpleList<IManagedFixedUpdate>(k_PreAllocatedSystems); |
| | 24 | | static int s_ManagedFixedUpdateCount; |
| 0 | 25 | | static SimpleList<IManagedEarlyUpdate> s_ManagedEarlyUpdate = |
| | 26 | | new SimpleList<IManagedEarlyUpdate>(k_PreAllocatedSystems); |
| | 27 | | static int s_ManagedEarlyUpdateCount; |
| 0 | 28 | | static SimpleList<IManagedPreLateUpdate> s_ManagedPreLateUpdate = |
| | 29 | | new SimpleList<IManagedPreLateUpdate>(k_PreAllocatedSystems); |
| | 30 | | static int s_ManagedPreLateUpdateCount; |
| 0 | 31 | | static SimpleList<IManagedPostLateUpdate> s_ManagedPostLateUpdate = |
| | 32 | | new SimpleList<IManagedPostLateUpdate>(k_PreAllocatedSystems); |
| | 33 | | static int s_ManagedPostLateUpdateCount; |
| | 34 | |
|
| | 35 | | static bool s_AddedToPlayerLoop; |
| | 36 | |
|
| | 37 | | public static void RegisterManagedInitialization(IManagedInitialization managedInitialization) |
| 0 | 38 | | { |
| 0 | 39 | | s_ManagedInitialization.AddWithExpandCheck(managedInitialization); |
| 0 | 40 | | s_ManagedInitializationCount = s_ManagedInitialization.Count; |
| 0 | 41 | | } |
| | 42 | | public static void RemoveManagedInitialization(IManagedInitialization managedInitialization) |
| 0 | 43 | | { |
| 0 | 44 | | s_ManagedInitialization.RemoveFirstItem(managedInitialization); |
| 0 | 45 | | s_ManagedInitializationCount = s_ManagedInitialization.Count; |
| 0 | 46 | | } |
| | 47 | | public static void RegisterManagedUpdate(IManagedUpdate managedUpdate) |
| 0 | 48 | | { |
| 0 | 49 | | s_ManagedUpdate.AddWithExpandCheck(managedUpdate); |
| 0 | 50 | | s_ManagedUpdateCount = s_ManagedUpdate.Count; |
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | public static void UnregisterManagedUpdate(IManagedUpdate managedUpdate) |
| 0 | 54 | | { |
| 0 | 55 | | s_ManagedUpdate.RemoveFirstItem(managedUpdate); |
| 0 | 56 | | s_ManagedUpdateCount = s_ManagedUpdate.Count; |
| 0 | 57 | | } |
| | 58 | |
|
| | 59 | | public static void RegisterManagedPreUpdate(IManagedPreUpdate managedPreUpdate) |
| 0 | 60 | | { |
| 0 | 61 | | s_ManagedPreUpdate.AddWithExpandCheck(managedPreUpdate); |
| 0 | 62 | | s_ManagedPreUpdateCount = s_ManagedPreUpdate.Count; |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | public static void UnregisterManagedPreUpdate(IManagedPreUpdate managedPreUpdate) |
| 0 | 66 | | { |
| 0 | 67 | | s_ManagedPreUpdate.RemoveFirstItem(managedPreUpdate); |
| 0 | 68 | | s_ManagedPreUpdateCount = s_ManagedPreUpdate.Count; |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | static void Initialization() |
| 0 | 72 | | { |
| 0 | 73 | | float deltaTime = Time.deltaTime; |
| 0 | 74 | | float unscaledDeltaTime = Time.unscaledDeltaTime; |
| 0 | 75 | | for (int i = 0; i < s_ManagedInitializationCount; i++) |
| 0 | 76 | | { |
| 0 | 77 | | s_ManagedInitialization.Array[i].ManagedInitialization(deltaTime, unscaledDeltaTime); |
| 0 | 78 | | } |
| 0 | 79 | | } |
| | 80 | |
|
| | 81 | | static void Update() |
| 0 | 82 | | { |
| 0 | 83 | | float deltaTime = Time.deltaTime; |
| 0 | 84 | | float unscaledDeltaTime = Time.unscaledDeltaTime; |
| 0 | 85 | | for (int i = 0; i < s_ManagedUpdateCount; i++) |
| 0 | 86 | | { |
| 0 | 87 | | s_ManagedUpdate.Array[i].ManagedUpdate(deltaTime, unscaledDeltaTime); |
| 0 | 88 | | } |
| 0 | 89 | | } |
| | 90 | |
|
| | 91 | | static void PreUpdate() |
| 0 | 92 | | { |
| 0 | 93 | | float deltaTime = Time.deltaTime; |
| 0 | 94 | | float unscaledDeltaTime = Time.unscaledDeltaTime; |
| 0 | 95 | | for (int i = 0; i < s_ManagedPreUpdateCount; i++) |
| 0 | 96 | | { |
| 0 | 97 | | s_ManagedPreUpdate.Array[i].ManagedPreUpdate(deltaTime, unscaledDeltaTime); |
| 0 | 98 | | } |
| 0 | 99 | | } |
| | 100 | |
|
| | 101 | | public static void RegisterManagedFixedUpdate(IManagedFixedUpdate managedFixedUpdate) |
| 0 | 102 | | { |
| 0 | 103 | | s_ManagedFixedUpdate.AddWithExpandCheck(managedFixedUpdate); |
| 0 | 104 | | s_ManagedFixedUpdateCount = s_ManagedFixedUpdate.Count; |
| 0 | 105 | | } |
| | 106 | |
|
| | 107 | | public static void UnregisterManagedFixedUpdate(IManagedFixedUpdate managedFixedUpdate) |
| 0 | 108 | | { |
| 0 | 109 | | s_ManagedFixedUpdate.RemoveFirstItem(managedFixedUpdate); |
| 0 | 110 | | s_ManagedFixedUpdateCount = s_ManagedFixedUpdate.Count; |
| 0 | 111 | | } |
| | 112 | |
|
| | 113 | | static void FixedUpdate() |
| 0 | 114 | | { |
| 0 | 115 | | float deltaTime = Time.deltaTime; |
| 0 | 116 | | float unscaledDeltaTime = Time.unscaledDeltaTime; |
| 0 | 117 | | for (int i = 0; i < s_ManagedFixedUpdateCount; i++) |
| 0 | 118 | | { |
| 0 | 119 | | s_ManagedFixedUpdate.Array[i].ManagedFixedUpdate(deltaTime, unscaledDeltaTime); |
| 0 | 120 | | } |
| 0 | 121 | | } |
| | 122 | |
|
| | 123 | | public static void RegisterManagedEarlyUpdate(IManagedEarlyUpdate managedEarlyUpdate) |
| 0 | 124 | | { |
| 0 | 125 | | s_ManagedEarlyUpdate.AddWithExpandCheck(managedEarlyUpdate); |
| 0 | 126 | | s_ManagedEarlyUpdateCount = s_ManagedEarlyUpdate.Count; |
| 0 | 127 | | } |
| | 128 | |
|
| | 129 | | public static void UnregisterManagedEarlyUpdate(IManagedEarlyUpdate managedEarlyUpdate) |
| 0 | 130 | | { |
| 0 | 131 | | s_ManagedEarlyUpdate.RemoveFirstItem(managedEarlyUpdate); |
| 0 | 132 | | s_ManagedEarlyUpdateCount = s_ManagedEarlyUpdate.Count; |
| 0 | 133 | | } |
| | 134 | |
|
| | 135 | | static void EarlyUpdate() |
| 0 | 136 | | { |
| 0 | 137 | | float deltaTime = Time.deltaTime; |
| 0 | 138 | | float unscaledDeltaTime = Time.unscaledDeltaTime; |
| 0 | 139 | | for (int i = 0; i < s_ManagedEarlyUpdateCount; i++) |
| 0 | 140 | | { |
| 0 | 141 | | s_ManagedEarlyUpdate.Array[i].ManagedEarlyUpdate(deltaTime, unscaledDeltaTime); |
| 0 | 142 | | } |
| 0 | 143 | | } |
| | 144 | |
|
| | 145 | | public static void RegisterManagedPreLateUpdate(IManagedPreLateUpdate managedPreLateUpdate) |
| 0 | 146 | | { |
| 0 | 147 | | s_ManagedPreLateUpdate.AddWithExpandCheck(managedPreLateUpdate); |
| 0 | 148 | | s_ManagedPreLateUpdateCount = s_ManagedPreLateUpdate.Count; |
| 0 | 149 | | } |
| | 150 | |
|
| | 151 | | public static void UnregisterManagedPreLateUpdate(IManagedPreLateUpdate managedPreLateUpdate) |
| 0 | 152 | | { |
| 0 | 153 | | s_ManagedPreLateUpdate.RemoveFirstItem(managedPreLateUpdate); |
| 0 | 154 | | s_ManagedPreLateUpdateCount = s_ManagedPreLateUpdate.Count; |
| 0 | 155 | | } |
| | 156 | |
|
| | 157 | | static void PreLateUpdate() |
| 0 | 158 | | { |
| 0 | 159 | | float deltaTime = Time.deltaTime; |
| 0 | 160 | | float unscaledDeltaTime = Time.unscaledDeltaTime; |
| 0 | 161 | | for (int i = 0; i < s_ManagedPreLateUpdateCount; i++) |
| 0 | 162 | | { |
| 0 | 163 | | s_ManagedPreLateUpdate.Array[i].ManagedPreLateUpdate(deltaTime, unscaledDeltaTime); |
| 0 | 164 | | } |
| 0 | 165 | | } |
| | 166 | |
|
| | 167 | | public static void RegisterManagedPostLateUpdate(IManagedPostLateUpdate managedPostLateUpdate) |
| 0 | 168 | | { |
| 0 | 169 | | s_ManagedPostLateUpdate.AddWithExpandCheck(managedPostLateUpdate); |
| 0 | 170 | | s_ManagedPostLateUpdateCount = s_ManagedPostLateUpdate.Count; |
| 0 | 171 | | } |
| | 172 | |
|
| | 173 | | public static void UnregisterManagedPostLateUpdate(IManagedPostLateUpdate managedPostLateUpdate) |
| 0 | 174 | | { |
| 0 | 175 | | s_ManagedPostLateUpdate.RemoveFirstItem(managedPostLateUpdate); |
| 0 | 176 | | s_ManagedPostLateUpdateCount = s_ManagedPostLateUpdate.Count; |
| 0 | 177 | | } |
| | 178 | |
|
| | 179 | | static void PostLateUpdate() |
| 0 | 180 | | { |
| 0 | 181 | | float deltaTime = Time.deltaTime; |
| 0 | 182 | | float unscaledDeltaTime = Time.unscaledDeltaTime; |
| 0 | 183 | | for (int i = 0; i < s_ManagedPostLateUpdateCount; i++) |
| 0 | 184 | | { |
| 0 | 185 | | s_ManagedPostLateUpdate.Array[i].ManagedPostLateUpdate(deltaTime, unscaledDeltaTime); |
| 0 | 186 | | } |
| 0 | 187 | | } |
| | 188 | |
|
| | 189 | |
|
| | 190 | | public static void AddToPlayerLoop() |
| 0 | 191 | | { |
| 0 | 192 | | PlayerLoopSystem systemRoot = PlayerLoop.GetCurrentPlayerLoop(); |
| | 193 | |
|
| 0 | 194 | | systemRoot.AddSubSystemToFirstSubSystemOfType( |
| | 195 | | typeof(Initialization), |
| | 196 | | typeof(ManagedUpdateSystem), Initialization); |
| 0 | 197 | | systemRoot.AddSubSystemToFirstSubSystemOfType( |
| | 198 | | typeof(EarlyUpdate), |
| | 199 | | typeof(ManagedUpdateSystem), EarlyUpdate); |
| 0 | 200 | | systemRoot.AddSubSystemToFirstSubSystemOfType( |
| | 201 | | typeof(FixedUpdate), |
| | 202 | | typeof(ManagedUpdateSystem), FixedUpdate); |
| | 203 | |
|
| 0 | 204 | | systemRoot.AddSubSystemToFirstSubSystemOfType( |
| | 205 | | typeof(PreUpdate), |
| | 206 | | typeof(ManagedUpdateSystem), PreUpdate); |
| | 207 | |
|
| 0 | 208 | | systemRoot.AddSubSystemToFirstSubSystemOfType( |
| | 209 | | typeof(Update), |
| | 210 | | typeof(ManagedUpdateSystem), Update); |
| | 211 | |
|
| 0 | 212 | | systemRoot.AddSubSystemToFirstSubSystemOfType( |
| | 213 | | typeof(PreLateUpdate), |
| | 214 | | typeof(ManagedUpdateSystem), PreLateUpdate); |
| | 215 | |
|
| 0 | 216 | | systemRoot.AddSubSystemToFirstSubSystemOfType( |
| | 217 | | typeof(PostLateUpdate), |
| | 218 | | typeof(ManagedUpdateSystem), PostLateUpdate); |
| | 219 | |
|
| 0 | 220 | | PlayerLoop.SetPlayerLoop(systemRoot); |
| 0 | 221 | | s_AddedToPlayerLoop = true; |
| 0 | 222 | | } |
| | 223 | |
|
| | 224 | | public static void RemoveFromPlayerLoop() |
| 0 | 225 | | { |
| 0 | 226 | | if (!s_AddedToPlayerLoop) return; |
| | 227 | |
|
| 0 | 228 | | PlayerLoopSystem systemRoot = PlayerLoop.GetCurrentPlayerLoop(); |
| | 229 | |
|
| 0 | 230 | | systemRoot.RemoveSubSystemsOfTypeFromFirstSubSystemOfType(typeof(Initialization), |
| | 231 | | typeof(ManagedUpdateSystem)); |
| | 232 | |
|
| 0 | 233 | | systemRoot.RemoveSubSystemsOfTypeFromFirstSubSystemOfType(typeof(EarlyUpdate), |
| | 234 | | typeof(EarlyUpdate)); |
| | 235 | |
|
| 0 | 236 | | systemRoot.RemoveSubSystemsOfTypeFromFirstSubSystemOfType(typeof(FixedUpdate), |
| | 237 | | typeof(FixedUpdate)); |
| | 238 | |
|
| 0 | 239 | | systemRoot.RemoveSubSystemsOfTypeFromFirstSubSystemOfType(typeof(PreUpdate), |
| | 240 | | typeof(PreUpdate)); |
| | 241 | |
|
| 0 | 242 | | systemRoot.RemoveSubSystemsOfTypeFromFirstSubSystemOfType(typeof(Update), |
| | 243 | | typeof(Update)); |
| | 244 | |
|
| 0 | 245 | | systemRoot.RemoveSubSystemsOfTypeFromFirstSubSystemOfType(typeof(PreLateUpdate), |
| | 246 | | typeof(PreLateUpdate)); |
| | 247 | |
|
| 0 | 248 | | systemRoot.RemoveSubSystemsOfTypeFromFirstSubSystemOfType(typeof(PostLateUpdate), |
| | 249 | | typeof(PostLateUpdate)); |
| | 250 | |
|
| 0 | 251 | | PlayerLoop.SetPlayerLoop(systemRoot); |
| 0 | 252 | | s_AddedToPlayerLoop = false; |
| 0 | 253 | | } |
| | 254 | |
|
| | 255 | | public static bool IsRunning() |
| 0 | 256 | | { |
| 0 | 257 | | return s_AddedToPlayerLoop; |
| 0 | 258 | | } |
| | 259 | |
|
| | 260 | | public interface IManagedInitialization |
| | 261 | | { |
| | 262 | | void ManagedInitialization(float deltaTime, float unscaledDeltaTime); |
| | 263 | | } |
| | 264 | | public interface IManagedUpdate |
| | 265 | | { |
| | 266 | | void ManagedUpdate(float deltaTime, float unscaledDeltaTime); |
| | 267 | | } |
| | 268 | | public interface IManagedPreUpdate |
| | 269 | | { |
| | 270 | | void ManagedPreUpdate(float deltaTime, float unscaledDeltaTime); |
| | 271 | | } |
| | 272 | |
|
| | 273 | | public interface IManagedFixedUpdate |
| | 274 | | { |
| | 275 | | void ManagedFixedUpdate(float deltaTime, float unscaledDeltaTime); |
| | 276 | | } |
| | 277 | |
|
| | 278 | |
|
| | 279 | | public interface IManagedEarlyUpdate |
| | 280 | | { |
| | 281 | | void ManagedEarlyUpdate(float deltaTime, float unscaledDeltaTime); |
| | 282 | | } |
| | 283 | |
|
| | 284 | | public interface IManagedPreLateUpdate |
| | 285 | | { |
| | 286 | | void ManagedPreLateUpdate(float deltaTime, float unscaledDeltaTime); |
| | 287 | | } |
| | 288 | |
|
| | 289 | | public interface IManagedPostLateUpdate |
| | 290 | | { |
| | 291 | | void ManagedPostLateUpdate(float deltaTime, float unscaledDeltaTime); |
| | 292 | | } |
| | 293 | | } |
| | 294 | | } |