Zenject框架(五)

  1. FromComponentsInHierarchy- FromComponentInHierarchy 的多組件版本
  2. FromComponentSibling - 在當(dāng)前物體的各組件中查找給定的組件
Container.Bind<Foo>().FromComponentSibling();

在這種情況下,ResultType 必須派生自UnityEngine.MonoBehaviour / UnityEngine.Component
如果非派生自Monobehaviour的類(lèi)要求給定類(lèi)型時(shí),會(huì)拋出異常,因?yàn)檫@時(shí)不存在current transform

  1. FromComponentsSibling -和FromComponentSibling一樣,但可返回多個(gè)或個(gè)值
  2. FromComponentInParents -在當(dāng)前物體及其父物體上查找給定組件
  3. FromComponentsInParents -FromComponentInParents的多組件版本
  4. FromComponentInChildren - 在當(dāng)前物體及其子物體上查找給定組件,存在多個(gè)匹配時(shí),返回第一個(gè)匹配項(xiàng),類(lèi)似GetComponentInChildren
  5. FromComponentsInChildren -FromComponentInChildren的多組件版本,類(lèi)似GetComponentsInChildren
  6. FromNewComponentOnRoot - 在當(dāng)前上下文根部實(shí)例化給定組件,大部分時(shí)候用于游戲?qū)ο笊舷挛?/li>
Container.Bind<Foo>().FromNewComponentOnRoot();
  1. FromResource - 通過(guò)調(diào)用Unity的Resources.Load創(chuàng)建ResultType類(lèi)型對(duì)象,可以用于加載Resources.Load可以加載的對(duì)象,比如textures, sounds, prefabs, 等等.
Container.Bind<Texture>().WithId("Glass").FromResource("Some/Path/Glass");
  1. FromResources - FromResource 的多值版本
  2. FromScriptableObjectResource - 直接綁定到給定資源路徑上的scriptable物體的實(shí)例上。注意:在Unity編輯器中對(duì)此值的更改將被永久保存。如果這不是你想要的,請(qǐng)使用FromNewScriptableObjectResource。
public class Foo : ScriptableObject
{
}

Container.Bind<Foo>().FromScriptableObjectResource("Some/Path/Foo");
  1. FromNewScriptableObjectResource - 和FromScriptableObjectResource類(lèi)似但會(huì)復(fù)制給定路徑的scriptable物體,如果您希望有給定的scriptable物體的多個(gè)不同實(shí)例,或者您希望確保在運(yùn)行時(shí)所做的更改不會(huì)影響scriptable對(duì)象的已保存的值,則此選項(xiàng)非常有用。
  2. FromResolve - 通過(guò)對(duì)容器執(zhí)行另一次查找來(lái)獲取實(shí)例(換句話(huà)說(shuō),調(diào)用DiContainer.Resolve<ResultType>())。請(qǐng)注意,要使用該方法,ResultType必須綁定在單獨(dú)的綁定語(yǔ)句中。當(dāng)您想要將接口綁定到另一個(gè)接口時(shí),此構(gòu)造方法特別有用,如下面的示例所示
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一樣但會(huì)匹配多個(gè)或0個(gè)值
  2. FromResolveGetter<ObjectType> - 從另一個(gè)依賴(lài)項(xiàng)的屬性中獲取實(shí)例,該依賴(lài)項(xiàng)是通過(guò)對(duì)容器執(zhí)行另一次查找獲得的(換句話(huà)說(shuō),調(diào)用DiContainer.Resolve<ObjectType>()然后訪(fǎng)問(wèn)返回的ResultType類(lèi)型實(shí)例上的值)。請(qǐng)注意,要使用該方法,ObjectType必須綁定在單獨(dú)的綁定語(yǔ)句中。
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> 一樣但會(huì)匹配多個(gè)或0個(gè)值
  2. FromSubContainerResolve-在子容器中查找以獲取ResultType實(shí)例,注意,要使用該方法,子容器中必須存在對(duì)ResultType的綁定。這種方法非常強(qiáng)大,因?yàn)樗试S您將相關(guān)的依賴(lài)項(xiàng)組合在一個(gè)迷你容器中,然后只暴露某些類(lèi)(也稱(chēng)為“Facades”)以在更高級(jí)別對(duì)這組依賴(lài)項(xiàng)進(jìn)行操作。有關(guān)使用子容器的更多詳細(xì)信息,請(qǐng)見(jiàn)后續(xù)。有幾種方法可以定義子容器:
  • ByNewPrefabMethod -通過(guò)實(shí)例化新的預(yù)設(shè)體來(lái)初始化子容器。注意,和ByNewContextPrefab不同,該綁定不要求在預(yù)設(shè)體上有物體上下文(GameObjectContext),在這種情況下,物體上下文會(huì)動(dòng)態(tài)的添加然后以給定的installer方法運(yùn)行
Container.Bind<Foo>().FromSubContainerResolve().ByNewPrefabMethod(MyPrefab, InstallFoo);

