| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using Hertzole.ScriptableValues.Helpers; |
| | | 3 | | using UnityEngine; |
| | | 4 | | using UnityEngine.Pool; |
| | | 5 | | |
| | | 6 | | namespace Hertzole.ScriptableValues |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// A <see cref="ScriptableObject" /> that holds a pool of <see cref="GameObject" />. |
| | | 10 | | /// </summary> |
| | | 11 | | #if UNITY_EDITOR |
| | | 12 | | [CreateAssetMenu(fileName = "New Game Object Pool", menuName = "Hertzole/Scriptable Values/Pools/Game Object Pool", |
| | | 13 | | #endif |
| | | 14 | | public class ScriptableGameObjectPool : ScriptablePool<GameObject> |
| | | 15 | | { |
| | | 16 | | [SerializeField] |
| | 216 | 17 | | private GameObject prefab = null!; |
| | | 18 | | |
| | | 19 | | public GameObject Prefab |
| | | 20 | | { |
| | 0 | 21 | | get { return prefab; } |
| | 540 | 22 | | set { prefab = value; } |
| | | 23 | | } |
| | | 24 | | |
| | | 25 | | protected override GameObject CreateObject() |
| | 186 | 26 | | { |
| | 186 | 27 | | return Instantiate(prefab); |
| | 186 | 28 | | } |
| | | 29 | | |
| | | 30 | | protected override void DestroyObject(GameObject item) |
| | 180 | 31 | | { |
| | | 32 | | // The object may already have been destroyed. Check if it exists first. |
| | 180 | 33 | | if (item != null) |
| | 180 | 34 | | { |
| | 180 | 35 | | Destroy(item.gameObject); |
| | 180 | 36 | | } |
| | 180 | 37 | | } |
| | | 38 | | |
| | | 39 | | internal override void OnGetInternal(GameObject item) |
| | 198 | 40 | | { |
| | | 41 | | // Don't check for poolable here as it's done with components in OnGet. |
| | 198 | 42 | | OnGet(item); |
| | 198 | 43 | | } |
| | | 44 | | |
| | | 45 | | internal override void OnReturnInternal(GameObject item) |
| | 96 | 46 | | { |
| | | 47 | | // Don't check for poolable here as it's done with components in OnReturn. |
| | 96 | 48 | | OnReturn(item); |
| | 96 | 49 | | } |
| | | 50 | | |
| | | 51 | | protected override void OnGet(GameObject item) |
| | 198 | 52 | | { |
| | 198 | 53 | | Guard.IsNotNull(item, nameof(item)); |
| | | 54 | | |
| | 198 | 55 | | item.gameObject.SetActive(true); |
| | | 56 | | |
| | 198 | 57 | | using PooledObject<List<IPoolable>> scope = ListPool<IPoolable>.Get(out List<IPoolable> poolableBuffer); |
| | 198 | 58 | | item.GetComponentsInChildren(true, poolableBuffer); |
| | 792 | 59 | | for (int i = 0; i < poolableBuffer.Count; i++) |
| | 198 | 60 | | { |
| | 198 | 61 | | poolableBuffer[i].OnUnpooled(); |
| | 198 | 62 | | } |
| | 396 | 63 | | } |
| | | 64 | | |
| | | 65 | | protected override void OnReturn(GameObject item) |
| | 96 | 66 | | { |
| | 96 | 67 | | using PooledObject<List<IPoolable>> scope = ListPool<IPoolable>.Get(out List<IPoolable> poolableBuffer); |
| | 96 | 68 | | item.GetComponentsInChildren(true, poolableBuffer); |
| | | 69 | | |
| | 384 | 70 | | for (int i = 0; i < poolableBuffer.Count; i++) |
| | 96 | 71 | | { |
| | 96 | 72 | | poolableBuffer[i].OnPooled(); |
| | 96 | 73 | | } |
| | | 74 | | |
| | 96 | 75 | | item.gameObject.SetActive(false); |
| | 192 | 76 | | } |
| | | 77 | | } |
| | | 78 | | } |