How to use a dependency injection mechanism between scenes
For instance, I have a scene with 2 game objects. The first has CButton component and its name "Button01", the second one has CActor with name "Actor01". CActor is an abstract class with two public methods Activate and Deactivate. So the button's component is dependent of CActor object. I want to inject this dependency into CButton component automatically using a dependency injection. Let another game object is placed at the scene with CCompositionRoot component. It's the place where I initialize some container and other dependencies. All dependencies are related with the first scene.
And now the question is how should I work with dependencies on other scenes which will be loaded after the first one? Is there some configuration file of scene's dependencies? Because an hirerarchy of a scene can be changed. And names of object can be changed too. So how to resolve this cases?
Answer by eventropy · Nov 22, 2016 at 04:02 PM
Are you using Zenject? If so, you could do this by adding a ZenjectBinding component to the actor object. Then the actor object should be injected properly into the button component.
As for resolving dependencies between scenes, assuming again that you are using Zenject, your options are either scene decorators or scene contract names (see docs for details)
I didn't use DI container in my prototype. All depdendencies were resolved manually. Now I think to try Zenject. So, I have a few scenes in my case. They are loaded separately. Therefore all dependencies of a particular scene are unique for it. The first scene has one button ("Button01") and one actor ("Door01"). The second one contains a button ("Button01"), but an actor, which should attached to it, has name ("Block01"). Can I specify the name of dependency for CButton component in the editor ("Door01" of CActor type in the first case and "Block01" of CActor type in the second one) ? And thanks for the previous answer!
Your answer
Follow this Question
Related Questions
How to make a surface glow when its hit by a laser. 0 Answers
C# for applications vs C# for Unity help 3 Answers
how to use class or struct in unity api? 1 Answer
Spawn a dot for every number typed? 0 Answers