Skip to content

Commit 032b6af

Browse files
committed
Added Clear method
1 parent c6df521 commit 032b6af

File tree

4 files changed

+83
-32
lines changed

4 files changed

+83
-32
lines changed

Runtime/Pool.cs

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,60 +5,97 @@ namespace ToolBox.Pools
55
{
66
internal sealed class Pool
77
{
8-
private readonly Poolable _prefab = null;
9-
private readonly Stack<Poolable> _instances = null;
10-
private readonly Quaternion _rotation = default;
11-
private readonly Vector3 _scale = default;
8+
private GameObject _source;
9+
private Poolable _prototype;
10+
private Stack<Poolable> _instances;
11+
private List<GameObject> _allInstances;
12+
private readonly Quaternion _rotation;
13+
private readonly Vector3 _scale;
14+
private readonly bool _prototypeIsNotSource;
1215

1316
private static readonly Dictionary<GameObject, Pool> _prefabLookup = new Dictionary<GameObject, Pool>(64);
1417
private static readonly Dictionary<GameObject, Pool> _instanceLookup = new Dictionary<GameObject, Pool>(512);
1518

16-
private const int INITIAL_SIZE = 128;
19+
private const int InitialSize = 128;
1720

1821
public Pool(GameObject prefab)
1922
{
20-
_prefab = prefab.GetComponent<Poolable>();
23+
_source = prefab;
24+
_prototype = prefab.GetComponent<Poolable>();
2125

22-
if (_prefab == null)
26+
if (_prototype == null)
2327
{
24-
_prefab = Object.Instantiate(prefab).AddComponent<Poolable>();
25-
Object.DontDestroyOnLoad(_prefab);
26-
_prefab.gameObject.SetActive(false);
28+
_prototype = Object.Instantiate(prefab).AddComponent<Poolable>();
29+
Object.DontDestroyOnLoad(_prototype);
30+
_prototype.gameObject.SetActive(false);
31+
_prototypeIsNotSource = true;
2732
}
2833

29-
_instances = new Stack<Poolable>(INITIAL_SIZE);
30-
_prefabLookup.Add(prefab, this);
34+
_instances = new Stack<Poolable>(InitialSize);
35+
_allInstances = new List<GameObject>(InitialSize);
36+
_prefabLookup.Add(_source, this);
3137

3238
var transform = prefab.transform;
3339
_rotation = transform.rotation;
3440
_scale = transform.localScale;
3541
}
3642

37-
public static Pool GetPrefabPool(GameObject prefab)
43+
public static Pool GetPoolByPrefab(GameObject prefab, bool create = true)
3844
{
39-
bool hasPool = _prefabLookup.TryGetValue(prefab, out var pool);
45+
var hasPool = _prefabLookup.TryGetValue(prefab, out var pool);
4046

41-
if (!hasPool)
47+
if (!hasPool && create)
4248
pool = new Pool(prefab);
4349

4450
return pool;
4551
}
4652

47-
public static bool TryGetInstancePool(GameObject instance, out Pool pool)
53+
public static bool GetPoolByInstance(GameObject instance, out Pool pool)
4854
{
4955
return _instanceLookup.TryGetValue(instance, out pool);
5056
}
5157

58+
public static void Remove(GameObject instance)
59+
{
60+
_instanceLookup.Remove(instance);
61+
}
62+
5263
public void Populate(int count)
5364
{
54-
for (int i = 0; i < count; i++)
65+
for (var i = 0; i < count; i++)
5566
{
5667
var instance = CreateInstance();
5768
instance.gameObject.SetActive(false);
5869
_instances.Push(instance);
5970
}
6071
}
6172

73+
public void Clear(bool destroyActive)
74+
{
75+
_prefabLookup.Remove(_source);
76+
77+
foreach (var instance in _allInstances)
78+
{
79+
if (instance == null)
80+
continue;
81+
82+
_instanceLookup.Remove(instance);
83+
84+
if (!destroyActive && instance.activeInHierarchy)
85+
continue;
86+
87+
Object.Destroy(instance);
88+
}
89+
90+
if (_prototypeIsNotSource)
91+
Object.Destroy(_prototype.gameObject);
92+
93+
_source = null;
94+
_prototype = null;
95+
_instances = null;
96+
_allInstances = null;
97+
}
98+
6299
public GameObject Reuse()
63100
{
64101
var instance = GetInstance();
@@ -121,7 +158,7 @@ public void Release(GameObject instance)
121158

122159
private Poolable GetInstance()
123160
{
124-
int count = _instances.Count;
161+
var count = _instances.Count;
125162

126163
if (count != 0)
127164
{
@@ -168,9 +205,11 @@ private Poolable GetInstance()
168205

169206
private Poolable CreateInstance()
170207
{
171-
var instance = Object.Instantiate(_prefab);
208+
var instance = Object.Instantiate(_prototype);
172209
var instanceGameObject = instance.gameObject;
210+
173211
_instanceLookup.Add(instanceGameObject, this);
212+
_allInstances.Add(instanceGameObject);
174213

175214
return instance;
176215
}

Runtime/PoolHelper.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,37 @@ public static class PoolHelper
66
{
77
public static void Populate(this GameObject prefab, int count)
88
{
9-
Pool.GetPrefabPool(prefab).Populate(count);
9+
Pool.GetPoolByPrefab(prefab).Populate(count);
10+
}
11+
12+
public static void Clear(this GameObject prefab, bool destroyActive)
13+
{
14+
Pool.GetPoolByPrefab(prefab, false)?.Clear(destroyActive);
1015
}
1116

1217
public static GameObject Reuse(this GameObject prefab)
1318
{
14-
return Pool.GetPrefabPool(prefab).Reuse();
19+
return Pool.GetPoolByPrefab(prefab).Reuse();
1520
}
1621

1722
public static GameObject Reuse(this GameObject prefab, Transform parent)
1823
{
19-
return Pool.GetPrefabPool(prefab).Reuse(parent);
24+
return Pool.GetPoolByPrefab(prefab).Reuse(parent);
2025
}
2126

2227
public static GameObject Reuse(this GameObject prefab, Transform parent, bool worldPositionStays)
2328
{
24-
return Pool.GetPrefabPool(prefab).Reuse(parent, worldPositionStays);
29+
return Pool.GetPoolByPrefab(prefab).Reuse(parent, worldPositionStays);
2530
}
2631

2732
public static GameObject Reuse(this GameObject prefab, Vector3 position, Quaternion rotation)
2833
{
29-
return Pool.GetPrefabPool(prefab).Reuse(position, rotation);
34+
return Pool.GetPoolByPrefab(prefab).Reuse(position, rotation);
3035
}
3136

3237
public static GameObject Reuse(this GameObject prefab, Vector3 position, Quaternion rotation, Transform parent)
3338
{
34-
return Pool.GetPrefabPool(prefab).Reuse(position, rotation, parent);
39+
return Pool.GetPoolByPrefab(prefab).Reuse(position, rotation, parent);
3540
}
3641

3742
public static T Reuse<T>(this GameObject prefab) where T : Component
@@ -61,7 +66,7 @@ public static T Reuse<T>(this GameObject prefab, Vector3 position, Quaternion ro
6166

6267
public static void Release(this GameObject instance)
6368
{
64-
bool isPooled = Pool.TryGetInstancePool(instance, out var pool);
69+
var isPooled = Pool.GetPoolByInstance(instance, out var pool);
6570

6671
if (isPooled)
6772
{

Runtime/PoolInstaller.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ namespace ToolBox.Pools
55
[DefaultExecutionOrder(-9999), DisallowMultipleComponent]
66
internal sealed class PoolInstaller : MonoBehaviour
77
{
8-
[SerializeField] private PoolContainer[] _pools = null;
8+
[SerializeField] private PoolContainer[] _pools;
99

1010
private void Awake()
1111
{
12-
for (int i = 0; i < _pools.Length; i++)
12+
for (var i = 0; i < _pools.Length; i++)
1313
_pools[i].Populate();
1414
}
1515

@@ -19,8 +19,10 @@ private struct PoolContainer
1919
[SerializeField] private GameObject _prefab;
2020
[SerializeField, Min(1)] private int _startCount;
2121

22-
public void Populate() =>
22+
public void Populate()
23+
{
2324
_prefab.Populate(_startCount);
25+
}
2426
}
2527
}
2628
}

Runtime/Poolable.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,31 @@ namespace ToolBox.Pools
77
internal sealed class Poolable : MonoBehaviour
88
{
99
private IPoolable[] _poolables = Array.Empty<IPoolable>();
10-
private bool _isInitialized = false;
10+
private bool _isInitialized;
1111

1212
private void Awake()
1313
{
1414
_poolables = GetComponentsInChildren<IPoolable>(true);
1515
_isInitialized = true;
1616
}
1717

18+
private void OnDestroy()
19+
{
20+
Pool.Remove(gameObject);
21+
}
22+
1823
public void OnReuse()
1924
{
2025
if (!_isInitialized)
2126
return;
2227

23-
for (int i = 0; i < _poolables.Length; i++)
28+
for (var i = 0; i < _poolables.Length; i++)
2429
_poolables[i].OnReuse();
2530
}
2631

2732
public void OnRelease()
2833
{
29-
for (int i = 0; i < _poolables.Length; i++)
34+
for (var i = 0; i < _poolables.Length; i++)
3035
_poolables[i].OnRelease();
3136
}
3237
}

0 commit comments

Comments
 (0)