| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using System; |
| | 3 | | using System.Threading; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.Pool; |
| | 6 | | using Object = UnityEngine.Object; |
| | 7 | |
|
| | 8 | | namespace MainScripts.DCL.Helpers.Utils |
| | 9 | | { |
| | 10 | | public interface IUnityObjectPool<T> : IObjectPool<T> where T : Component |
| | 11 | | { |
| | 12 | | T Prefab { get; } |
| | 13 | |
|
| | 14 | | void Prewarm(int count); |
| | 15 | |
|
| | 16 | | UniTask PrewarmAsync(int count, int createPerFrame, CancellationToken ct); |
| | 17 | | } |
| | 18 | |
|
| | 19 | | public class UnityObjectPool<T> : IUnityObjectPool<T> where T: Component |
| | 20 | | { |
| | 21 | | private readonly T prefab; |
| | 22 | | private readonly Transform parent; |
| | 23 | | private readonly Vector3 defaultPosition; |
| | 24 | | private readonly Quaternion defaultRot; |
| | 25 | | private readonly Action<T> actionOnCreate; |
| | 26 | |
|
| | 27 | | private readonly ObjectPool<T> internalPool; |
| | 28 | |
|
| 852 | 29 | | public UnityObjectPool( |
| | 30 | | T prefab, |
| | 31 | | Transform parent, |
| | 32 | | Vector3 defaultPosition = default, |
| | 33 | | Quaternion defaultRot = default, |
| | 34 | | Action<T> actionOnCreate = null, |
| | 35 | | Action<T> actionOnGet = null, |
| | 36 | | Action<T> actionOnRelease = null, |
| | 37 | | Action<T> actionOnDestroy = null, |
| | 38 | | int defaultCapacity = 10, |
| | 39 | | int maxSize = 1000) |
| | 40 | | { |
| 852 | 41 | | this.prefab = prefab; |
| 852 | 42 | | this.parent = parent; |
| 852 | 43 | | this.defaultPosition = defaultPosition; |
| 852 | 44 | | this.defaultRot = defaultRot; |
| 852 | 45 | | this.actionOnCreate = actionOnCreate; |
| | 46 | |
|
| 852 | 47 | | internalPool = new ObjectPool<T>( |
| | 48 | | Instantiate, |
| | 49 | | actionOnGet ?? EnableObject, |
| | 50 | | actionOnRelease ?? DisableObject, |
| | 51 | | actionOnDestroy ?? Destroy, |
| | 52 | | Application.isEditor, |
| | 53 | | defaultCapacity, |
| | 54 | | maxSize |
| | 55 | | ); |
| 852 | 56 | | } |
| | 57 | |
|
| 5760 | 58 | | T IUnityObjectPool<T>.Prefab => prefab; |
| | 59 | |
|
| | 60 | | public void Prewarm(int count) |
| | 61 | | { |
| 6542 | 62 | | for (var i = 0; i < count; i++) |
| 2803 | 63 | | Release(Instantiate()); |
| 468 | 64 | | } |
| | 65 | |
|
| | 66 | | public async UniTask PrewarmAsync(int count, int createPerFrame, CancellationToken ct) |
| | 67 | | { |
| 1280 | 68 | | while (count > 0) |
| | 69 | | { |
| 35252 | 70 | | for (var i = 0; i < createPerFrame && count > 0; i++) |
| | 71 | | { |
| 16700 | 72 | | Release(Instantiate()); |
| 16700 | 73 | | count--; |
| | 74 | | } |
| | 75 | |
|
| 926 | 76 | | await UniTask.DelayFrame(1, cancellationToken: ct); |
| | 77 | | } |
| 354 | 78 | | } |
| | 79 | |
|
| | 80 | | private T Instantiate() |
| | 81 | | { |
| 19503 | 82 | | var obj = Object.Instantiate(prefab, defaultPosition, defaultRot, parent); |
| 19503 | 83 | | actionOnCreate?.Invoke(obj); |
| 19503 | 84 | | return obj; |
| | 85 | | } |
| | 86 | |
|
| | 87 | | private static void DisableObject(T obj) |
| | 88 | | { |
| 19537 | 89 | | obj.gameObject.SetActive(false); |
| 19534 | 90 | | } |
| | 91 | |
|
| | 92 | | private static void EnableObject(T obj) |
| | 93 | | { |
| 26 | 94 | | obj.gameObject.SetActive(true); |
| 26 | 95 | | } |
| | 96 | |
|
| | 97 | | private void Destroy(T obj) |
| | 98 | | { |
| 1295 | 99 | | if (obj) |
| 1295 | 100 | | global::DCL.Helpers.Utils.SafeDestroy(obj.gameObject); |
| 1295 | 101 | | } |
| | 102 | |
|
| | 103 | | public T Get() => |
| 26 | 104 | | internalPool.Get(); |
| | 105 | |
|
| | 106 | | public PooledObject<T> Get(out T v) => |
| 0 | 107 | | internalPool.Get(out v); |
| | 108 | |
|
| | 109 | | public void Release(T element) |
| | 110 | | { |
| 19537 | 111 | | internalPool.Release(element); |
| 19534 | 112 | | } |
| | 113 | |
|
| | 114 | | public void Clear() |
| | 115 | | { |
| 85 | 116 | | internalPool.Clear(); |
| 85 | 117 | | } |
| | 118 | |
|
| 0 | 119 | | public int CountInactive => internalPool.CountInactive; |
| | 120 | | } |
| | 121 | | } |