Skip to content

Commit 61ad28c

Browse files
committed
Big refactoring
1 parent cf47821 commit 61ad28c

File tree

4 files changed

+90
-133
lines changed

4 files changed

+90
-133
lines changed

Runtime/Pool.cs

Lines changed: 59 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -6,144 +6,137 @@ namespace ToolBox.Pools
66
public sealed class Pool
77
{
88
private Poolable _prefab = null;
9-
private Queue<Poolable> _entities = null;
9+
private Stack<Poolable> _entities = null;
1010

11-
private static Dictionary<int, Pool> _pools = new Dictionary<int, Pool>();
11+
private static Dictionary<int, Pool> _prefabLookup = new Dictionary<int, Pool>();
12+
private static Dictionary<int, Pool> _instanceLookup = new Dictionary<int, Pool>();
1213

13-
public Pool(Poolable prefab, int count)
14-
{
15-
_prefab = prefab;
16-
_entities = new Queue<Poolable>(count);
17-
_pools.Add(prefab.gameObject.GetHashCode(), this);
18-
19-
Populate(count);
20-
}
21-
22-
public Pool(GameObject prefab, int count)
14+
public Pool(GameObject prefab)
2315
{
2416
_prefab = prefab.GetComponent<Poolable>();
2517

2618
if (_prefab == null)
2719
{
2820
_prefab = Object.Instantiate(prefab).AddComponent<Poolable>();
29-
UnityEngine.Object.DontDestroyOnLoad(_prefab);
21+
Object.DontDestroyOnLoad(_prefab);
3022
_prefab.gameObject.SetActive(false);
3123
}
3224

33-
_entities = new Queue<Poolable>(count);
34-
_pools.Add(prefab.GetHashCode(), this);
35-
36-
Populate(count);
25+
_entities = new Stack<Poolable>();
26+
_prefabLookup.Add(prefab.GetHashCode(), this);
3727
}
3828

39-
public static Pool Get(GameObject prefab)
29+
public static Pool GetPrefabPool(GameObject prefab)
4030
{
41-
var hasPool = _pools.TryGetValue(prefab.GetHashCode(), out var pool);
31+
var hasPool = _prefabLookup.TryGetValue(prefab.GetHashCode(), out var pool);
4232

4333
if (!hasPool)
44-
pool = new Pool(prefab, 0);
34+
pool = new Pool(prefab);
4535

4636
return pool;
4737
}
4838

39+
public static Pool GetInstancePool(GameObject instance)
40+
{
41+
_instanceLookup.TryGetValue(instance.GetHashCode(), out var pool);
42+
return pool;
43+
}
44+
4945
public void Populate(int count)
5046
{
5147
for (int i = 0; i < count; i++)
5248
{
53-
Poolable entity = Object.Instantiate(_prefab);
54-
entity.SetPool(this);
55-
_entities.Enqueue(entity);
49+
var entity = Object.Instantiate(_prefab);
50+
_entities.Push(entity);
5651
entity.gameObject.SetActive(false);
5752
}
5853
}
5954

60-
public Poolable GetEntity()
55+
public GameObject Get()
6156
{
62-
Poolable entity = GetEntityFromPool();
63-
entity.ReturnFromPool();
57+
var entity = GetEntityFromPool();
6458

65-
return entity;
59+
return entity.gameObject;
6660
}
6761

68-
public Poolable GetEntity(Transform parent, bool spawnInWorldSpace)
62+
public GameObject Get(Transform parent, bool spawnInWorldSpace)
6963
{
70-
Poolable entity = GetEntityFromPool();
64+
var entity = GetEntityFromPool();
7165

7266
entity.transform.SetParent(parent, spawnInWorldSpace);
73-
entity.ReturnFromPool();
7467

75-
return entity;
68+
return entity.gameObject;
7669
}
7770

78-
public Poolable GetEntity(Vector3 position, Quaternion rotation)
71+
public GameObject Get(Vector3 position, Quaternion rotation)
7972
{
80-
Poolable entity = GetEntityFromPool();
73+
var entity = GetEntityFromPool();
8174

8275
entity.transform.SetPositionAndRotation(position, rotation);
83-
entity.ReturnFromPool();
8476

85-
return entity;
77+
return entity.gameObject;
8678
}
8779

88-
public Poolable GetEntity(Vector3 position, Quaternion rotation, Transform parent, bool spawnInWorldSpace)
80+
public GameObject Get(Vector3 position, Quaternion rotation, Transform parent, bool spawnInWorldSpace)
8981
{
90-
Poolable entity = GetEntityFromPool();
91-
Transform entityTransform = entity.transform;
82+
var entity = GetEntityFromPool();
83+
var entityTransform = entity.transform;
9284

9385
entityTransform.SetParent(parent, spawnInWorldSpace);
9486
entityTransform.SetPositionAndRotation(position, rotation);
95-
entity.ReturnFromPool();
9687

97-
return entity;
88+
return entity.gameObject;
9889
}
9990

100-
public T GetEntity<T>() where T : Component =>
101-
GetEntity().GetComponent<T>();
91+
public T Get<T>() where T : Component =>
92+
Get().GetComponent<T>();
10293

103-
public T GetEntity<T>(Transform parent, bool spawnInWorldSpace) where T : Component =>
104-
GetEntity(parent, spawnInWorldSpace).GetComponent<T>();
94+
public T Get<T>(Transform parent, bool spawnInWorldSpace) where T : Component =>
95+
Get(parent, spawnInWorldSpace).GetComponent<T>();
10596

106-
public T GetEntity<T>(Vector3 position, Quaternion rotation) where T : Component =>
107-
GetEntity(position, rotation).GetComponent<T>();
97+
public T Get<T>(Vector3 position, Quaternion rotation) where T : Component =>
98+
Get(position, rotation).GetComponent<T>();
10899

109-
public T GetEntity<T>(Vector3 position, Quaternion rotation, Transform parent, bool spawnInWorldSpace) where T : Component =>
110-
GetEntity(position, rotation, parent, spawnInWorldSpace).GetComponent<T>();
100+
public T Get<T>(Vector3 position, Quaternion rotation, Transform parent, bool spawnInWorldSpace) where T : Component =>
101+
Get(position, rotation, parent, spawnInWorldSpace).GetComponent<T>();
111102

112-
public void ReturnEntity(Poolable entity)
103+
public void Release(Poolable entity)
113104
{
114-
if (entity.Pool != this)
115-
return;
116-
117-
_entities.Enqueue(entity);
105+
_entities.Push(entity);
118106

119107
entity.transform.SetParent(null, false);
120108
entity.gameObject.SetActive(false);
109+
entity.ReturnToPool();
121110
}
122111

123112
private Poolable GetEntityFromPool()
124113
{
125-
Poolable entity;
126-
127114
if (_entities.Count == 0)
128115
{
129-
entity = Object.Instantiate(_prefab);
130-
entity.SetPool(this);
116+
var entity = Object.Instantiate(_prefab);
117+
_instanceLookup.Add(entity.gameObject.GetHashCode(), this);
131118
entity.gameObject.SetActive(true);
132119

133120
return entity;
134121
}
135-
136-
entity = _entities.Dequeue();
137-
138-
if (entity == null)
122+
else
139123
{
140-
entity = Object.Instantiate(_prefab);
141-
entity.SetPool(this);
142-
}
124+
var entity = _entities.Pop();
125+
126+
if (entity == null)
127+
{
128+
entity = Object.Instantiate(_prefab);
129+
_instanceLookup.Add(entity.gameObject.GetHashCode(), this);
130+
}
131+
else
132+
{
133+
entity.ReturnFromPool();
134+
}
143135

144-
entity.gameObject.SetActive(true);
136+
entity.gameObject.SetActive(true);
145137

146-
return entity;
138+
return entity;
139+
}
147140
}
148141
}
149142
}

