facebook twitter hatena line email

Unity/DIフレームワーク/Extenject/基本

提供: 初心者エンジニアの簡易メモ
2022年2月26日 (土) 00:59時点におけるAdmin (トーク | 投稿記録)による版 (エラーについて)

移動: 案内検索

Extenjectとは

UnityのDIフレームワーク。Zenjectは、作者が前務めてた会社で作られたDIフレームワークの名前。

ダウンロード

unityのassetstore https://assetstore.unity.com/packages/tools/utilities/extenject-dependency-injection-ioc-157735

公式 https://github.com/modesttree/Zenject/releases

サンプル

Installerからデータを入れて、[Inject]部分に入る。

ILogic.cs

public interface ILogic
{
    bool Exec();
}

HogeLogic.cs

public class HogeLogic : ILogic
{
    public bool Exec()
    {
        Debug.Log("HogeLogc Exec");
        return true;
    }
}

CarView.cs

using UnityEngine;
using Zenject;
public class CarView : MonoBehaviour
{
    [Inject] ILogic _logic;
    void Start()
    {
        if (_logic != null)
        {
            _logic.Exec();
        }
    }
}

HogeLogicInstaller.cs

using Zenject;
public class HogeLogicInstaller : MonoInstaller
{
    public override void InstallBindings()
    {
        Container
            .Bind<ILogic>()
            .To<HogeLogic>()
            .AsCached();
    }
}
  1. Scene選択
  2. Hierarchyに、Create EmptyでGameObjectを作り、CarViewの名前に変更し、CarView.csを、AddCompornentする。
  3. Hierarchyに、Create EmptyでGameObjectを作り、HogeLogicInstallerの名前に変更し、HogeLogicInstaller.csを、AddCompornentする。
  4. Hierarchyから右クリックし、Zenject/SceneContextを追加
  5. 作成した、SceneContextを選択し、InspectorのMonoInstallersに、HogeLogicInstallerのGameObjectをドラッグする。
  6. 実行するとCarのHogeLogicが実行できる

installer<TDerived>エラー

public class HogeLogicInstaller : Installer<HogeLogic>の部分で、 以下エラーが発生。

unity はジェネリック型またはメソッド installer<TDerived> 内で型パラメーター'TDerived として使用できません。

InstallBindingsメソッドを実行するクラスの継承元を : Installer<HogeLogic> などから MonoInstaller へ

参考:https://github.com/modesttree/Zenject

AsSingleとAsCachedとAsTransient

  • AsSingle:インスタンスは一つで、シングルトンと同じ。
  • AsCached:インスタンスを、キャッシュする。UnBindで破棄される。
  • AsTransient:インスタンスは毎回作る。

AsCachedについて

        Container
            .Bind<ILogic>()
            .To<HogeLogic>()
            .AsCached(); // キャッシュする
        Container
            .Unbind<HogeLogic>(); // キャッシュを削除
        Container
            .Bind<ILogic>()
            .To<HogeLogic>()
            .AsCached(); // キャッシュする
        Container
            .Resolve<HogeLogic>()
            .Exec(); // 実行

キャッシュを削除して、再度Bindして実行

インスタンスを入れる場合

        Container
            .Bind<ILogic>()
            .FromInstance(new HogeLogic())
            .AsSingle();

参考:https://light11.hatenadiary.com/entry/2019/02/20/234834

エラーについて

こんな感じのエラーのときは

while building object with type Object graph:

Installerに、Containerがたりないので、以下のような感じで、追加すれば良い。

Container
    .Bind<IHogeLogic>()
    .FromInstance(new HogeLogic())
    .AsCached()
    .IfNotBound();

参考:https://adarapata.hatenablog.com/entry/2019/01/02/000848

インストーラーからコンテナを削除したときのエラー

Container
    .Bind<IHogeLogic>()
    .FromInstance(new HogeLogic())
    .AsCached()
    .IfNotBound();

を削除したときは、以下がエラーとして出てくる。

ZenjectException: Unable to resolve 'IHogeLogic' while building object with type 'Main'. Object graph:
UserMain

参考

DIフレームワーク「Extenject(Zenject)」についてまとめました https://www.theylive.co.jp/unitydi%E3%83%95%E3%83%AC%E3%83%BC%E3%83%A0%E3%83%AF%E3%83%BC%E3%82%AFextenjectzenject%E3%81%AB%E3%81%A4%E3%81%84%E3%81%A6%E3%81%BE%E3%81%A8/

依存性注入とは?と、なっているのでZenject(Extenject)を入門 https://qiita.com/4_mio_11/items/4306732bc47780802b74