|
|
| (同じ利用者による、間の12版が非表示) |
| 行1: |
行1: |
| − | ==Extenjectとは==
| + | [[Unity/DIフレームワーク/Extenject/基本]] |
| − | UnityのDIフレームワーク。Zenjectは、作者が前務めてた会社で作られたDIフレームワークの名前。
| + | |
| | | | |
| − | ==ダウンロード==
| + | [[Unity/DIフレームワーク/Extenject/SceneLoader]] |
| − | unityのassetstore
| + | |
| − | https://assetstore.unity.com/packages/tools/utilities/extenject-dependency-injection-ioc-157735
| + | |
| | | | |
| − | 公式
| + | [[Unity/DIフレームワーク/Extenject/Scene間共有]] |
| − | https://github.com/modesttree/Zenject/releases
| + | |
| | | | |
| − | ==サンプル==
| + | [[Unity/DIフレームワーク/Extenject/非MonoBehavior]] |
| − | ILogic.cs
| + | |
| − | <pre>
| + | |
| − | public interface ILogic
| + | |
| − | {
| + | |
| − | bool Exec();
| + | |
| − | }
| + | |
| − | </pre>
| + | |
| − | HogeLogic.cs
| + | |
| − | <pre>
| + | |
| − | public class HogeLogic : ILogic
| + | |
| − | {
| + | |
| − | public bool Exec()
| + | |
| − | {
| + | |
| − | Debug.Log("HogeLogc Exec");
| + | |
| − | return true;
| + | |
| − | }
| + | |
| − | }
| + | |
| − | </pre>
| + | |
| − | CarView.cs
| + | |
| − | <pre>
| + | |
| − | using UnityEngine;
| + | |
| − | using Zenject;
| + | |
| − | public class CarView : MonoBehaviour
| + | |
| − | {
| + | |
| − | [Inject] ILogic _logic;
| + | |
| − | void Start()
| + | |
| − | {
| + | |
| − | if (_logic != null)
| + | |
| − | {
| + | |
| − | _logic.Exec();
| + | |
| − | }
| + | |
| − | }
| + | |
| − | }
| + | |
| − | </pre>
| + | |
| | | | |
| − | HogeLogicInstaller.cs
| + | [[Unity/DIフレームワーク/Extenject/ProjectContext]] |
| − | <pre>
| + | |
| − | using Zenject;
| + | |
| − | public class HogeLogicInstaller : MonoInstaller
| + | |
| − | {
| + | |
| − | public override void InstallBindings()
| + | |
| − | {
| + | |
| − | Container
| + | |
| − | .Bind<ILogic>()
| + | |
| − | .To<HogeLogic>()
| + | |
| − | .AsCached();
| + | |
| − | }
| + | |
| − | }
| + | |
| − | </pre>
| + | |
| − | | + | |
| − | #Scene選択
| + | |
| − | #Hierarchyに、Create EmptyでGameObjectを作り、CarViewの名前に変更し、CarView.csを、AddCompornentする。
| + | |
| − | #Hierarchyに、Create EmptyでGameObjectを作り、HogeLogicInstallerの名前に変更し、HogeLogicInstaller.csを、AddCompornentする。
| + | |
| − | #Hierarchyから右クリックし、Zenject/SceneContextを追加
| + | |
| − | #作成した、SceneContextを選択し、InspectorのMonoINstallersに、HogeLogicInstallerのGameObjectをドラッグする。
| + | |
| − | #実行すると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について===
| + | |
| − | <pre>
| + | |
| − | Container
| + | |
| − | .Bind<ILogic>()
| + | |
| − | .To<HogeLogic>()
| + | |
| − | .AsCached(); // キャッシュする
| + | |
| − | Container
| + | |
| − | .Unbind<HogeLogic>(); // キャッシュを削除
| + | |
| − | Container
| + | |
| − | .Bind<ILogic>()
| + | |
| − | .To<HogeLogic>()
| + | |
| − | .AsCached(); // キャッシュする
| + | |
| − | Container
| + | |
| − | .Resolve<HogeLogic>()
| + | |
| − | .Exec(); // 実行
| + | |
| − | </pre>
| + | |
| − | キャッシュを削除して、再度Bindして実行
| + | |
| − | | + | |
| − | ==参考==
| + | |
| − | 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
| + | |
| − | | + | |
| − | zenjectでクリーンアーキテクチャ
| + | |
| − | https://qiita.com/sai_maple_/items/0a0be9156d2a28f20cb7
| + | |