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