Zenject
Zenject copied to clipboard
Loading multiple scenes as children of current scene concurrently does not work
Describe the bug As already described by @svermeulen himself in this issue https://github.com/modesttree/Zenject/issues/375 it is still not possible to load multiple scenes Additive and bind objects within them. It will only work for the first scene added, all others which follow will not bind correctly.
It appears to work fine when I am using contract names but it should be documented somewhere that this issue still exists.
To Reproduce See linked issue, sample code provided.
Expected behavior Bindings should work in both scenes.
Screenshots
Extenject and Unity info (please complete the following information):
- Zenject version: 9.1.0
- Unity version: 2020.1.0f1
- Project's scripting backend [e.g. Mono/IL2CPP]: Mono
In the meanwhile I stumbled over this again as I had to abandon the scene contract name approach because of some other issue: https://gitter.im/Extenject/community?at=5fc3b8d90219b46a2667fd2e
And I believe I know now why this initial issue happens when you load multiple scenes additive as child:
zenjectSceneLoader.LoadScene("Scene1", LoadSceneMode.Additive, null, LoadSceneRelationship.Child);
zenjectSceneLoader.LoadScene("Scene2", LoadSceneMode.Additive, null, LoadSceneRelationship.Child);
Unitys SceneManager.LoadScene
only loads the Scene in the following frame and only then will SceneContext read SceneContext.ParentContainers
and also set it to null: https://github.com/svermeulen/Extenject/blob/master/UnityProject/Assets/Plugins/Zenject/Source/Install/Contexts/SceneContext.cs#L162
Following that Unity attempts to load the second scene previously scheduled but at this point SceneContext.ParentContainers
is already null so the bindings will not be available in this sub-scene.
At the moment I load the scenes in a sub-sequential way to work around this issue. So SceneContext.ParentContainers will be set again only after the first scene completed loading (like seen here https://github.com/svermeulen/Extenject/blob/master/UnityProject/Assets/Plugins/Zenject/Source/Util/ZenjectSceneLoader.cs#L100).
var sceneLoader1 = zenjectSceneLoader.LoadSceneAsync("Scene1", LoadSceneMode.Additive, null, LoadSceneRelationship.Child);
sceneLoader1.completed += operation =>
{
zenjectSceneLoader.LoadScene("Scene2", LoadSceneMode.Additive, null, LoadSceneRelationship.Child);
};