Skip to content

Commit 262590a

Browse files
committed
v1.8
1 parent 6015dfa commit 262590a

File tree

7 files changed

+106
-102
lines changed

7 files changed

+106
-102
lines changed

Runtime/IPoolable.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
{
33
public interface IPoolable
44
{
5-
void OnGet();
5+
/// <summary>
6+
/// This method will be called on 2nd Reuse call.
7+
/// Use Unity's Awake method for first initialization and this method for others
8+
/// </summary>
9+
void OnReuse();
610
void OnRelease();
711
}
812
}

Runtime/Pool.cs

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
using System.Collections.Generic;
22
using UnityEngine;
3-
#if ZENJECT
4-
using UnityEngine.SceneManagement;
5-
using Zenject;
6-
#endif
73

84
namespace ToolBox.Pools
95
{
@@ -13,16 +9,11 @@ internal sealed class Pool
139
private readonly Stack<Poolable> _instances = null;
1410
private readonly Quaternion _rotation = default;
1511
private readonly Vector3 _scale = default;
16-
#if ZENJECT
17-
private readonly DiContainer _projectContainer = null;
18-
private DiContainer _sceneContainer = null;
19-
private Scene _currentScene = default;
20-
#endif
21-
12+
2213
private static readonly Dictionary<GameObject, Pool> _prefabLookup = new Dictionary<GameObject, Pool>(64);
2314
private static readonly Dictionary<GameObject, Pool> _instanceLookup = new Dictionary<GameObject, Pool>(512);
2415

25-
private const int CAPACITY = 128;
16+
private const int INITIAL_SIZE = 128;
2617

2718
public Pool(GameObject prefab)
2819
{
@@ -34,12 +25,8 @@ public Pool(GameObject prefab)
3425
Object.DontDestroyOnLoad(_prefab);
3526
_prefab.gameObject.SetActive(false);
3627
}
37-
38-
#if ZENJECT
39-
_projectContainer = ProjectContext.Instance.Container;
40-
UpdateContainer();
41-
#endif
42-
_instances = new Stack<Poolable>(CAPACITY);
28+
29+
_instances = new Stack<Poolable>(INITIAL_SIZE);
4330
_prefabLookup.Add(prefab, this);
4431

4532
var transform = prefab.transform;
@@ -57,8 +44,10 @@ public static Pool GetPrefabPool(GameObject prefab)
5744
return pool;
5845
}
5946

60-
public static bool TryGetInstancePool(GameObject instance, out Pool pool) =>
61-
_instanceLookup.TryGetValue(instance, out pool);
47+
public static bool TryGetInstancePool(GameObject instance, out Pool pool)
48+
{
49+
return _instanceLookup.TryGetValue(instance, out pool);
50+
}
6251

6352
public void Populate(int count)
6453
{
@@ -70,14 +59,14 @@ public void Populate(int count)
7059
}
7160
}
7261

73-
public GameObject Get()
62+
public GameObject Reuse()
7463
{
7564
var instance = GetInstance();
7665

7766
return instance.gameObject;
7867
}
7968

80-
public GameObject Get(Transform parent)
69+
public GameObject Reuse(Transform parent)
8170
{
8271
var instance = GetInstance();
8372

@@ -86,7 +75,7 @@ public GameObject Get(Transform parent)
8675
return instance.gameObject;
8776
}
8877

89-
public GameObject Get(Transform parent, bool worldPositionStays)
78+
public GameObject Reuse(Transform parent, bool worldPositionStays)
9079
{
9180
var instance = GetInstance();
9281

@@ -95,7 +84,7 @@ public GameObject Get(Transform parent, bool worldPositionStays)
9584
return instance.gameObject;
9685
}
9786

98-
public GameObject Get(Vector3 position, Quaternion rotation)
87+
public GameObject Reuse(Vector3 position, Quaternion rotation)
9988
{
10089
var instance = GetInstance();
10190

@@ -104,7 +93,7 @@ public GameObject Get(Vector3 position, Quaternion rotation)
10493
return instance.gameObject;
10594
}
10695

