| | 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 | | #if !UNITY_DOTSRUNTIME |
| | 6 | |
|
| | 7 | | using System.Collections.Generic; |
| | 8 | | using UnityEngine; |
| | 9 | |
|
| | 10 | | namespace GDX.Collections.Pooling |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// A time-slicing builder behaviour for <see cref="IManagedPool" />. |
| | 14 | | /// </summary> |
| | 15 | | /// <remarks>A demonstration of usage can be found in <see cref="GameObjectPool" />.</remarks> |
| | 16 | | /// <exception cref="UnsupportedRuntimeException">Not supported on DOTS Runtime.</exception> |
| | 17 | | public class ManagedPoolBuilder : MonoBehaviour |
| | 18 | | { |
| | 19 | | /// <summary> |
| | 20 | | /// The number of instantiates to make per frame. |
| | 21 | | /// </summary> |
| | 22 | | /// <remarks> |
| | 23 | | /// During defined loading periods this value could be increased for faster allocations, |
| | 24 | | /// and then returned to a much more performant value afterwards. |
| | 25 | | /// </remarks> |
| 1 | 26 | | public static int InstantiatesPerFrame = 5; |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// Should the <see cref="ManagedPoolBuilder" /> destroy itself when finished? |
| | 30 | | /// </summary> |
| 1 | 31 | | public static bool DestroyBuilderOnFinish = true; |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// A cached reference to the <see cref="GameObject" /> the builder created for itself. |
| | 35 | | /// </summary> |
| | 36 | | static GameObject s_BuilderObject; |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// A <see cref="List{T}" /> of <see cref="IManagedPool" /> which are being built out. |
| | 40 | | /// </summary> |
| 1 | 41 | | static readonly List<IManagedPool> k_TargetPools = new List<IManagedPool>(); |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// A cached numerical count of the number of <see cref="IManagedPool" /> contained in <see cref="k_TargetPo |
| | 45 | | /// </summary> |
| | 46 | | static int s_TargetPoolsCount; |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Unity's LateUpdate event. |
| | 50 | | /// </summary> |
| | 51 | | #pragma warning disable IDE0051 |
| | 52 | | // ReSharper disable UnusedMember.Local |
| | 53 | | void LateUpdate() |
| 0 | 54 | | { |
| | 55 | | // ReSharper disable once Unity.PerformanceCriticalCodeInvocation, Unity.ExpensiveCode |
| 0 | 56 | | Tick(); |
| | 57 | |
|
| | 58 | | // Do we have work to be done? |
| 0 | 59 | | if (s_TargetPoolsCount != 0) |
| 0 | 60 | | { |
| 0 | 61 | | return; |
| | 62 | | } |
| | 63 | |
|
| | 64 | | // Nothing left to do, get rid of myself |
| 0 | 65 | | s_BuilderObject = null; |
| 0 | 66 | | Destroy(gameObject); |
| 0 | 67 | | } |
| | 68 | | // ReSharper restore UnusedMember.Local |
| | 69 | | #pragma warning restore IDE0051 |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// Add an <see cref="IManagedPool" /> to the builder to be built out. |
| | 73 | | /// </summary> |
| | 74 | | /// <param name="targetManagedPool">The <see cref="IManagedPool" /> to build out.</param> |
| | 75 | | public static void AddManagedPool(IManagedPool targetManagedPool) |
| 14 | 76 | | { |
| 14 | 77 | | if (k_TargetPools.Contains(targetManagedPool)) |
| 0 | 78 | | { |
| 0 | 79 | | return; |
| | 80 | | } |
| | 81 | |
|
| 14 | 82 | | k_TargetPools.Add(targetManagedPool); |
| 14 | 83 | | s_TargetPoolsCount++; |
| | 84 | |
|
| | 85 | | // We already have a builder, no need to make one. |
| 14 | 86 | | if (s_BuilderObject != null) |
| 0 | 87 | | { |
| 0 | 88 | | return; |
| | 89 | | } |
| | 90 | |
|
| 14 | 91 | | s_BuilderObject = new GameObject("GDX.GameObjectPoolBuilder"); |
| 14 | 92 | | s_BuilderObject.AddComponent<ManagedPoolBuilder>(); |
| 14 | 93 | | } |
| | 94 | |
|
| | 95 | | /// <summary> |
| | 96 | | /// Remove an <see cref="IManagedPool" /> from the builder. |
| | 97 | | /// </summary> |
| | 98 | | /// <param name="targetManagedPool">The <see cref="IManagedPool" /> to be removed.</param> |
| | 99 | | public static void RemoveManagedPool(IManagedPool targetManagedPool) |
| 14 | 100 | | { |
| 14 | 101 | | if (!k_TargetPools.Contains(targetManagedPool)) |
| 0 | 102 | | { |
| 0 | 103 | | return; |
| | 104 | | } |
| | 105 | |
|
| 14 | 106 | | k_TargetPools.Remove(targetManagedPool); |
| 14 | 107 | | s_TargetPoolsCount--; |
| | 108 | |
|
| | 109 | | // We still have pools, no sense destroying anything yet |
| 14 | 110 | | if (s_TargetPoolsCount > 0 || !DestroyBuilderOnFinish) |
| 0 | 111 | | { |
| 0 | 112 | | return; |
| | 113 | | } |
| | 114 | | #if UNITY_EDITOR |
| 14 | 115 | | if (Application.isPlaying) |
| 0 | 116 | | { |
| 0 | 117 | | Destroy(s_BuilderObject); |
| 0 | 118 | | } |
| | 119 | | else |
| 14 | 120 | | { |
| 14 | 121 | | DestroyImmediate(s_BuilderObject); |
| 14 | 122 | | } |
| | 123 | | #else |
| | 124 | | Destroy(s_BuilderObject); |
| | 125 | | #endif // UNITY_EDITOR |
| 14 | 126 | | s_BuilderObject = null; |
| 14 | 127 | | } |
| | 128 | |
|
| | 129 | | /// <summary> |
| | 130 | | /// Extracted tick update for the builder; creating a limited number of items per tick. |
| | 131 | | /// </summary> |
| | 132 | | static void Tick() |
| 0 | 133 | | { |
| 0 | 134 | | int spawnsThisUpdate = 0; |
| | 135 | |
|
| 0 | 136 | | for (int i = s_TargetPoolsCount - 1; i >= 0; i--) |
| 0 | 137 | | { |
| 0 | 138 | | if (k_TargetPools[i] == null) |
| 0 | 139 | | { |
| 0 | 140 | | continue; |
| | 141 | | } |
| | 142 | |
|
| 0 | 143 | | if (k_TargetPools[i].HasMinimumPooledItems()) |
| 0 | 144 | | { |
| 0 | 145 | | k_TargetPools.RemoveAt(i); |
| 0 | 146 | | s_TargetPoolsCount--; |
| 0 | 147 | | } |
| | 148 | | else |
| 0 | 149 | | { |
| | 150 | | // Build Item |
| | 151 | | // ReSharper disable once Unity.PerformanceCriticalCodeInvocation |
| 0 | 152 | | k_TargetPools[i].CreateItem(); |
| | 153 | |
|
| 0 | 154 | | spawnsThisUpdate++; |
| 0 | 155 | | if (spawnsThisUpdate > InstantiatesPerFrame) |
| 0 | 156 | | { |
| 0 | 157 | | break; |
| | 158 | | } |
| 0 | 159 | | } |
| 0 | 160 | | } |
| 0 | 161 | | } |
| | 162 | | } |
| | 163 | | } |
| | 164 | | #endif // !UNITY_DOTSRUNTIME |