| | | 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="Component" />. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <typeparam name="T">The type of <see cref="Component" />.</typeparam> |
| | | 12 | | public abstract class ScriptableComponentPool<T> : ScriptablePool<T> where T : Component |
| | | 13 | | { |
| | | 14 | | [SerializeField] |
| | 210 | 15 | | private T prefab = null!; |
| | | 16 | | |
| | | 17 | | public T Prefab |
| | | 18 | | { |
| | 0 | 19 | | get { return prefab; } |
| | 540 | 20 | | set { prefab = value; } |
| | | 21 | | } |
| | | 22 | | |
| | | 23 | | protected override T CreateObject() |
| | 186 | 24 | | { |
| | 186 | 25 | | return Instantiate(prefab); |
| | 186 | 26 | | } |
| | | 27 | | |
| | | 28 | | protected override void DestroyObject(T item) |
| | 180 | 29 | | { |
| | | 30 | | // The object may already have been destroyed. Check if it exists first. |
| | 180 | 31 | | if (item != null) |
| | 180 | 32 | | { |
| | 180 | 33 | | Destroy(item.gameObject); |
| | 180 | 34 | | } |
| | 180 | 35 | | } |
| | | 36 | | |
| | | 37 | | internal override void OnGetInternal(T item) |
| | 198 | 38 | | { |
| | | 39 | | // Don't check for poolable here and it's done with components in OnGet. |
| | 198 | 40 | | OnGet(item); |
| | 198 | 41 | | } |
| | | 42 | | |
| | | 43 | | internal override void OnReturnInternal(T item) |
| | 96 | 44 | | { |
| | | 45 | | // Don't check for poolable here and it's done with components in OnReturn. |
| | 96 | 46 | | OnReturn(item); |
| | 96 | 47 | | } |
| | | 48 | | |
| | | 49 | | protected override void OnGet(T item) |
| | 198 | 50 | | { |
| | 198 | 51 | | Guard.IsNotNull(item, nameof(item)); |
| | | 52 | | |
| | 198 | 53 | | item.gameObject.SetActive(true); |
| | | 54 | | |
| | 198 | 55 | | using PooledObject<List<IPoolable>> scope = ListPool<IPoolable>.Get(out List<IPoolable> poolableBuffer); |
| | 198 | 56 | | item.GetComponentsInChildren(true, poolableBuffer); |
| | 792 | 57 | | for (int i = 0; i < poolableBuffer.Count; i++) |
| | 198 | 58 | | { |
| | 198 | 59 | | poolableBuffer[i].OnUnpooled(); |
| | 198 | 60 | | } |
| | 396 | 61 | | } |
| | | 62 | | |
| | | 63 | | protected override void OnReturn(T item) |
| | 96 | 64 | | { |
| | 96 | 65 | | using PooledObject<List<IPoolable>> scope = ListPool<IPoolable>.Get(out List<IPoolable> poolableBuffer); |
| | 96 | 66 | | item.GetComponentsInChildren(true, poolableBuffer); |
| | | 67 | | |
| | 384 | 68 | | for (int i = 0; i < poolableBuffer.Count; i++) |
| | 96 | 69 | | { |
| | 96 | 70 | | poolableBuffer[i].OnPooled(); |
| | 96 | 71 | | } |
| | | 72 | | |
| | 96 | 73 | | item.gameObject.SetActive(false); |
| | 192 | 74 | | } |
| | | 75 | | } |
| | | 76 | | } |