Skip to content

Commit bc9f1b2

Browse files
committed
Zenject version optimization
1 parent efbb38d commit bc9f1b2

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

Runtime/Pool.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.Generic;
22
using UnityEngine;
33
#if ZENJECT
4+
using UnityEngine.SceneManagement;
45
using Zenject;
56
#endif
67

@@ -14,6 +15,8 @@ internal sealed class Pool
1415
private readonly Vector3 _scale = default;
1516
#if ZENJECT
1617
private readonly DiContainer _projectContainer = null;
18+
private DiContainer _sceneContainer = null;
19+
private Scene _currentScene = default;
1720
#endif
1821

1922
private static readonly Dictionary<GameObject, Pool> _prefabLookup = new Dictionary<GameObject, Pool>(64);
@@ -34,6 +37,7 @@ public Pool(GameObject prefab)
3437

3538
#if ZENJECT
3639
_projectContainer = ProjectContext.Instance.Container;
40+
UpdateContainer();
3741
#endif
3842
_instances = new Stack<Poolable>(CAPACITY);
3943
_prefabLookup.Add(prefab, this);
@@ -177,13 +181,22 @@ private Poolable CreateInstance()
177181
var instanceGameObject = instance.gameObject;
178182
_instanceLookup.Add(instanceGameObject, this);
179183
#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);
184188
#endif
185189

186190
return instance;
187191
}
192+
193+
#if ZENJECT
194+
private void UpdateContainer()
195+
{
196+
_currentScene = SceneManager.GetActiveScene();
197+
_sceneContainer = _projectContainer.Resolve<SceneContextRegistry>()
198+
.GetContainerForScene(_currentScene);
199+
}
200+
#endif
188201
}
189202
}

0 commit comments

Comments
 (0)