Zenject框架(五)

  1. FromComponentsInHierarchy- FromComponentInHierarchy 的多组件版本
  2. FromComponentSibling - 在当前物体的各组件中查找给定的组件
Container.Bind().FromComponentSibling();

在这种情况下,ResultType 必须派生自UnityEngine.MonoBehaviour / UnityEngine.Component
如果非派生自Monobehaviour的类要求给定类型时,会抛出异常,因为这时不存在current transform

  1. FromComponentsSibling -和FromComponentSibling一样,但可返回多个或个值
  2. FromComponentInParents -在当前物体及其父物体上查找给定组件
  3. FromComponentsInParents -FromComponentInParents的多组件版本
  4. FromComponentInChildren - 在当前物体及其子物体上查找给定组件,存在多个匹配时,返回第一个匹配项,类似GetComponentInChildren
  5. FromComponentsInChildren -FromComponentInChildren的多组件版本,类似GetComponentsInChildren
  6. FromNewComponentOnRoot - 在当前上下文根部实例化给定组件,大部分时候用于游戏对象上下文
Container.Bind().FromNewComponentOnRoot();
  1. FromResource - 通过调用Unity的Resources.Load创建ResultType类型对象,可以用于加载Resources.Load可以加载的对象,比如textures, sounds, prefabs, 等等.
Container.Bind().WithId("Glass").FromResource("Some/Path/Glass");
  1. FromResources - FromResource 的多值版本
  2. FromScriptableObjectResource - 直接绑定到给定资源路径上的scriptable物体的实例上。注意:在Unity编辑器中对此值的更改将被永久保存。如果这不是你想要的,请使用FromNewScriptableObjectResource。
public class Foo : ScriptableObject
{
}

Container.Bind().FromScriptableObjectResource("Some/Path/Foo");
  1. FromNewScriptableObjectResource - 和FromScriptableObjectResource类似但会复制给定路径的scriptable物体,如果您希望有给定的scriptable物体的多个不同实例,或者您希望确保在运行时所做的更改不会影响scriptable对象的已保存的值,则此选项非常有用。
  2. FromResolve - 通过对容器执行另一次查找来获取实例(换句话说,调用DiContainer.Resolve())。请注意,要使用该方法,ResultType必须绑定在单独的绑定语句中。当您想要将接口绑定到另一个接口时,此构造方法特别有用,如下面的示例所示
public interface IFoo
{
}

public interface IBar : IFoo
{
}

public class Foo : IBar
{
}

Container.Bind().To().FromResolve();
Container.Bind().To();
  1. FromResolveAll-和FromResolve一样但会匹配多个或0个值
  2. FromResolveGetter<ObjectType> - 从另一个依赖项的属性中获取实例,该依赖项是通过对容器执行另一次查找获得的(换句话说,调用DiContainer.Resolve()然后访问返回的ResultType类型实例上的值)。请注意,要使用该方法,ObjectType必须绑定在单独的绑定语句中。
public class Bar
{
}

public class Foo
{
    public Bar GetBar()
    {
        return new Bar();
    }
}

Container.Bind();
Container.Bind().FromResolveGetter(x => x.GetBar());
  1. FromResolveAllGetter -和FromResolveGetter 一样但会匹配多个或0个值
  2. FromSubContainerResolve-在子容器中查找以获取ResultType实例,注意,要使用该方法,子容器中必须存在对ResultType的绑定。这种方法非常强大,因为它允许您将相关的依赖项组合在一个迷你容器中,然后只暴露某些类(也称为“Facades”)以在更高级别对这组依赖项进行操作。有关使用子容器的更多详细信息,请见后续。有几种方法可以定义子容器:
  • ByNewPrefabMethod -通过实例化新的预设体来初始化子容器。注意,和ByNewContextPrefab不同,该绑定不要求在预设体上有物体上下文(GameObjectContext),在这种情况下,物体上下文会动态的添加然后以给定的installer方法运行
Container.Bind().FromSubContainerResolve().ByNewPrefabMethod(MyPrefab, InstallFoo);

void InstallFoo(DiContainer subContainer)
{
       subContainer.Bind();
}
  • ByNewPrefabInstaller- 通过实例化新的预设体实例子容器,和ByNewPrefabMethod类似,但是它是通过给定的installer初始化动态创建的物体上下文而不是通过一个方法
