1
1
using System . Collections . Generic ;
2
2
using UnityEngine ;
3
3
#if ZENJECT
4
+ using UnityEngine . SceneManagement ;
4
5
using Zenject ;
5
6
#endif
6
7
@@ -14,6 +15,8 @@ internal sealed class Pool
14
15
private readonly Vector3 _scale = default ;
15
16
#if ZENJECT
16
17
private readonly DiContainer _projectContainer = null ;
18
+ private DiContainer _sceneContainer = null ;
19
+ private Scene _currentScene = default ;
17
20
#endif
18
21
19
22
private static readonly Dictionary < GameObject , Pool > _prefabLookup = new Dictionary < GameObject , Pool > ( 64 ) ;
@@ -34,6 +37,7 @@ public Pool(GameObject prefab)
34
37
35
38
#if ZENJECT
36
39
_projectContainer = ProjectContext . Instance . Container ;
40
+ UpdateContainer ( ) ;
37
41
#endif
38
42
_instances = new Stack < Poolable > ( CAPACITY ) ;
39
43
_prefabLookup . Add ( prefab , this ) ;
@@ -177,13 +181,22 @@ private Poolable CreateInstance()
177
181
var instanceGameObject = instance . gameObject ;
178
182
_instanceLookup . Add ( instanceGameObject , this ) ;
179
183
#if ZENJECT
180
- _projectContainer
181
- . Resolve < SceneContextRegistry > ( )
182
- . GetContainerForScene ( instanceGameObject . scene )
183
- . InjectGameObject ( instanceGameObject ) ;
184
+ if ( ! _currentScene . isLoaded )
185
+ UpdateContainer ( ) ;
186
+
187
+ _sceneContainer . InjectGameObject ( instanceGameObject ) ;
184
188
#endif
185
189
186
190
return instance ;
187
191
}
192
+
193
+ #if ZENJECT
194
+ private void UpdateContainer ( )
195
+ {
196
+ _currentScene = SceneManager . GetActiveScene ( ) ;
197
+ _sceneContainer = _projectContainer . Resolve < SceneContextRegistry > ( )
198
+ . GetContainerForScene ( _currentScene ) ;
199
+ }
200
+ #endif
188
201
}
189
202
}
0 commit comments