Zenject: Installer

yoonsang lee·2022년 5월 29일
0

there is some collections of related bindings for each sub-system and so it makes sense to group those bindings into a re-usable object. In Zenject this re-usable object is called an installer.

public class FooInstaller : MonoInstaller {
    public override void InstallBindings() {
        Container.Bind<Bar>().AsSingle();
        Container.BindInterfacesTo<Foo>().AsSingle();
        // etc...
    }
}
  • it must be attached to the Installers property of the SceneContext object to be triggered.

using other Installers without attaching to GameObject in Unity Scenes

  • Installers are installed in the order given to SceneContext (with scriptable object installers first, then mono installers, then prefab installers)
public class BarInstaller : Installer<BarInstaller> {
    public override void InstallBindings() {
        ...
    }
}

public class FooInstaller : MonoInstaller {
    public override void InstallBindings() {
        BarInstaller.Install(Container);
    }
}
  • BarInstaller is of type Installer<> (note the generic arguments) and not MonoInstaller, which is why we can simply call BarInstaller.Install(Container) and don't require that BarInstaller be added to our scene already.

  • the reason why we use installers as opposed to just having all our bindings declared all at once for each scene, is to make them re-usable.


How to use multiple installers through multiple scene layers?

use installer as a prefab or use ScriptableObjectInstaller

profile
Unity3D Freelancer Programmer + React

0개의 댓글