| | 1 | | using System.Collections.Generic; |
| | 2 | | using DCL.Components; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | namespace DCL |
| | 6 | | { |
| | 7 | | public class PoolableObject : IPoolableObject |
| | 8 | | { |
| | 9 | | public Pool pool; |
| | 10 | | public LinkedListNode<PoolableObject> node; |
| 10 | 11 | | public GameObject gameObject { get; } |
| | 12 | |
|
| 2 | 13 | | public bool isInsidePool { get { return node != null; } } |
| | 14 | |
|
| | 15 | | public event System.Action OnGet; |
| | 16 | | public event System.Action OnRelease; |
| | 17 | |
|
| | 18 | | private IPoolLifecycleHandler[] lifecycleHandlers = null; |
| | 19 | |
|
| 806 | 20 | | public PoolableObject(Pool poolOwner, GameObject go) |
| | 21 | | { |
| 806 | 22 | | this.gameObject = go; |
| 806 | 23 | | this.pool = poolOwner; |
| | 24 | |
|
| 806 | 25 | | if (pool.useLifecycleHandlers) |
| 2 | 26 | | lifecycleHandlers = gameObject.GetComponents<IPoolLifecycleHandler>(); |
| 806 | 27 | | } |
| | 28 | |
|
| | 29 | | public void OnPoolGet() |
| | 30 | | { |
| 852 | 31 | | if (lifecycleHandlers != null) |
| | 32 | | { |
| 12 | 33 | | for (var i = 0; i < lifecycleHandlers.Length; i++) |
| | 34 | | { |
| 4 | 35 | | var handler = lifecycleHandlers[i]; |
| 4 | 36 | | handler.OnPoolGet(); |
| | 37 | | } |
| | 38 | | } |
| | 39 | |
|
| 852 | 40 | | OnGet?.Invoke(); |
| 0 | 41 | | } |
| | 42 | |
|
| | 43 | | public void OnPoolRelease() |
| | 44 | | { |
| 843 | 45 | | if (lifecycleHandlers != null) |
| | 46 | | { |
| 12 | 47 | | for (var i = 0; i < lifecycleHandlers.Length; i++) |
| | 48 | | { |
| 4 | 49 | | var handler = lifecycleHandlers[i]; |
| 4 | 50 | | handler.OnPoolRelease(); |
| | 51 | | } |
| | 52 | | } |
| | 53 | |
|
| 843 | 54 | | OnRelease?.Invoke(); |
| 0 | 55 | | } |
| | 56 | |
|
| | 57 | | public void Release() |
| | 58 | | { |
| 843 | 59 | | if (this == null) |
| | 60 | | { |
| | 61 | | #if UNITY_EDITOR |
| 0 | 62 | | Debug.LogWarning("Release == null??! This shouldn't happen"); |
| | 63 | | #endif |
| 0 | 64 | | return; |
| | 65 | | } |
| | 66 | |
|
| 843 | 67 | | if (pool != null) |
| | 68 | | { |
| 843 | 69 | | pool.Release(this); |
| 843 | 70 | | } |
| | 71 | | else |
| | 72 | | { |
| | 73 | | #if UNITY_EDITOR |
| 0 | 74 | | Debug.LogError("Pool is null upon release!"); |
| | 75 | | #endif |
| | 76 | | } |
| | 77 | |
|
| 843 | 78 | | OnPoolRelease(); |
| 843 | 79 | | } |
| | 80 | |
|
| 4 | 81 | | public void RemoveFromPool() { pool.RemoveFromPool(this); } |
| | 82 | |
|
| | 83 | | public void OnCleanup(DCL.ICleanableEventDispatcher sender) |
| | 84 | | { |
| 70 | 85 | | sender.OnCleanupEvent -= this.OnCleanup; |
| 70 | 86 | | Release(); |
| 70 | 87 | | } |
| | 88 | | } |
| | 89 | | } |