@@ -5,60 +5,97 @@ namespace ToolBox.Pools
5
5
{
6
6
internal sealed class Pool
7
7
{
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 ;
12
15
13
16
private static readonly Dictionary < GameObject , Pool > _prefabLookup = new Dictionary < GameObject , Pool > ( 64 ) ;
14
17
private static readonly Dictionary < GameObject , Pool > _instanceLookup = new Dictionary < GameObject , Pool > ( 512 ) ;
15
18
16
- private const int INITIAL_SIZE = 128 ;
19
+ private const int InitialSize = 128 ;
17
20
18
21
public Pool ( GameObject prefab )
19
22
{
20
- _prefab = prefab . GetComponent < Poolable > ( ) ;
23
+ _source = prefab ;
24
+ _prototype = prefab . GetComponent < Poolable > ( ) ;
21
25
22
- if ( _prefab == null )
26
+ if ( _prototype == null )
23
27
{
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 ;
27
32
}
28
33
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 ) ;
31
37
32
38
var transform = prefab . transform ;
33
39
_rotation = transform . rotation ;
34
40
_scale = transform . localScale ;
35
41
}
36
42
37
- public static Pool GetPrefabPool ( GameObject prefab )
43
+ public static Pool GetPoolByPrefab ( GameObject prefab , bool create = true )
38
44
{
39
- bool hasPool = _prefabLookup . TryGetValue ( prefab , out var pool ) ;
45
+ var hasPool = _prefabLookup . TryGetValue ( prefab , out var pool ) ;
40
46
41
- if ( ! hasPool )
47
+ if ( ! hasPool && create )
42
48
pool = new Pool ( prefab ) ;
43
49
44
50
return pool ;
45
51
}
46
52
47
- public static bool TryGetInstancePool ( GameObject instance , out Pool pool )
53
+ public static bool GetPoolByInstance ( GameObject instance , out Pool pool )
48
54
{
49
55
return _instanceLookup . TryGetValue ( instance , out pool ) ;
50
56
}
51
57
58
+ public static void Remove ( GameObject instance )
59
+ {
60
+ _instanceLookup . Remove ( instance ) ;
61
+ }
62
+
52
63
public void Populate ( int count )
53
64
{
54
- for ( int i = 0 ; i < count ; i ++ )
65
+ for ( var i = 0 ; i < count ; i ++ )
55
66
{
56
67
var instance = CreateInstance ( ) ;
57
68
instance . gameObject . SetActive ( false ) ;
58
69
_instances . Push ( instance ) ;
59
70
}
60
71
}
61
72
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
+
62
99
public GameObject Reuse ( )
63
100
{
64
101
var instance = GetInstance ( ) ;
@@ -121,7 +158,7 @@ public void Release(GameObject instance)
121
158
122
159
private Poolable GetInstance ( )
123
160
{
124
- int count = _instances . Count ;
161
+ var count = _instances . Count ;
125
162
126
163
if ( count != 0 )
127
164
{
@@ -168,9 +205,11 @@ private Poolable GetInstance()
168
205
169
206
private Poolable CreateInstance ( )
170
207
{
171
- var instance = Object . Instantiate ( _prefab ) ;
208
+ var instance = Object . Instantiate ( _prototype ) ;
172
209
var instanceGameObject = instance . gameObject ;
210
+
173
211
_instanceLookup . Add ( instanceGameObject , this ) ;
212
+ _allInstances . Add ( instanceGameObject ) ;
174
213
175
214
return instance ;
176
215
}
0 commit comments