Container.Bind().FromSubContainerResolve().ByNewPrefabInstaller(MyPrefab);

class FooInstaller : Installer
{
        public override void InstallBindings()
        {
            Container.Bind();
        }
}
  • ByNewPrefabResourceMethod -通过实例化Resources.Load获取的预制件来初始化子容器。请注意,与ByNewPrefabResource绑定方法不同,该绑定不要求在预设体上有物体上下文(GameObjectContext),在这种情况下,物体上下文会动态的添加然后以给定的installer方法运行
Container.Bind().FromSubContainerResolve().ByNewPrefabResourceMethod("Path/To/MyPrefab", InstallFoo);

void InstallFoo(DiContainer subContainer)
{
    subContainer.Bind();
}
  • ByNewPrefabResourceInstaller -通过实例化Resources.Load获取的预制件来初始化子容器。和ByNewPrefabResourceMethod类似,但是它是通过给定的installer初始化动态创建的物体上下文而不是通过一个方法
Container.Bind().FromSubContainerResolve().ByNewPrefabResourceInstaller("Path/To/MyPrefab");

class FooInstaller : MonoInstaller
{
  public override void InstallBindings()
   {
        Container.Bind();
    }
}
  • ByNewGameObjectInstaller -通过实例化一个新的空游戏对象,添加游戏对象上下文,然后安装给定的installer来初始化子容器,该方法和ByInstaller非常类似但还有一下优点:
    • 任何ITickable,IInitializable,IDisposable等绑定都将被正确调用
    • 在子容器内实例化的任何新游戏对象都将成为游戏对象上下文对象下的父对象
    • 您可以通过销毁游戏对象上下文的游戏对象来销毁子容器
  • ByNewGameObjectMethod - 和ByNewGameObjectInstaller类似但子容器通过给定的方法而不是installer类型被初始化。
  • ByMethod - 通过一个方法初始化子容器
Container.Bind().FromSubContainerResolve().ByMethod(InstallFooFacade);

void InstallFooFacade(DiContainer subContainer)
{
    subContainer.Bind();
}

注意,使用ByMethod时,如果你想在子容器中使用zenject的接口比如ITickable, IInitializable, IDisposable ,你就必须同时使用WithKernel 绑定方法,如下:

Container.Bind().FromSubContainerResolve().ByMethod(InstallFooFacade).WithKernel();

void InstallFooFacade(DiContainer subContainer)
{
    subContainer.Bind();
    subContainer.Bind().To();
}
  • ByInstaller -通过使用Installer的子类来初始化子容器。这是ByMethod的更清晰更不易出错的替代方案,尤其是你需要把数据注入到Installer自身中的时候。不易出错是因为在使用ByMethod时,通常会在方法中意外使用Container而不是subContainer。
Container.Bind().FromSubContainerResolve().ByInstaller();

class FooFacadeInstaller : Installer
{
    public override void InstallBindings()
    {
        Container.Bind();
    }
}

注意,使用ByInstaller时,如果你想在子容器中使用zenject的接口比如ITickable, IInitializable, IDisposable ,你就必须同时使用WithKernel 绑定方法,如下:

Container.Bind().FromSubContainerResolve().ByInstaller().WithKernel();
  • ByNewContextPrefab -通过实例化新的预制体来初始化子容器。请注意,预制体根部必须包含GameObjectContext组件。有关GameObjectContext的详细信息请见后续。
Container.Bind().FromSubContainerResolve().ByNewContextPrefab(MyPrefab);

// Assuming here that this installer is added to the GameObjectContext at the root
// of the prefab.  You could also use a ZenjectBinding in the case where Foo is a MonoBehaviour
class FooFacadeInstaller : MonoInstaller
{
    public override void InstallBindings()
    {
        Container.Bind();
    }
}
  • ByNewContextPrefabResource -通过实例化Resources.Load获取的预制体来初始化子容器。请注意,预制体根部必须包含GameObjectContext组件。
Container.Bind().FromSubContainerResolve().ByNewContextPrefabResource("Path/To/MyPrefab");
  • ByInstance -直接使用您自己提供的给定DiContainer的实例来初始化子容器。很少使用。
  1. FromSubContainerResolveAll -和FromSubContainerResolve类似但会匹配多个或0个值

你可能感兴趣的:(Zenject框架(五))