void InstallFoo(DiContainer subContainer)
{
       subContainer.Bind<Foo>();
}
  • ByNewPrefabInstaller- 通過(guò)實(shí)例化新的預(yù)設(shè)體實(shí)例子容器,和ByNewPrefabMethod類(lèi)似,但是它是通過(guò)給定的installer初始化動(dòng)態(tài)創(chuàng)建的物體上下文而不是通過(guò)一個(gè)方法
Container.Bind<Foo>().FromSubContainerResolve().ByNewPrefabInstaller<FooInstaller>(MyPrefab);

class FooInstaller : Installer
{
        public override void InstallBindings()
        {
            Container.Bind<Foo>();
        }
}
  • ByNewPrefabResourceMethod -通過(guò)實(shí)例化Resources.Load獲取的預(yù)制件來(lái)初始化子容器。請(qǐng)注意,與ByNewPrefabResource綁定方法不同,該綁定不要求在預(yù)設(shè)體上有物體上下文(GameObjectContext),在這種情況下,物體上下文會(huì)動(dòng)態(tài)的添加然后以給定的installer方法運(yùn)行
Container.Bind<Foo>().FromSubContainerResolve().ByNewPrefabResourceMethod("Path/To/MyPrefab", InstallFoo);

void InstallFoo(DiContainer subContainer)
{
    subContainer.Bind<Foo>();
}
  • ByNewPrefabResourceInstaller -通過(guò)實(shí)例化Resources.Load獲取的預(yù)制件來(lái)初始化子容器。和ByNewPrefabResourceMethod類(lèi)似,但是它是通過(guò)給定的installer初始化動(dòng)態(tài)創(chuàng)建的物體上下文而不是通過(guò)一個(gè)方法
Container.Bind<Foo>().FromSubContainerResolve().ByNewPrefabResourceInstaller<FooInstaller>("Path/To/MyPrefab");

class FooInstaller : MonoInstaller
{
  public override void InstallBindings()
   {
        Container.Bind<Foo>();
    }
}
  • ByNewGameObjectInstaller -通過(guò)實(shí)例化一個(gè)新的空游戲?qū)ο?,添加游戲?qū)ο笊舷挛模缓蟀惭b給定的installer來(lái)初始化子容器,該方法和ByInstaller非常類(lèi)似但還有一下優(yōu)點(diǎn):
    • 任何ITickable,IInitializable,IDisposable等綁定都將被正確調(diào)用
    • 在子容器內(nèi)實(shí)例化的任何新游戲?qū)ο蠖紝⒊蔀橛螒驅(qū)ο笊舷挛膶?duì)象下的父對(duì)象
    • 您可以通過(guò)銷(xiāo)毀游戲?qū)ο笊舷挛牡挠螒驅(qū)ο髞?lái)銷(xiāo)毀子容器
  • ByNewGameObjectMethod - 和ByNewGameObjectInstaller類(lèi)似但子容器通過(guò)給定的方法而不是installer類(lèi)型被初始化。
  • ByMethod - 通過(guò)一個(gè)方法初始化子容器
Container.Bind<Foo>().FromSubContainerResolve().ByMethod(InstallFooFacade);

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

注意,使用ByMethod時(shí),如果你想在子容器中使用zenject的接口比如ITickable, IInitializable, IDisposable ,你就必須同時(shí)使用WithKernel 綁定方法,如下:

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

void InstallFooFacade(DiContainer subContainer)
{
    subContainer.Bind<Foo>();
    subContainer.Bind<ITickable>().To<Bar>();
}
  • ByInstaller -通過(guò)使用Installer的子類(lèi)來(lái)初始化子容器。這是ByMethod的更清晰更不易出錯(cuò)的替代方案,尤其是你需要把數(shù)據(jù)注入到Installer自身中的時(shí)候。不易出錯(cuò)是因?yàn)樵谑褂肂yMethod時(shí),通常會(huì)在方法中意外使用Container而不是subContainer。
Container.Bind<Foo>().FromSubContainerResolve().ByInstaller<FooFacadeInstaller>();

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

注意,使用ByInstaller時(shí),如果你想在子容器中使用zenject的接口比如ITickable, IInitializable, IDisposable ,你就必須同時(shí)使用WithKernel 綁定方法,如下:

Container.Bind<Foo>().FromSubContainerResolve().ByInstaller<FooFacadeInstaller>().WithKernel();
  • ByNewContextPrefab -通過(guò)實(shí)例化新的預(yù)制體來(lái)初始化子容器。請(qǐng)注意,預(yù)制體根部必須包含GameObjectContext組件。有關(guān)GameObjectContext的詳細(xì)信息請(qǐng)見(jiàn)后續(xù)。
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>();
    }
}
  • ByNewContextPrefabResource -通過(guò)實(shí)例化Resources.Load獲取的預(yù)制體來(lái)初始化子容器。請(qǐng)注意,預(yù)制體根部必須包含GameObjectContext組件。
Container.Bind<Foo>().FromSubContainerResolve().ByNewContextPrefabResource("Path/To/MyPrefab");
  • ByInstance -直接使用您自己提供的給定DiContainer的實(shí)例來(lái)初始化子容器。很少使用。
  1. FromSubContainerResolveAll -和FromSubContainerResolve類(lèi)似但會(huì)匹配多個(gè)或0個(gè)值
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容