Saturday, November 5, 2022
HomeGame Developmentunity - Zenject: Inject into monobehavior

unity – Zenject: Inject into monobehavior


I wish to begin utilizing zenject in my challenge however I encountered a problem.
From their tutorial in github web page I used to be capable of inject non monobehavior objects into one another like this.

public class MyInstaller : MonoInstaller {
     public override void InstallBindings() {
         Container.Bind<string>().FromInstance("Hey World!");
         Container.Bind<Greeter>().AsSingle().NonLazy();
         Container.Bind<BasicArmorDamageCalculator>().AsSingle().NonLazy();
     }
}

public class Greeter {

    public Greeter(String message) {
        Debug.Log("this was injected: " + message);
    }
}

However the issue is that I can’t inject objects to Monobehavior lessons.
Utilizing this Installer I attempted to inject Greeter to monobehavior class like this

[Zenject.Inject]
public void Assemble(Greeter greeter) { 
    Debug.log(greeter);
}

And the greeter variable is null. As I perceive I’ve to register my monobehavior class within the Installer class however It requires instantiating it whereas mono class is already on some recreation object.

What’s the answer to this one?

Thanks upfront!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments