-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathPoolable.cs
52 lines (43 loc) · 975 Bytes
/
Poolable.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#if ODIN_INSPECTOR
using Sirenix.OdinInspector;
#endif
using UnityEngine;
namespace ToolBox.Pools
{
[DisallowMultipleComponent]
public class Poolable : MonoBehaviour
{
public Pool Pool { get; private set; } = null;
private IPoolable[] _poolables = new IPoolable[0];
private bool _isPooled = false;
private bool _isEnabled = true;
private void Awake() =>
_poolables = GetComponentsInChildren<IPoolable>(true);
#if ODIN_INSPECTOR
[Button]
#endif
public void ReturnToPool()
{
if (!_isEnabled)
return;
for (int i = 0; i < _poolables.Length; i++)
_poolables[i].OnDespawn();
Pool.ReturnEntity(this);
_isEnabled = false;
}
public void ReturnFromPool()
{
for (int i = 0; i < _poolables.Length; i++)
_poolables[i].OnSpawn();
_isEnabled = true;
}
public void SetPool(Pool pool)
{
if (!_isPooled)
{
Pool = pool;
_isPooled = true;
}
}
}
}