Runtime/PoolExtensions.cs

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,73 +6,62 @@ public static class PoolExtensions
66
{
77
public static void Populate(this GameObject prefab, int count)
88
{
9-
var pool = Pool.Get(prefab);
9+
var pool = Pool.GetPrefabPool(prefab);
1010
pool.Populate(count);
1111
}
1212

13-
public static Poolable Spawn(this GameObject prefab)
13+
public static GameObject Get(this GameObject prefab)
1414
{
15-
var pool = Pool.Get(prefab);
16-
var entity = pool.GetEntity();
15+
var pool = Pool.GetPrefabPool(prefab);
16+
var entity = pool.Get();
1717

1818
return entity;
1919
}
2020

21-
public static Poolable Spawn(this GameObject prefab, Transform parent, bool spawnInWorldSpace)
21+
public static GameObject Get(this GameObject prefab, Transform parent, bool spawnInWorldSpace)
2222
{
23-
var pool = Pool.Get(prefab);
24-
var entity = pool.GetEntity(parent, spawnInWorldSpace);
23+
var pool = Pool.GetPrefabPool(prefab);
24+
var entity = pool.Get(parent, spawnInWorldSpace);
2525

2626
return entity;
2727
}
2828

29-
public static Poolable Spawn(this GameObject prefab, Vector3 position, Quaternion rotation)
29+
public static GameObject Get(this GameObject prefab, Vector3 position, Quaternion rotation)
3030
{
31-
var pool = Pool.Get(prefab);
32-
var entity = pool.GetEntity(position, rotation);
31+
var pool = Pool.GetPrefabPool(prefab);
32+
var entity = pool.Get(position, rotation);
3333

3434
return entity;
3535
}
3636

37-
public static Poolable Spawn(this GameObject prefab, Vector3 position, Quaternion rotation, Transform parent, bool spawnInWorldSpace)
37+
public static GameObject Get(this GameObject prefab, Vector3 position, Quaternion rotation, Transform parent, bool spawnInWorldSpace)
3838
{
39-
var pool = Pool.Get(prefab);
40-
var entity = pool.GetEntity(position, rotation, parent, spawnInWorldSpace);
39+
var pool = Pool.GetPrefabPool(prefab);
40+
var entity = pool.Get(position, rotation, parent, spawnInWorldSpace);
4141

4242
return entity;
4343
}
4444

45-
public static T Spawn<T>(this GameObject prefab) where T : Component =>
46-
prefab.Spawn().GetComponent<T>();
45+
public static T Get<T>(this GameObject prefab) where T : Component =>
46+
prefab.Get().GetComponent<T>();
4747

48-
public static T Spawn<T>(this GameObject prefab, Transform parent, bool spawnInWorldSpace) where T : Component =>
49-
prefab.Spawn(parent, spawnInWorldSpace).GetComponent<T>();
48+
public static T Get<T>(this GameObject prefab, Transform parent, bool spawnInWorldSpace) where T : Component =>
49+
prefab.Get(parent, spawnInWorldSpace).GetComponent<T>();
5050

51-
public static T Spawn<T>(this GameObject prefab, Vector3 position, Quaternion rotation) where T : Component =>
52-
prefab.Spawn(position, rotation).GetComponent<T>();
51+
public static T Get<T>(this GameObject prefab, Vector3 position, Quaternion rotation) where T : Component =>
52+
prefab.Get(position, rotation).GetComponent<T>();
5353

54-
public static T Spawn<T>(this GameObject prefab, Vector3 position, Quaternion rotation, Transform parent, bool spawnInWorldSpace) where T : Component =>
55-
prefab.Spawn(position, rotation, parent, spawnInWorldSpace).GetComponent<T>();
54+
public static T Get<T>(this GameObject prefab, Vector3 position, Quaternion rotation, Transform parent, bool spawnInWorldSpace) where T : Component =>
55+
prefab.Get(position, rotation, parent, spawnInWorldSpace).GetComponent<T>();
5656

57-
public static void Despawn(this Poolable instance)
57+
public static void Release(this GameObject instance)
5858
{
59-
if (!instance.IsPooled)
60-
{
61-
Object.Destroy(instance.gameObject);
62-
return;
63-
}
59+
var pool = Pool.GetInstancePool(instance);
6460

65-
instance.ReturnToPool();
66-
}
67-
68-
public static void Despawn(this GameObject instance)
69-
{
70-
var isPooled = Poolable.IsInstancePooled(instance);
71-
72-
if (isPooled)
73-
instance.GetComponent<Poolable>().ReturnToPool();
74-
else
61+
if (pool == null)
7562
Object.Destroy(instance);
63+
else
64+
pool.Release(instance.GetComponent<Poolable>());
7665
}
7766
}
7867
}

