11using System . Collections . Generic ;
22using UnityEngine ;
3- #if ZENJECT
4- using UnityEngine . SceneManagement ;
5- using Zenject ;
6- #endif
73
84namespace 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}
0 commit comments