107-
public GameObject Get(Vector3 position, Quaternion rotation, Transform parent)
96+
public GameObject Reuse(Vector3 position, Quaternion rotation, Transform parent)
10897
{
10998
var instance = GetInstance();
11099
var instanceTransform = instance.transform;
@@ -148,7 +137,7 @@ private Poolable GetInstance()
148137

149138
if (instance != null)
150139
{
151-
instance.OnGet();
140+
instance.OnReuse();
152141
instance.gameObject.SetActive(true);
153142

154143
return instance;
@@ -162,13 +151,11 @@ private Poolable GetInstance()
162151

163152
return instance;
164153
}
165-
else
166-
{
167-
instance.OnGet();
168-
instance.gameObject.SetActive(true);
169154

170-
return instance;
171-
}
155+
instance.OnReuse();
156+
instance.gameObject.SetActive(true);
157+
158+
return instance;
172159
}
173160
else
174161
{
@@ -184,23 +171,8 @@ private Poolable CreateInstance()
184171
var instance = Object.Instantiate(_prefab);
185172
var instanceGameObject = instance.gameObject;
186173
_instanceLookup.Add(instanceGameObject, this);
187-
#if ZENJECT
188-
if (!_currentScene.isLoaded)
189-
UpdateContainer();
190-
191-
_sceneContainer.InjectGameObject(instanceGameObject);
192-
#endif
193174

194175
return instance;
195176
}
196-
197-
#if ZENJECT
198-
private void UpdateContainer()
199-
{
200-
_currentScene = SceneManager.GetActiveScene();
201-
_sceneContainer = _projectContainer.Resolve<SceneContextRegistry>()
202-
.GetContainerForScene(_currentScene);
203-
}
204-
#endif
205177
}
206178
}

Runtime/PoolExtensions.cs

Lines changed: 0 additions & 50 deletions
This file was deleted.

Runtime/PoolHelper.cs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using UnityEngine;
2+
3+
namespace ToolBox.Pools
4+
{
5+
public static class PoolHelper
6+
{
7+
public static void Populate(this GameObject prefab, int count)
8+
{
9+
Pool.GetPrefabPool(prefab).Populate(count);
10+
}
11+
12+
public static GameObject Reuse(this GameObject prefab)
13+
{
14+
return Pool.GetPrefabPool(prefab).Reuse();
15+
}
16+
17+
public static GameObject Reuse(this GameObject prefab, Transform parent)
18+
{
19+
return Pool.GetPrefabPool(prefab).Reuse(parent);
20+
}
21+
22+
public static GameObject Reuse(this GameObject prefab, Transform parent, bool worldPositionStays)
23+
{
24+
return Pool.GetPrefabPool(prefab).Reuse(parent, worldPositionStays);
25+
}
26+
27+
public static GameObject Reuse(this GameObject prefab, Vector3 position, Quaternion rotation)
28+
{
29+
return Pool.GetPrefabPool(prefab).Reuse(position, rotation);
30+
}
31+
32+
public static GameObject Reuse(this GameObject prefab, Vector3 position, Quaternion rotation, Transform parent)
33+
{
34+
return Pool.GetPrefabPool(prefab).Reuse(position, rotation, parent);
35+
}
36+
37+
public static T Reuse<T>(this GameObject prefab) where T : Component
38+
{
39+
return prefab.Reuse().GetComponent<T>();
40+
}
41+
42+
public static T Reuse<T>(this GameObject prefab, Transform parent) where T : Component
43+
{
44+
return prefab.Reuse(parent).GetComponent<T>();
45+
}
46+
47+
public static T Reuse<T>(this GameObject prefab, Transform parent, bool worldPositionStays) where T : Component
48+
{
49+
return prefab.Reuse(parent, worldPositionStays).GetComponent<T>();
50+
}
51+
52+
public static T Reuse<T>(this GameObject prefab, Vector3 position, Quaternion rotation) where T : Component
53+
{
54+
return prefab.Reuse(position, rotation).GetComponent<T>();
55+
}
56+
57+
public static T Reuse<T>(this GameObject prefab, Vector3 position, Quaternion rotation, Transform parent) where T : Component
58+
{
59+
return prefab.Reuse(position, rotation, parent).GetComponent<T>();
60+
}
61+
62+
public static void Release(this GameObject instance)
63+
{
64+
bool isPooled = Pool.TryGetInstancePool(instance, out var pool);
65+
66+
if (isPooled)
67+
{
68+
pool.Release(instance);
69+
}
70+
else
71+
{
72+
Object.Destroy(instance);
73+
}
74+
}
75+
}
76+
}
File renamed without changes.

Runtime/Poolable.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ private void Awake()
1515
_isInitialized = true;
1616
}
1717

18-
public void OnGet()
18+
public void OnReuse()
1919
{
20-
if (_isInitialized)
21-
for (int i = 0; i < _poolables.Length; i++)
22-
_poolables[i].OnGet();
20+
if (!_isInitialized)
21+
return;
22+
23+
for (int i = 0; i < _poolables.Length; i++)
24+
_poolables[i].OnReuse();
2325
}
2426

2527
public void OnRelease()

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.intothedev.objectpooling",
3-
"version": "1.7.0",
3+
"version": "1.8.0",
44
"displayName": "Object Pooling",
55
"description": "Object Pooling for Unity.",
66
"author": {

0 commit comments

Comments
 (0)