Runtime/PoolInstaller.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ private struct PoolContainer
1919
[SerializeField] private GameObject _prefab;
2020
[SerializeField, Min(1)] private int _startCount;
2121

22-
public void Populate() =>
23-
new Pool(_prefab, _startCount);
22+
public void Populate()
23+
{
24+
var pool = new Pool(_prefab);
25+
pool.Populate(_startCount);
26+
}
2427
}
2528
}
2629
}

Runtime/Poolable.cs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
#if ODIN_INSPECTOR
22
using Sirenix.OdinInspector;
33
#endif
4-
using System.Collections.Generic;
54
using UnityEngine;
65

76
namespace ToolBox.Pools
87
{
98
[DisallowMultipleComponent]
109
public class Poolable : MonoBehaviour
1110
{
12-
public Pool Pool { get; private set; } = null;
13-
public bool IsPooled { get; private set; } = false;
14-
1511
private IPoolable[] _poolables = new IPoolable[0];
16-
private bool _isEnabled = false;
17-
18-
private static HashSet<int> _pooledObjects = new HashSet<int>();
1912

2013
private void Awake() =>
2114
_poolables = GetComponentsInChildren<IPoolable>(true);
@@ -25,35 +18,14 @@ private void Awake() =>
2518
#endif
2619
public void ReturnToPool()
2720
{
28-
if (!_isEnabled)
29-
return;
30-
3121
for (int i = 0; i < _poolables.Length; i++)
3222
_poolables[i].OnDespawn();
33-
34-
Pool.ReturnEntity(this);
35-
_isEnabled = false;
3623
}
3724

3825
public void ReturnFromPool()
3926
{
4027
for (int i = 0; i < _poolables.Length; i++)
4128
_poolables[i].OnSpawn();
42-
43-
_isEnabled = true;
4429
}
45-
46-
public void SetPool(Pool pool)
47-
{
48-
if (!IsPooled)
49-
{
50-
Pool = pool;
51-
IsPooled = true;
52-
_pooledObjects.Add(gameObject.GetHashCode());
53-
}
54-
}
55-
56-
public static bool IsInstancePooled(GameObject instance) =>
57-
_pooledObjects.Contains(instance.GetHashCode());
5830
}
5931
}

0 commit comments

Comments
 (0)