| | | 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.Runtime.CompilerServices; |
| | | 8 | | using UnityEngine; |
| | | 9 | | |
| | | 10 | | namespace GDX.Collections.Pooling |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// <see cref="GameObject" /> based functionality extending the <see cref="SimpleListManagedPool" /> to better s |
| | | 14 | | /// <see cref="GameObject" /> patterns. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <exception cref="UnsupportedRuntimeException">Not supported on DOTS Runtime.</exception> |
| | | 17 | | [VisualScriptingCompatible(1)] |
| | | 18 | | public static class GameObjectPool |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// The <see cref="SimpleListManagedPool" /> flags index used to determine if the object which is used to cr |
| | | 22 | | /// objects |
| | | 23 | | /// has the <see cref="IGameObjectPoolItem" /> interface on a root component. |
| | | 24 | | /// </summary> |
| | | 25 | | const int k_HasInterfaceFlag = 5; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Create a <see cref="GameObject" /> based <see cref="SimpleListManagedPool" /> for the provided |
| | | 29 | | /// <paramref name="gameObject" />. |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="gameObject">The object which going to be cloned.</param> |
| | | 32 | | /// <param name="parent">The container object.</param> |
| | | 33 | | /// <param name="minimumObjects">The minimum number of objects to be pooled.</param> |
| | | 34 | | /// <param name="maximumObjects">The maximum number of objects to be pooled.</param> |
| | | 35 | | /// <param name="allowCreateMore">Can more items be created as needed when starved for items?</param> |
| | | 36 | | /// <param name="allowReuseWhenCapped">Should we reuse oldest items when starving for items?</param> |
| | | 37 | | /// <param name="allowManagedTearDown">Does the pool allow a managed tear down event call?</param> |
| | | 38 | | public static IManagedPool CreatePool( |
| | | 39 | | GameObject gameObject, |
| | | 40 | | Transform parent, |
| | | 41 | | int minimumObjects = 10, |
| | | 42 | | int maximumObjects = 50, |
| | | 43 | | bool allowCreateMore = true, |
| | | 44 | | bool allowReuseWhenCapped = false, |
| | | 45 | | bool allowManagedTearDown = false) |
| | 14 | 46 | | { |
| | | 47 | | // Create our new pool |
| | 14 | 48 | | SimpleListManagedPool newGameManagedPool = new SimpleListManagedPool( |
| | | 49 | | gameObject, |
| | | 50 | | CreateItem, |
| | | 51 | | minimumObjects, |
| | | 52 | | maximumObjects, |
| | | 53 | | parent, |
| | | 54 | | false, |
| | | 55 | | allowCreateMore, |
| | | 56 | | allowReuseWhenCapped, |
| | | 57 | | allowManagedTearDown) |
| | | 58 | | { |
| | | 59 | | Flags = { [k_HasInterfaceFlag] = gameObject.GetComponent<IGameObjectPoolItem>() != null } |
| | | 60 | | }; |
| | | 61 | | |
| | 14 | 62 | | ManagedPoolBuilder.AddManagedPool(newGameManagedPool); |
| | | 63 | | |
| | 14 | 64 | | newGameManagedPool.OutCachedCount = 0; |
| | | 65 | | |
| | | 66 | | // Assign actions |
| | 14 | 67 | | newGameManagedPool.destroyedItem += OnDestroyItemAction; |
| | 14 | 68 | | newGameManagedPool.tearingDown += OnTearDownAction; |
| | 14 | 69 | | newGameManagedPool.spawnedItem += OnSpawnedFromPoolAction; |
| | 14 | 70 | | newGameManagedPool.returnedItem += OnReturnedToPoolAction; |
| | | 71 | | |
| | 14 | 72 | | return newGameManagedPool; |
| | 14 | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Get the next available item from the <paramref name="pool" />. |
| | | 77 | | /// </summary> |
| | | 78 | | /// <param name="pool"> |
| | | 79 | | /// The <see cref="SimpleListManagedPool" /> created with <see cref="GameObjectPool" /> to pull an item |
| | | 80 | | /// from. |
| | | 81 | | /// </param> |
| | | 82 | | /// <param name="triggerOnSpawnedFromPool"> |
| | | 83 | | /// Should the <see cref="OnSpawnedFromPoolAction" /> be called when getting this |
| | | 84 | | /// item. |
| | | 85 | | /// </param> |
| | | 86 | | /// <returns>A <see cref="GameObject" /> from the <see cref="SimpleListManagedPool" />, or null if no item is av |
| | | 87 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 88 | | public static GameObject Get(SimpleListManagedPool pool, bool triggerOnSpawnedFromPool = true) |
| | 4 | 89 | | { |
| | | 90 | | // Pull |
| | 4 | 91 | | object item = pool.Get(false); |
| | 4 | 92 | | if (item == null) |
| | 0 | 93 | | { |
| | 0 | 94 | | return null; |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | // Actions |
| | 4 | 98 | | if (triggerOnSpawnedFromPool) |
| | 1 | 99 | | { |
| | 1 | 100 | | OnSpawnedFromPoolAction(pool, item); |
| | 1 | 101 | | } |
| | | 102 | | |
| | | 103 | | // Return |
| | 4 | 104 | | return item is IGameObjectPoolItem gameObjectPoolItem |
| | | 105 | | ? gameObjectPoolItem.GetGameObject() |
| | | 106 | | : (GameObject)item; |
| | 4 | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Get the next available item from the <paramref name="pool" /> and parent it to a <see cref="Transform" / |
| | | 111 | | /// </summary> |
| | | 112 | | /// <param name="pool"> |
| | | 113 | | /// The <see cref="SimpleListManagedPool" /> created with <see cref="GameObjectPool" /> to pull an item |
| | | 114 | | /// from. |
| | | 115 | | /// </param> |
| | | 116 | | /// <param name="parent">The transform parent on the item pulled from the <see cref="SimpleListManagedPool" />.< |
| | | 117 | | /// <param name="worldPositionStays"> |
| | | 118 | | /// Ensure that the world position of the item pulled from the |
| | | 119 | | /// <see cref="SimpleListManagedPool" /> remains the same through parenting. |
| | | 120 | | /// </param> |
| | | 121 | | /// <param name="zeroLocalPosition"> |
| | | 122 | | /// Set the local position of the item pulled from the <see cref="SimpleListManagedPool" /> |
| | | 123 | | /// to being <see cref="Vector3.zero" /> after parenting. |
| | | 124 | | /// </param> |
| | | 125 | | /// <param name="triggerOnSpawnedFromPool"> |
| | | 126 | | /// Should the <see cref="OnSpawnedFromPoolAction" /> be called when getting this |
| | | 127 | | /// item. |
| | | 128 | | /// </param> |
| | | 129 | | /// <returns>A <see cref="GameObject" /> from the <see cref="SimpleListManagedPool" />, or null if no item is av |
| | | 130 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 131 | | public static GameObject Get(SimpleListManagedPool pool, Transform parent, bool worldPositionStays = false, |
| | | 132 | | bool zeroLocalPosition = true, bool triggerOnSpawnedFromPool = true) |
| | 2 | 133 | | { |
| | | 134 | | // Pull |
| | 2 | 135 | | object item = pool.Get(false); |
| | 2 | 136 | | if (item == null) |
| | 0 | 137 | | { |
| | 0 | 138 | | return null; |
| | | 139 | | } |
| | | 140 | | |
| | 2 | 141 | | GameObject returnObject = item is IGameObjectPoolItem gameObjectPoolItem |
| | | 142 | | ? gameObjectPoolItem.GetGameObject() |
| | | 143 | | : (GameObject)item; |
| | 2 | 144 | | if (returnObject == null) |
| | 0 | 145 | | { |
| | 0 | 146 | | return null; |
| | | 147 | | } |
| | | 148 | | |
| | | 149 | | // Translate |
| | 2 | 150 | | Transform transform = returnObject.transform; |
| | 2 | 151 | | transform.SetParent(parent, worldPositionStays); |
| | 2 | 152 | | if (!worldPositionStays && zeroLocalPosition) |
| | 1 | 153 | | { |
| | 1 | 154 | | transform.localPosition = Vector3.zero; |
| | 1 | 155 | | } |
| | | 156 | | |
| | | 157 | | // Actions |
| | 2 | 158 | | if (triggerOnSpawnedFromPool) |
| | 2 | 159 | | { |
| | 2 | 160 | | OnSpawnedFromPoolAction(pool, item); |
| | 2 | 161 | | } |
| | | 162 | | |
| | | 163 | | // Return |
| | 2 | 164 | | return returnObject; |
| | 2 | 165 | | } |
| | | 166 | | |
| | | 167 | | /// <summary> |
| | | 168 | | /// Get the next available item from the <paramref name="pool" />, parent it to a <see cref="Transform" />, |
| | | 169 | | /// set it's local position and rotation. |
| | | 170 | | /// </summary> |
| | | 171 | | /// <param name="pool"> |
| | | 172 | | /// The <see cref="SimpleListManagedPool" /> created with <see cref="GameObjectPool" /> to pull an item |
| | | 173 | | /// from. |
| | | 174 | | /// </param> |
| | | 175 | | /// <param name="parent">The transform parent on the item pulled from the <see cref="SimpleListManagedPool" />.< |
| | | 176 | | /// <param name="localPosition"> |
| | | 177 | | /// The local position to set on the item pulled from the <see cref="SimpleListManagedPool" /> |
| | | 178 | | /// after parenting. |
| | | 179 | | /// </param> |
| | | 180 | | /// <param name="localRotation"> |
| | | 181 | | /// The local rotation to set on the item pulled from the <see cref="SimpleListManagedPool" /> |
| | | 182 | | /// after parenting. |
| | | 183 | | /// </param> |
| | | 184 | | /// <param name="triggerOnSpawnedFromPool"> |
| | | 185 | | /// Should the <see cref="OnSpawnedFromPoolAction" /> be called when getting this |
| | | 186 | | /// item. |
| | | 187 | | /// </param> |
| | | 188 | | /// <returns>A <see cref="GameObject" /> from the <see cref="SimpleListManagedPool" />, or null if no item is av |
| | | 189 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 190 | | public static GameObject Get(SimpleListManagedPool pool, Transform parent, Vector3 localPosition, |
| | | 191 | | Quaternion localRotation, bool triggerOnSpawnedFromPool = true) |
| | 1 | 192 | | { |
| | | 193 | | // Pull |
| | 1 | 194 | | object item = pool.Get(false); |
| | 1 | 195 | | if (item == null) |
| | 0 | 196 | | { |
| | 0 | 197 | | return null; |
| | | 198 | | } |
| | | 199 | | |
| | 1 | 200 | | GameObject returnObject = item is IGameObjectPoolItem gameObjectPoolItem |
| | | 201 | | ? gameObjectPoolItem.GetGameObject() |
| | | 202 | | : (GameObject)item; |
| | 1 | 203 | | if (returnObject == null) |
| | 0 | 204 | | { |
| | 0 | 205 | | return null; |
| | | 206 | | } |
| | | 207 | | |
| | | 208 | | // Translate |
| | 1 | 209 | | Transform transform = returnObject.transform; |
| | 1 | 210 | | transform.SetParent(parent); |
| | 1 | 211 | | transform.localPosition = localPosition; |
| | 1 | 212 | | transform.localRotation = localRotation; |
| | | 213 | | |
| | | 214 | | // Actions |
| | 1 | 215 | | if (triggerOnSpawnedFromPool) |
| | 1 | 216 | | { |
| | 1 | 217 | | OnSpawnedFromPoolAction(pool, item); |
| | 1 | 218 | | } |
| | | 219 | | |
| | | 220 | | // Return |
| | 1 | 221 | | return returnObject; |
| | 1 | 222 | | } |
| | | 223 | | |
| | | 224 | | /// <summary> |
| | | 225 | | /// Get the next available item from the <paramref name="pool" />, parent it to a <see cref="Transform" />, |
| | | 226 | | /// setting it's local position and where it is looking. |
| | | 227 | | /// </summary> |
| | | 228 | | /// <param name="pool"> |
| | | 229 | | /// The <see cref="SimpleListManagedPool" /> created with <see cref="GameObjectPool" /> to pull an item |
| | | 230 | | /// from. |
| | | 231 | | /// </param> |
| | | 232 | | /// <param name="parent">The transform parent on the item pulled from the <see cref="SimpleListManagedPool" />.< |
| | | 233 | | /// <param name="localPosition"> |
| | | 234 | | /// The local position to set on the item pulled from the <see cref="SimpleListManagedPool" /> |
| | | 235 | | /// after parenting. |
| | | 236 | | /// </param> |
| | | 237 | | /// <param name="worldLookAtPosition"> |
| | | 238 | | /// The world position to have the item pulled from the |
| | | 239 | | /// <see cref="SimpleListManagedPool" /> look at |
| | | 240 | | /// </param> |
| | | 241 | | /// <param name="triggerOnSpawnedFromPool"> |
| | | 242 | | /// Should the <see cref="OnSpawnedFromPoolAction" /> be called when getting this |
| | | 243 | | /// item. |
| | | 244 | | /// </param> |
| | | 245 | | /// <returns>A <see cref="GameObject" /> from the <see cref="SimpleListManagedPool" />, or null if no item is av |
| | | 246 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 247 | | public static GameObject Get(SimpleListManagedPool pool, Transform parent, Vector3 localPosition, |
| | | 248 | | Vector3 worldLookAtPosition, bool triggerOnSpawnedFromPool = true) |
| | 1 | 249 | | { |
| | | 250 | | // Pull |
| | 1 | 251 | | object item = pool.Get(false); |
| | 1 | 252 | | if (item == null) |
| | 0 | 253 | | { |
| | 0 | 254 | | return null; |
| | | 255 | | } |
| | | 256 | | |
| | 1 | 257 | | GameObject returnObject = item is IGameObjectPoolItem gameObjectPoolItem |
| | | 258 | | ? gameObjectPoolItem.GetGameObject() |
| | | 259 | | : (GameObject)item; |
| | 1 | 260 | | if (returnObject == null) |
| | 0 | 261 | | { |
| | 0 | 262 | | return null; |
| | | 263 | | } |
| | | 264 | | |
| | | 265 | | // Translate |
| | 1 | 266 | | Transform transform = returnObject.transform; |
| | 1 | 267 | | transform.SetParent(parent); |
| | 1 | 268 | | transform.localPosition = localPosition; |
| | 1 | 269 | | transform.LookAt(worldLookAtPosition); |
| | | 270 | | |
| | | 271 | | // Actions |
| | 1 | 272 | | if (triggerOnSpawnedFromPool) |
| | 1 | 273 | | { |
| | 1 | 274 | | OnSpawnedFromPoolAction(pool, item); |
| | 1 | 275 | | } |
| | | 276 | | |
| | | 277 | | // Return |
| | 1 | 278 | | return returnObject; |
| | 1 | 279 | | } |
| | | 280 | | |
| | | 281 | | /// <summary> |
| | | 282 | | /// Get the next available item from the <paramref name="pool" />, and set its world position and where it i |
| | | 283 | | /// </summary> |
| | | 284 | | /// <param name="pool"> |
| | | 285 | | /// The <see cref="SimpleListManagedPool" /> created with <see cref="GameObjectPool" /> to pull an item |
| | | 286 | | /// from. |
| | | 287 | | /// </param> |
| | | 288 | | /// <param name="worldPosition">The world position to set on the item pulled from the <see cref="SimpleListManag |
| | | 289 | | /// <param name="worldLookAtPosition"> |
| | | 290 | | /// The world position to have the item pulled from the |
| | | 291 | | /// <see cref="SimpleListManagedPool" /> look at |
| | | 292 | | /// </param> |
| | | 293 | | /// <param name="triggerOnSpawnedFromPool"> |
| | | 294 | | /// Should the <see cref="OnSpawnedFromPoolAction" /> be called when getting this |
| | | 295 | | /// item. |
| | | 296 | | /// </param> |
| | | 297 | | /// <returns>A <see cref="GameObject" /> from the <see cref="SimpleListManagedPool" />, or null if no item is av |
| | | 298 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 299 | | public static GameObject Get(SimpleListManagedPool pool, Vector3 worldPosition, Vector3 worldLookAtPosition, |
| | | 300 | | bool triggerOnSpawnedFromPool = true) |
| | 1 | 301 | | { |
| | | 302 | | // Pull |
| | 1 | 303 | | object item = pool.Get(false); |
| | 1 | 304 | | if (item == null) |
| | 0 | 305 | | { |
| | 0 | 306 | | return null; |
| | | 307 | | } |
| | | 308 | | |
| | 1 | 309 | | GameObject returnObject = item is IGameObjectPoolItem gameObjectPoolItem |
| | | 310 | | ? gameObjectPoolItem.GetGameObject() |
| | | 311 | | : (GameObject)item; |
| | 1 | 312 | | if (returnObject == null) |
| | 0 | 313 | | { |
| | 0 | 314 | | return null; |
| | | 315 | | } |
| | | 316 | | |
| | | 317 | | // Translate |
| | 1 | 318 | | Transform transform = returnObject.transform; |
| | 1 | 319 | | transform.position = worldPosition; |
| | 1 | 320 | | transform.LookAt(worldLookAtPosition); |
| | | 321 | | |
| | | 322 | | // Actions |
| | 1 | 323 | | if (triggerOnSpawnedFromPool) |
| | 1 | 324 | | { |
| | 1 | 325 | | OnSpawnedFromPoolAction(pool, item); |
| | 1 | 326 | | } |
| | | 327 | | |
| | | 328 | | // Return |
| | 1 | 329 | | return returnObject; |
| | 1 | 330 | | } |
| | | 331 | | |
| | | 332 | | /// <summary> |
| | | 333 | | /// Get the next available item from the <paramref name="pool" />, and set its world position and rotation. |
| | | 334 | | /// </summary> |
| | | 335 | | /// <param name="pool"> |
| | | 336 | | /// The <see cref="SimpleListManagedPool" /> created with <see cref="GameObjectPool" /> to pull an item |
| | | 337 | | /// from. |
| | | 338 | | /// </param> |
| | | 339 | | /// <param name="worldPosition">The world position to set on the item pulled from the <see cref="SimpleListManag |
| | | 340 | | /// <param name="worldRotation">The world rotation to set on the item pulled from the <see cref="SimpleListManag |
| | | 341 | | /// <param name="triggerOnSpawnedFromPool"> |
| | | 342 | | /// Should the <see cref="OnSpawnedFromPoolAction" /> be called when getting this |
| | | 343 | | /// item. |
| | | 344 | | /// </param> |
| | | 345 | | /// <returns>A <see cref="GameObject" /> from the <see cref="SimpleListManagedPool" />, or null if no item is av |
| | | 346 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 347 | | public static GameObject Get(SimpleListManagedPool pool, Vector3 worldPosition, Quaternion worldRotation, |
| | | 348 | | bool triggerOnSpawnedFromPool = true) |
| | 1 | 349 | | { |
| | | 350 | | // Pull |
| | 1 | 351 | | object item = pool.Get(false); |
| | 1 | 352 | | if (item == null) |
| | 0 | 353 | | { |
| | 0 | 354 | | return null; |
| | | 355 | | } |
| | | 356 | | |
| | 1 | 357 | | GameObject returnObject = item is IGameObjectPoolItem gameObjectPoolItem |
| | | 358 | | ? gameObjectPoolItem.GetGameObject() |
| | | 359 | | : (GameObject)item; |
| | 1 | 360 | | if (returnObject == null) |
| | 0 | 361 | | { |
| | 0 | 362 | | return null; |
| | | 363 | | } |
| | | 364 | | |
| | | 365 | | // Translate |
| | 1 | 366 | | Transform transform = returnObject.transform; |
| | 1 | 367 | | transform.SetPositionAndRotation(worldPosition, worldRotation); |
| | | 368 | | |
| | | 369 | | // Actions |
| | 1 | 370 | | if (triggerOnSpawnedFromPool) |
| | 1 | 371 | | { |
| | 1 | 372 | | OnSpawnedFromPoolAction(pool, item); |
| | 1 | 373 | | } |
| | | 374 | | |
| | | 375 | | // Return |
| | 1 | 376 | | return returnObject; |
| | 1 | 377 | | } |
| | | 378 | | |
| | | 379 | | /// <summary> |
| | | 380 | | /// Get the next available item from the <paramref name="pool" />, and set its world position and rotation a |
| | | 381 | | /// parenting. |
| | | 382 | | /// </summary> |
| | | 383 | | /// <param name="pool"> |
| | | 384 | | /// The <see cref="SimpleListManagedPool" /> created with <see cref="GameObjectPool" /> to pull an item |
| | | 385 | | /// from. |
| | | 386 | | /// </param> |
| | | 387 | | /// <param name="worldPosition"> |
| | | 388 | | /// The world position to set on the item pulled from the <see cref="SimpleListManagedPool" /> |
| | | 389 | | /// after parenting. |
| | | 390 | | /// </param> |
| | | 391 | | /// <param name="worldRotation"> |
| | | 392 | | /// The world rotation to set on the item pulled from the <see cref="SimpleListManagedPool" /> |
| | | 393 | | /// after parenting. |
| | | 394 | | /// </param> |
| | | 395 | | /// <param name="parent">The transform parent on the item pulled from the <see cref="SimpleListManagedPool" />.< |
| | | 396 | | /// <param name="triggerOnSpawnedFromPool"> |
| | | 397 | | /// Should the <see cref="OnSpawnedFromPoolAction" /> be called when getting this |
| | | 398 | | /// item. |
| | | 399 | | /// </param> |
| | | 400 | | /// <returns>A <see cref="GameObject" /> from the <see cref="SimpleListManagedPool" />, or null if no item is av |
| | | 401 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 402 | | public static GameObject Get(SimpleListManagedPool pool, Vector3 worldPosition, Quaternion worldRotation, |
| | | 403 | | Transform parent, bool triggerOnSpawnedFromPool = true) |
| | 1 | 404 | | { |
| | | 405 | | // Pull |
| | 1 | 406 | | object item = pool.Get(false); |
| | 1 | 407 | | if (item == null) |
| | 0 | 408 | | { |
| | 0 | 409 | | return null; |
| | | 410 | | } |
| | | 411 | | |
| | 1 | 412 | | GameObject returnObject = item is IGameObjectPoolItem gameObjectPoolItem |
| | | 413 | | ? gameObjectPoolItem.GetGameObject() |
| | | 414 | | : (GameObject)item; |
| | 1 | 415 | | if (returnObject == null) |
| | 0 | 416 | | { |
| | 0 | 417 | | return null; |
| | | 418 | | } |
| | | 419 | | |
| | | 420 | | // Translate |
| | 1 | 421 | | Transform transform = returnObject.transform; |
| | 1 | 422 | | transform.SetPositionAndRotation(worldPosition, worldRotation); |
| | 1 | 423 | | transform.SetParent(parent, true); |
| | | 424 | | |
| | | 425 | | // Actions |
| | 1 | 426 | | if (triggerOnSpawnedFromPool) |
| | 1 | 427 | | { |
| | 1 | 428 | | OnSpawnedFromPoolAction(pool, item); |
| | 1 | 429 | | } |
| | | 430 | | |
| | | 431 | | // Return |
| | 1 | 432 | | return returnObject; |
| | 1 | 433 | | } |
| | | 434 | | |
| | | 435 | | /// <summary> |
| | | 436 | | /// Get the next available item from the <paramref name="pool" />, and set its world position and look at po |
| | | 437 | | /// after parenting. |
| | | 438 | | /// </summary> |
| | | 439 | | /// <param name="pool"> |
| | | 440 | | /// The <see cref="SimpleListManagedPool" /> created with <see cref="GameObjectPool" /> to pull an item |
| | | 441 | | /// from. |
| | | 442 | | /// </param> |
| | | 443 | | /// <param name="worldPosition"> |
| | | 444 | | /// The world position to set on the item pulled from the <see cref="SimpleListManagedPool" /> |
| | | 445 | | /// after parenting. |
| | | 446 | | /// </param> |
| | | 447 | | /// <param name="worldLookAtPosition"> |
| | | 448 | | /// The world position to have the item pulled from the |
| | | 449 | | /// <see cref="SimpleListManagedPool" /> look at after parenting. |
| | | 450 | | /// </param> |
| | | 451 | | /// <param name="parent">The transform parent on the item pulled from the <see cref="SimpleListManagedPool" />.< |
| | | 452 | | /// <param name="triggerOnSpawnedFromPool"> |
| | | 453 | | /// Should the <see cref="OnSpawnedFromPoolAction" /> be called when getting this |
| | | 454 | | /// item. |
| | | 455 | | /// </param> |
| | | 456 | | /// <returns>A <see cref="GameObject" /> from the <see cref="SimpleListManagedPool" />, or null if no item is av |
| | | 457 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 458 | | public static GameObject Get(SimpleListManagedPool pool, Vector3 worldPosition, Vector3 worldLookAtPosition, |
| | | 459 | | Transform parent, bool triggerOnSpawnedFromPool = true) |
| | 1 | 460 | | { |
| | | 461 | | // Pull |
| | 1 | 462 | | object item = pool.Get(false); |
| | 1 | 463 | | if (item == null) |
| | 0 | 464 | | { |
| | 0 | 465 | | return null; |
| | | 466 | | } |
| | | 467 | | |
| | 1 | 468 | | GameObject returnObject = item is IGameObjectPoolItem gameObjectPoolItem |
| | | 469 | | ? gameObjectPoolItem.GetGameObject() |
| | | 470 | | : (GameObject)item; |
| | 1 | 471 | | if (returnObject == null) |
| | 0 | 472 | | { |
| | 0 | 473 | | return null; |
| | | 474 | | } |
| | | 475 | | |
| | | 476 | | // Translate |
| | 1 | 477 | | Transform transform = returnObject.transform; |
| | 1 | 478 | | transform.position = worldPosition; |
| | 1 | 479 | | transform.SetParent(parent, true); |
| | 1 | 480 | | transform.LookAt(worldLookAtPosition); |
| | | 481 | | |
| | | 482 | | // Actions |
| | 1 | 483 | | if (triggerOnSpawnedFromPool) |
| | 1 | 484 | | { |
| | 1 | 485 | | OnSpawnedFromPoolAction(pool, item); |
| | 1 | 486 | | } |
| | | 487 | | |
| | | 488 | | // Return |
| | 1 | 489 | | return returnObject; |
| | 1 | 490 | | } |
| | | 491 | | |
| | | 492 | | /// <summary> |
| | | 493 | | /// Gets a pool for the <paramref name="gameObject" />, or creates a new <see cref="SimpleListManagedPool" / |
| | | 494 | | /// </summary> |
| | | 495 | | /// <param name="gameObject">The object which going to be cloned.</param> |
| | | 496 | | /// <param name="parent">The container object.</param> |
| | | 497 | | /// <param name="minimumObjects">The minimum number of objects to be pooled.</param> |
| | | 498 | | /// <param name="maximumObjects">The maximum number of objects to be pooled.</param> |
| | | 499 | | /// <param name="allowCreateMore">Can more items be created as needed when starved for items?</param> |
| | | 500 | | /// <param name="allowReuseWhenCapped">Should we reuse oldest items when starving for items?</param> |
| | | 501 | | /// <param name="allowManagedTearDown">Does the pool allow a managed tear down event call?</param> |
| | | 502 | | public static IManagedPool GetOrCreatePool( |
| | | 503 | | GameObject gameObject, |
| | | 504 | | Transform parent, |
| | | 505 | | int minimumObjects = 10, |
| | | 506 | | int maximumObjects = 50, |
| | | 507 | | bool allowCreateMore = true, |
| | | 508 | | bool allowReuseWhenCapped = false, |
| | | 509 | | bool allowManagedTearDown = false) |
| | 14 | 510 | | { |
| | 14 | 511 | | if (ManagedPools.TryGetFirstPool(gameObject, out IManagedPool checkPool)) |
| | 0 | 512 | | { |
| | 0 | 513 | | return checkPool; |
| | | 514 | | } |
| | | 515 | | |
| | 14 | 516 | | return CreatePool(gameObject, parent, minimumObjects, maximumObjects, allowCreateMore, allowReuseWhenCapped, |
| | | 517 | | allowManagedTearDown); |
| | 14 | 518 | | } |
| | | 519 | | |
| | | 520 | | /// <summary> |
| | | 521 | | /// Create a new item for the <paramref name="pool" />. |
| | | 522 | | /// </summary> |
| | | 523 | | /// <param name="pool">The <see cref="SimpleListManagedPool" /> to create an item for, and assign too.</param> |
| | | 524 | | /// <returns>The newly created item.</returns> |
| | | 525 | | static object CreateItem(SimpleListManagedPool pool) |
| | 12 | 526 | | { |
| | 12 | 527 | | GameObject spawnedObject = |
| | | 528 | | Object.Instantiate((GameObject)pool.BaseObject, (Transform)pool.ContainerObject, true); |
| | | 529 | | |
| | 12 | 530 | | if (pool.Flags[k_HasInterfaceFlag]) |
| | 9 | 531 | | { |
| | | 532 | | // The old swap for the interface instead of the GameObject |
| | | 533 | | // ReSharper disable once Unity.PerformanceCriticalCodeInvocation, Unity.ExpensiveCode |
| | 9 | 534 | | IGameObjectPoolItem item = spawnedObject.GetComponent<IGameObjectPoolItem>(); |
| | 9 | 535 | | item.SetParentPool(pool); |
| | 9 | 536 | | item.OnReturnedToPool(); |
| | 9 | 537 | | pool.InItems.AddWithExpandCheck(item); |
| | 9 | 538 | | pool.InCachedCount++; |
| | 9 | 539 | | return item; |
| | | 540 | | } |
| | | 541 | | |
| | 3 | 542 | | spawnedObject.SetActive(false); |
| | 3 | 543 | | pool.InItems.AddWithExpandCheck(spawnedObject); |
| | 3 | 544 | | pool.InCachedCount++; |
| | 3 | 545 | | return spawnedObject; |
| | 12 | 546 | | } |
| | | 547 | | |
| | | 548 | | /// <summary> |
| | | 549 | | /// The subscribed action called when an item is requested to be destroyed.. |
| | | 550 | | /// </summary> |
| | | 551 | | /// <param name="item">The item being destroyed.</param> |
| | | 552 | | static void OnDestroyItemAction(object item) |
| | 12 | 553 | | { |
| | 12 | 554 | | if (item == null) |
| | 0 | 555 | | { |
| | 0 | 556 | | return; |
| | | 557 | | } |
| | | 558 | | |
| | | 559 | | Object unityObject; |
| | 12 | 560 | | if (item is IGameObjectPoolItem poolItem && poolItem.IsValidItem()) |
| | 9 | 561 | | { |
| | 9 | 562 | | unityObject = poolItem.GetGameObject(); |
| | 9 | 563 | | } |
| | | 564 | | else |
| | 3 | 565 | | { |
| | 3 | 566 | | unityObject = (Object)item; |
| | 3 | 567 | | } |
| | | 568 | | |
| | 12 | 569 | | if (unityObject != null) |
| | 12 | 570 | | { |
| | | 571 | | #if UNITY_EDITOR |
| | 12 | 572 | | if (Application.isPlaying) |
| | 0 | 573 | | { |
| | 0 | 574 | | Object.Destroy(unityObject, 0f); |
| | 0 | 575 | | } |
| | | 576 | | else |
| | 12 | 577 | | { |
| | 12 | 578 | | Object.DestroyImmediate(unityObject); |
| | 12 | 579 | | } |
| | | 580 | | #else |
| | | 581 | | Object.Destroy(unityObject, 0f); |
| | | 582 | | #endif // UNITY_EDITOR |
| | 12 | 583 | | } |
| | 12 | 584 | | } |
| | | 585 | | |
| | | 586 | | /// <summary> |
| | | 587 | | /// The subscribed action called when an item is returned to the <paramref name="pool" />. |
| | | 588 | | /// </summary> |
| | | 589 | | /// <param name="pool">The <see cref="SimpleListManagedPool" /> which the <paramref name="item" /> is being retu |
| | | 590 | | /// <param name="item">The item being returned to the <paramref name="pool" />.</param> |
| | | 591 | | static void OnReturnedToPoolAction(SimpleListManagedPool pool, object item) |
| | 12 | 592 | | { |
| | 12 | 593 | | if (!pool.Flags[k_HasInterfaceFlag]) |
| | 3 | 594 | | { |
| | 3 | 595 | | (item as GameObject)?.SetActive(false); |
| | 3 | 596 | | return; |
| | | 597 | | } |
| | | 598 | | |
| | 9 | 599 | | (item as IGameObjectPoolItem)?.OnReturnedToPool(); |
| | 12 | 600 | | } |
| | | 601 | | |
| | | 602 | | /// <summary> |
| | | 603 | | /// The subscribed action called when an item is spawned from the <paramref name="pool" />. |
| | | 604 | | /// </summary> |
| | | 605 | | /// <param name="pool">The <see cref="SimpleListManagedPool" /> which has had the <paramref name="item" /> spawn |
| | | 606 | | /// <param name="item">The spawned item.</param> |
| | | 607 | | static void OnSpawnedFromPoolAction(SimpleListManagedPool pool, object item) |
| | 9 | 608 | | { |
| | 9 | 609 | | if (!pool.Flags[k_HasInterfaceFlag]) |
| | 2 | 610 | | { |
| | 2 | 611 | | (item as GameObject)?.SetActive(true); |
| | 2 | 612 | | return; |
| | | 613 | | } |
| | | 614 | | |
| | 7 | 615 | | (item as IGameObjectPoolItem)?.OnSpawnedFromPool(); |
| | 9 | 616 | | } |
| | | 617 | | |
| | | 618 | | /// <summary> |
| | | 619 | | /// The subscribed action called when the <paramref name="pool" /> is asked to <see cref="IManagedPool.TearD |
| | | 620 | | /// before items were returned to the pool. |
| | | 621 | | /// </summary> |
| | | 622 | | /// <param name="pool">The <see cref="SimpleListManagedPool" /> being torn down.</param> |
| | | 623 | | static void OnTearDownAction(SimpleListManagedPool pool) |
| | 14 | 624 | | { |
| | 14 | 625 | | ManagedPoolBuilder.RemoveManagedPool(pool); |
| | 14 | 626 | | } |
| | | 627 | | } |
| | | 628 | | } |
| | | 629 | | #endif // !UNITY_DOTSRUNTIME |