zenJect

Zenject框架(五)

2019-03-12  本文已影响0人  虫小白
  1. FromComponentsInHierarchy- FromComponentInHierarchy 的多组件版本
  2. FromComponentSibling - 在当前物体的各组件中查找给定的组件
Container.Bind<Foo>().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<Foo>().FromNewComponentOnRoot();
  1. FromResource - 通过调用Unity的Resources.Load创建ResultType类型对象,可以用于加载Resources.Load可以加载的对象,比如textures, sounds, prefabs, 等等.
Container.Bind<Texture>().WithId("Glass").FromResource("Some/Path/Glass");
  1. FromResources - FromResource 的多值版本
  2. FromScriptableObjectResource - 直接绑定到给定资源路径上的scriptable物体的实例上。注意:在Unity编辑器中对此值的更改将被永久保存。如果这不是你想要的,请使用FromNewScriptableObjectResource。
public class Foo : ScriptableObject
{
}

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

public interface IBar : IFoo
{
}

public class Foo : IBar
{
}

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

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

Container.Bind<Foo>();
Container.Bind<Bar>().FromResolveGetter<Foo>(x => x.GetBar());
  1. FromResolveAllGetter<ObjectType> -和FromResolveGetter<ObjectType> 一样但会匹配多个或0个值
  2. FromSubContainerResolve-在子容器中查找以获取ResultType实例,注意,要使用该方法,子容器中必须存在对ResultType的绑定。这种方法非常强大,因为它允许您将相关的依赖项组合在一个迷你容器中,然后只暴露某些类(也称为“Facades”)以在更高级别对这组依赖项进行操作。有关使用子容器的更多详细信息,请见后续。有几种方法可以定义子容器:
Container.Bind<Foo>().FromSubContainerResolve().ByNewPrefabMethod(MyPrefab, InstallFoo);

void InstallFoo(DiContainer subContainer)
{
       subContainer.Bind<Foo>();
}
Container.Bind<Foo>().FromSubContainerResolve().ByNewPrefabInstaller<FooInstaller>(MyPrefab);

class FooInstaller : Installer
{
        public override void InstallBindings()
        {
            Container.Bind<Foo>();
        }
}
Container.Bind<Foo>().FromSubContainerResolve().ByNewPrefabResourceMethod("Path/To/MyPrefab", InstallFoo);

void InstallFoo(DiContainer subContainer)
{
    subContainer.Bind<Foo>();
}
Container.Bind<Foo>().FromSubContainerResolve().ByNewPrefabResourceInstaller<FooInstaller>("Path/To/MyPrefab");

class FooInstaller : MonoInstaller
{
  public override void InstallBindings()
   {
        Container.Bind<Foo>();
    }
}
Container.Bind<Foo>().FromSubContainerResolve().ByMethod(InstallFooFacade);

void InstallFooFacade(DiContainer subContainer)
{
    subContainer.Bind<Foo>();
}

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

Container.Bind<Foo>().FromSubContainerResolve().ByMethod(InstallFooFacade).WithKernel();

void InstallFooFacade(DiContainer subContainer)
{
    subContainer.Bind<Foo>();
    subContainer.Bind<ITickable>().To<Bar>();
}
Container.Bind<Foo>().FromSubContainerResolve().ByInstaller<FooFacadeInstaller>();

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

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

Container.Bind<Foo>().FromSubContainerResolve().ByInstaller<FooFacadeInstaller>().WithKernel();
Container.Bind<Foo>().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<Foo>();
    }
}
Container.Bind<Foo>().FromSubContainerResolve().ByNewContextPrefabResource("Path/To/MyPrefab");
  1. FromSubContainerResolveAll -和FromSubContainerResolve类似但会匹配多个或0个值
上一篇 下一篇

猜你喜欢

热点阅读