[Zenject]Installers学习笔记

来源:互联网 发布:在线app打包网站源码 编辑:程序博客网 时间:2024/06/05 21:30

MonoBehaviour

1、需要继承MonoInstaller
2、MonoInstaller是继承于MonoBechaviou
3、通过重写InstallBindings方法添加依赖关系
4、Installer必须添加在Context(通常指SceneContext)

eg:

public class ClassName: MonoInstaller{    public override viod InstallBindings()    {        \\依赖关系    }}

Non-MonoBehaviour

1、需要继承Installer
2、通过重写InstallBindings方法添加依赖关系
3、无需添加到Scene

eg:

public class ClassName : Installer<ClassName>{    public override viod InstallBindings()    {        \\依赖关系    }}

Scriptable Object Installer

1、通常用于储存游戏配置信息
2、进行调试模式时,其修改后的属性会被保留
3、通过重写InstallBindings方法添加依赖关系

eg:

public class Settings : ScriptableObjectInstaller{    public Something.Settings Something;    public override viod InstallBindings()    {        Container.BindInstance(Something);    }}public class Something{    readonly Settings _settings;    public Something(Settings settings)    {        _settings = settings;    }    [Serializable]    public class Settings    {        public int number;    }

问题集

how would we re-use a MonoInstaller in multiple scenes?

There are three ways to do this.

Prefab instances within the scene. After attaching your MonoInstaller to a gameobject in your scene, you can then create a prefab out of it. This is nice because it allows you to share any configuration that you’ve done in the inspector on the MonoInstaller across scenes (and also have per-scene overrides if you want). After adding it in your scene you can then drag and drop it on to the Installers property of a Context

Prefabs. You can also directly drag your installer prefab from the Project tab into the InstallerPrefabs property of SceneContext. Note that in this case you cannot have per-scene overrides like you can when having the prefab instantiated in your scene, but can be nice to avoid clutter in the scene.

Prefabs within Resources folder. You can also place your installer prefabs underneath a Resoures folder and install them directly from code by using the Resources path. For details on usage see here.

0 0
原创粉丝点击