I started to learn Zenject + Unity. I learned the Zenject readme but I have no unity experience I need shared data between scenes and I want to have some code over the scene. I try to inject some GameData class in two scenes. My scenes have GameObjects with code where I use injections. The first scene has the installer and the first scene loads the second scene as an additive I make bind so:
public class MainInstaller : MonoInstaller
{
public override void InstallBindings()
{
Container.Bind<GameData>().AsSingle().NonLazy();
}
}
I guess that I will have one instance GameData
First scene:
private GameData _gameData;
[Inject]
public void Construct(GameData gameData)
{
_gameData = gameData;
SceneManager.LoadScene("Menu", LoadSceneMode.Additive);
}
private void Start()
{
_gameData.CurrentState = GameStates.Menu; // Makes some changes
}
Second scene
private GameData _gameData;
[Inject]
public void Construct(GameData gameData)
{
_gameData = gameData;
}
The injection works ok. But I don't see my changes in log. And I think that exist two instance of GameDate.
If second scene do't have another context, bind is taken over i think. But if not, I'm afraid maybe I don't graps your project current situation.
When bind is't taken over, you have other aproach. standard way to share the Bind between two (or more) scenes.
1.Project Context
If now you use scene context, and Main Installer is attached it, you can create ProjectContext. And you can make ProjectContext bind GameData instead.
(Maybe Main Installer's InstallBindings() Method become empty. But empty method and installer is required)
https://github.com/modesttree/Zenject#global-bindings-using-project-context
2.Context Parenting
This is easier way but remain Scope issue. because ProjectContext is global.
If scope issue annoy you, Scene Context Parenting is another way.
https://github.com/modesttree/Zenject#scene-parenting-using-contract-names
=supplement=
Zenject's bindng scope follow context.
Context has some types, ProjectContext , SceneContext, GameObjectContext.
Project Context is always top and global.
SeneContext is lower level than ProjectContext. And it can be other SeneContext's parent (or child).
GameContext is lower level than SceneContext.
Hope this answer helps.