Skip to content

Commit e567e82

Browse files
committed
Code improvements
1 parent ab883b6 commit e567e82

File tree

2 files changed

+37
-69
lines changed

2 files changed

+37
-69
lines changed

Runtime/Pool.cs

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ public sealed class Pool
88
private Poolable _prefab = null;
99
private Stack<Poolable> _instances = null;
1010

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

1414
public Pool(GameObject prefab)
1515
{
@@ -23,41 +23,33 @@ public Pool(GameObject prefab)
2323
}
2424

2525
_instances = new Stack<Poolable>();
26-
_prefabLookup.Add(prefab.GetHashCode(), this);
26+
_prefabLookup.Add(prefab, this);
2727
}
2828

2929
public static Pool GetPrefabPool(GameObject prefab)
3030
{
31-
bool hasPool = _prefabLookup.TryGetValue(prefab.GetHashCode(), out var pool);
31+
bool hasPool = _prefabLookup.TryGetValue(prefab, out var pool);
3232

3333
if (!hasPool)
3434
pool = new Pool(prefab);
3535

3636
return pool;
3737
}
3838

39-
public static Pool GetInstancePool(GameObject instance)
40-
{
41-
_instanceLookup.TryGetValue(instance.GetHashCode(), out var pool);
42-
return pool;
43-
}
39+
public static bool TryGetInstancePool(GameObject instance, out Pool pool) =>
40+
_instanceLookup.TryGetValue(instance, out pool);
4441

4542
public void Populate(int count)
4643
{
4744
for (int i = 0; i < count; i++)
4845
{
4946
var instance = CreateInstance();
5047
_instances.Push(instance);
51-
instance.gameObject.SetActive(false);
5248
}
5349
}
5450

55-
public GameObject Get()
56-
{
57-
var instance = GetInstance();
58-
59-
return instance.gameObject;
60-
}
51+
public GameObject Get() =>
52+
GetInstance().gameObject;
6153

6254
public GameObject Get(Transform parent, bool spawnInWorldSpace)
6355
{
@@ -100,45 +92,44 @@ public T Get<T>(Vector3 position, Quaternion rotation) where T : Component =>
10092
public T Get<T>(Vector3 position, Quaternion rotation, Transform parent, bool spawnInWorldSpace) where T : Component =>
10193
Get(position, rotation, parent, spawnInWorldSpace).GetComponent<T>();
10294

103-
public void Release(Poolable instance)
95+
public void Release(GameObject instance)
10496
{
105-
_instances.Push(instance);
97+
var poolable = instance.GetComponent<Poolable>();
98+
_instances.Push(poolable);
10699

107100
instance.transform.SetParent(null, false);
108-
instance.gameObject.SetActive(false);
109-
instance.OnRelease();
101+
instance.SetActive(false);
102+
poolable.OnRelease();
110103
}
111104

112105
private Poolable GetInstance()
113106
{
107+
Poolable instance;
108+
114109
if (_instances.Count == 0)
115110
{
116-
var instance = CreateInstance();
117-
instance.gameObject.SetActive(true);
118-
119-
return instance;
111+
instance = CreateInstance();
120112
}
121113
else
122114
{
123-
var instance = _instances.Pop();
115+
instance = _instances.Pop();
124116

125117
if (instance == null)
126118
instance = CreateInstance();
127119
else
128120
instance.OnGet();
129-
130-
instance.gameObject.SetActive(true);
131-
132-
return instance;
133121
}
122+
123+
instance.gameObject.SetActive(true);
124+
return instance;
134125
}
135126

136127
private Poolable CreateInstance()
137128
{
138-
var entity = Object.Instantiate(_prefab);
139-
_instanceLookup.Add(entity.gameObject.GetHashCode(), this);
129+
var instance = Object.Instantiate(_prefab);
130+
_instanceLookup.Add(instance.gameObject, this);
140131

141-
return entity;
132+
return instance;
142133
}
143134
}
144135
}

Runtime/PoolExtensions.cs

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,20 @@ namespace ToolBox.Pools
44
{
55
public static class PoolExtensions
66
{
7-
public static void Populate(this GameObject prefab, int count)
8-
{
9-
var pool = Pool.GetPrefabPool(prefab);
10-
pool.Populate(count);
11-
}
12-
13-
public static GameObject Get(this GameObject prefab)
14-
{
15-
var pool = Pool.GetPrefabPool(prefab);
16-
var entity = pool.Get();
17-
18-
return entity;
19-
}
20-
21-
public static GameObject Get(this GameObject prefab, Transform parent, bool spawnInWorldSpace)
22-
{
23-
var pool = Pool.GetPrefabPool(prefab);
24-
var entity = pool.Get(parent, spawnInWorldSpace);
7+
public static void Populate(this GameObject prefab, int count) =>
8+
Pool.GetPrefabPool(prefab).Populate(count);
259

26-
return entity;
27-
}
28-
29-
public static GameObject Get(this GameObject prefab, Vector3 position, Quaternion rotation)
30-
{
31-
var pool = Pool.GetPrefabPool(prefab);
32-
var entity = pool.Get(position, rotation);
10+
public static GameObject Get(this GameObject prefab) =>
11+
Pool.GetPrefabPool(prefab).Get();
3312

34-
return entity;
35-
}
13+
public static GameObject Get(this GameObject prefab, Transform parent, bool spawnInWorldSpace) =>
14+
Pool.GetPrefabPool(prefab).Get(parent, spawnInWorldSpace);
3615

37-
public static GameObject Get(this GameObject prefab, Vector3 position, Quaternion rotation, Transform parent, bool spawnInWorldSpace)
38-
{
39-
var pool = Pool.GetPrefabPool(prefab);
40-
var entity = pool.Get(position, rotation, parent, spawnInWorldSpace);
16+
public static GameObject Get(this GameObject prefab, Vector3 position, Quaternion rotation) =>
17+
Pool.GetPrefabPool(prefab).Get(position, rotation);
4118

42-
return entity;
43-
}
19+
public static GameObject Get(this GameObject prefab, Vector3 position, Quaternion rotation, Transform parent, bool spawnInWorldSpace) =>
20+
Pool.GetPrefabPool(prefab).Get(position, rotation, parent, spawnInWorldSpace);
4421

4522
public static T Get<T>(this GameObject prefab) where T : Component =>
4623
prefab.Get().GetComponent<T>();
@@ -56,12 +33,12 @@ public static T Get<T>(this GameObject prefab, Vector3 position, Quaternion rota
5633

5734
public static void Release(this GameObject instance)
5835
{
59-
var pool = Pool.GetInstancePool(instance);
36+
bool isPooled = Pool.TryGetInstancePool(instance, out var pool);
6037

61-
if (pool == null)
62-
Object.Destroy(instance);
38+
if (isPooled)
39+
pool.Release(instance);
6340
else
64-
pool.Release(instance.GetComponent<Poolable>());
41+
Object.Destroy(instance);
6542
}
6643
}
6744
}

0 commit comments

Comments
 (0)