1
1
using System . Collections . Generic ;
2
2
using UnityEngine ;
3
- #if ZENJECT
4
- using UnityEngine . SceneManagement ;
5
- using Zenject ;
6
- #endif
7
3
8
4
namespace ToolBox . Pools
9
5
{
@@ -13,16 +9,11 @@ internal sealed class Pool
13
9
private readonly Stack < Poolable > _instances = null ;
14
10
private readonly Quaternion _rotation = default ;
15
11
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
+
22
13
private static readonly Dictionary < GameObject , Pool > _prefabLookup = new Dictionary < GameObject , Pool > ( 64 ) ;
23
14
private static readonly Dictionary < GameObject , Pool > _instanceLookup = new Dictionary < GameObject , Pool > ( 512 ) ;
24
15
25
- private const int CAPACITY = 128 ;
16
+ private const int INITIAL_SIZE = 128 ;
26
17
27
18
public Pool ( GameObject prefab )
28
19
{
@@ -34,12 +25,8 @@ public Pool(GameObject prefab)
34
25
Object . DontDestroyOnLoad ( _prefab ) ;
35
26
_prefab . gameObject . SetActive ( false ) ;
36
27
}
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 ) ;
43
30
_prefabLookup . Add ( prefab , this ) ;
44
31
45
32
var transform = prefab . transform ;
@@ -57,8 +44,10 @@ public static Pool GetPrefabPool(GameObject prefab)
57
44
return pool ;
58
45
}
59
46
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
+ }
62
51
63
52
public void Populate ( int count )
64
53
{
@@ -70,14 +59,14 @@ public void Populate(int count)
70
59
}
71
60
}
72
61
73
- public GameObject Get ( )
62
+ public GameObject Reuse ( )
74
63
{
75
64
var instance = GetInstance ( ) ;
76
65
77
66
return instance . gameObject ;
78
67
}
79
68
80
- public GameObject Get ( Transform parent )
69
+ public GameObject Reuse ( Transform parent )
81
70
{
82
71
var instance = GetInstance ( ) ;
83
72
@@ -86,7 +75,7 @@ public GameObject Get(Transform parent)
86
75
return instance . gameObject ;
87
76
}
88
77
89
- public GameObject Get ( Transform parent , bool worldPositionStays )
78
+ public GameObject Reuse ( Transform parent , bool worldPositionStays )
90
79
{
91
80
var instance = GetInstance ( ) ;
92
81
@@ -95,7 +84,7 @@ public GameObject Get(Transform parent, bool worldPositionStays)
95
84
return instance . gameObject ;
96
85
}
97
86
98
- public GameObject Get ( Vector3 position , Quaternion rotation )
87
+ public GameObject Reuse ( Vector3 position , Quaternion rotation )
99
88
{
100
89
var instance = GetInstance ( ) ;
101
90
@@ -104,7 +93,7 @@ public GameObject Get(Vector3 position, Quaternion rotation)
104
93
return instance . gameObject ;
105
94
}
106
95
107
- public GameObject Get ( Vector3 position , Quaternion rotation , Transform parent )
96
+ public GameObject Reuse ( Vector3 position , Quaternion rotation , Transform parent )
108
97
{
109
98
var instance = GetInstance ( ) ;
110
99
var instanceTransform = instance . transform ;
@@ -148,7 +137,7 @@ private Poolable GetInstance()
148
137
149
138
if ( instance != null )
150
139
{
151
- instance . OnGet ( ) ;
140
+ instance . OnReuse ( ) ;
152
141
instance . gameObject . SetActive ( true ) ;
153
142
154
143
return instance ;
@@ -162,13 +151,11 @@ private Poolable GetInstance()
162
151
163
152
return instance ;
164
153
}
165
- else
166
- {
167
- instance . OnGet ( ) ;
168
- instance . gameObject . SetActive ( true ) ;
169
154
170
- return instance ;
171
- }
155
+ instance . OnReuse ( ) ;
156
+ instance . gameObject . SetActive ( true ) ;
157
+
158
+ return instance ;
172
159
}
173
160
else
174
161
{
@@ -184,23 +171,8 @@ private Poolable CreateInstance()
184
171
var instance = Object . Instantiate ( _prefab ) ;
185
172
var instanceGameObject = instance . gameObject ;
186
173
_instanceLookup . Add ( instanceGameObject , this ) ;
187
- #if ZENJECT
188
- if ( ! _currentScene . isLoaded )
189
- UpdateContainer ( ) ;
190
-
191
- _sceneContainer . InjectGameObject ( instanceGameObject ) ;
192
- #endif
193
174
194
175
return instance ;
195
176
}
196
-
197
- #if ZENJECT
198
- private void UpdateContainer ( )
199
- {
200
- _currentScene = SceneManager . GetActiveScene ( ) ;
201
- _sceneContainer = _projectContainer . Resolve < SceneContextRegistry > ( )
202
- . GetContainerForScene ( _currentScene ) ;
203
- }
204
- #endif
205
177
}
206
178
}
0 commit comments