| | 1 | | // Copyright (c) 2020-2023 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 System; |
| | 6 | | using System.Diagnostics; |
| | 7 | | using GDX.Experimental; |
| | 8 | | using GDX.Experimental.Logging; |
| | 9 | | using UnityEngine; |
| | 10 | | using UnityEngine.LowLevel; |
| | 11 | | using UnityEngine.PlayerLoop; |
| | 12 | |
|
| | 13 | | namespace GDX.Threading |
| | 14 | | { |
| | 15 | | public static class TaskDirectorSystem |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Has the task director been added to the player loop for updating at runtime. |
| | 19 | | /// </summary> |
| | 20 | | static bool s_AddedToPlayerLoop; |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// How often should the <see cref="TaskDirector"/> be ticked? |
| | 24 | | /// </summary> |
| | 25 | | /// <remarks> |
| | 26 | | /// This works by accumulation, when time between the last tick and current time exceeds |
| | 27 | | /// <see cref="s_TickRate"/>, a tick will be triggered. Default configured in <see cref="Config"/>. |
| | 28 | | /// </remarks> |
| 1 | 29 | | static float s_TickRate = -1; |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// An accumulation of time since the last tick. |
| | 33 | | /// </summary> |
| | 34 | | static float s_TimeSinceLastTick; |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Triggered after the <see cref="TaskDirectorSystem"/> has ticked, with the delta time. |
| | 38 | | /// </summary> |
| | 39 | | public static Action<float> ticked; |
| | 40 | |
|
| | 41 | | public static void AddToPlayerLoop() |
| 8 | 42 | | { |
| 13 | 43 | | if (s_AddedToPlayerLoop || !Config.TaskDirectorSystem) return; |
| | 44 | | #if !UNITY_EDITOR |
| | 45 | | ManagedLog.Info(LogCategory.GDX, "GDX runtime task scheduler activated."); |
| | 46 | | #endif // !UNITY_EDITOR |
| | 47 | |
|
| 3 | 48 | | PlayerLoopSystem systemRoot = PlayerLoop.GetCurrentPlayerLoop(); |
| 3 | 49 | | systemRoot.AddSubSystemToFirstSubSystemOfType( |
| | 50 | | typeof(Update.ScriptRunDelayedTasks), |
| | 51 | | typeof(TaskDirectorSystem), PlayerLoopTick); |
| 3 | 52 | | PlayerLoop.SetPlayerLoop(systemRoot); |
| | 53 | |
|
| 3 | 54 | | s_AddedToPlayerLoop = true; |
| 8 | 55 | | } |
| | 56 | |
|
| | 57 | | /// <summary> |
| | 58 | | /// Get the current tick rate used by the <see cref="TaskDirectorSystem"/>. |
| | 59 | | /// </summary> |
| | 60 | | /// <returns> |
| | 61 | | /// A double value representing the elapsed time necessary to trigger an update to the |
| | 62 | | /// <see cref="TaskDirectorSystem"/>. |
| | 63 | | /// </returns> |
| | 64 | | public static float GetTickRate() |
| 8 | 65 | | { |
| 8 | 66 | | return s_TickRate; |
| 8 | 67 | | } |
| | 68 | |
|
| | 69 | | public static void RemoveFromPlayerLoop() |
| 5 | 70 | | { |
| 7 | 71 | | if (!s_AddedToPlayerLoop) return; |
| 3 | 72 | | PlayerLoopSystem systemRoot = PlayerLoop.GetCurrentPlayerLoop(); |
| 3 | 73 | | systemRoot.RemoveSubSystemsOfTypeFromFirstSubSystemOfType(typeof(Update.ScriptRunDelayedTasks), |
| | 74 | | typeof(TaskDirectorSystem)); |
| 3 | 75 | | PlayerLoop.SetPlayerLoop(systemRoot); |
| 3 | 76 | | s_AddedToPlayerLoop = false; |
| 5 | 77 | | } |
| | 78 | |
|
| | 79 | | /// <summary> |
| | 80 | | /// Update the rate at which the <see cref="TaskDirectorSystem"/> updates the <see cref="TaskDirector"/>. |
| | 81 | | /// </summary> |
| | 82 | | /// <remarks> |
| | 83 | | /// This will not survive domain reload, please see <see cref="Config.TaskDirectorSystemTickRate"/>. |
| | 84 | | /// </remarks> |
| | 85 | | /// <param name="tickRate">The new tick rate.</param> |
| | 86 | | public static void SetTickRate(float tickRate) |
| 11 | 87 | | { |
| 11 | 88 | | s_TickRate = tickRate; |
| | 89 | | #if UNITY_EDITOR |
| 11 | 90 | | if (s_TickRate >= 0 && !Config.TaskDirectorSystem) |
| 1 | 91 | | { |
| 1 | 92 | | ManagedLog.Warning(LogCategory.GDX, "Tick rate set whilst TaskDirectorSystem has been configured off."); |
| 1 | 93 | | } |
| | 94 | | #endif // UNITY_EDITOR |
| | 95 | |
|
| | 96 | | // If for some reason the tick rate is set at runtime |
| | 97 | | #if !UNITY_EDITOR |
| | 98 | | if (tickRate >= 0) |
| | 99 | | { |
| | 100 | | AddToPlayerLoop(); |
| | 101 | | } |
| | 102 | | else |
| | 103 | | { |
| | 104 | | RemoveFromPlayerLoop(); |
| | 105 | | } |
| | 106 | | #endif // !UNITY_EDITOR |
| 11 | 107 | | } |
| | 108 | |
|
| | 109 | | /// <summary> |
| | 110 | | /// Sets up some default state for the <see cref="TaskDirectorSystem"/>. |
| | 111 | | /// </summary> |
| | 112 | | [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)] |
| | 113 | | static void Initialize() |
| 5 | 114 | | { |
| | 115 | | // Because invoke order is not deterministic, we cheat a bit and make sure that the core gets called |
| | 116 | | // regardless from this system. This pattern probably needs to happen in other cases like this. |
| 5 | 117 | | Core.InitializeOnMainThread(); |
| | 118 | |
|
| 5 | 119 | | if (Config.TaskDirectorSystem) |
| 3 | 120 | | { |
| 3 | 121 | | if (s_TickRate < 0) |
| 1 | 122 | | { |
| 1 | 123 | | s_TickRate = Config.TaskDirectorSystemTickRate; |
| 1 | 124 | | } |
| | 125 | |
|
| 3 | 126 | | AddToPlayerLoop(); |
| 3 | 127 | | } |
| 5 | 128 | | } |
| | 129 | |
|
| | 130 | | /// <summary> |
| | 131 | | /// The internal update call to the <see cref="TaskDirector"/>. |
| | 132 | | /// </summary> |
| | 133 | | static void PlayerLoopTick() |
| 2814 | 134 | | { |
| | 135 | | #if UNITY_EDITOR |
| 2814 | 136 | | if (!Application.isPlaying) |
| 1 | 137 | | { |
| 1 | 138 | | ManagedLog.Warning(LogCategory.GDX, "Unable to tick Task Director from PlayerLoop outside of PlayMode.") |
| 1 | 139 | | return; |
| | 140 | | } |
| | 141 | | #endif // UNITY_EDITOR |
| | 142 | |
|
| 2813 | 143 | | s_TimeSinceLastTick += Time.deltaTime; |
| 2813 | 144 | | if (s_TimeSinceLastTick < s_TickRate) |
| 2792 | 145 | | { |
| 2792 | 146 | | return; |
| | 147 | | } |
| | 148 | |
|
| 21 | 149 | | TaskDirector.Tick(); |
| 21 | 150 | | ticked?.Invoke(s_TimeSinceLastTick); |
| 21 | 151 | | s_TimeSinceLastTick = 0; |
| 2814 | 152 | | } |
| | 153 | | } |
| | 154 | | } |