Unity/DIフレームワーク/Extenject/ProjectContext
提供: 初心者エンジニアの簡易メモ
2023年11月15日 (水) 05:23時点におけるAdmin (トーク | 投稿記録)による版 (ページの作成:「==ProjectContextとは== 全共通して使えるSceneContext 参考:https://www.subarunari.com/entry/2018/01/08/024341 ==準備== #Edit/Zenject/CreateProjectContext...」)
ProjectContextとは
全共通して使えるSceneContext
参考:https://www.subarunari.com/entry/2018/01/08/024341
準備
- Edit/Zenject/CreateProjectContextで、Project内の、Resources直下に、ProjectContextを作られる
- 適当なInstaller(以下例ではProjectContextInstaller)を作り、Prefab化したGameObjectにAddComponentしておく。
- ProjectContextのPrefabInstallersに、上記Installerを追加する。
- ヒエラルキー上に、SceneContextを作っておく。
サンプル
ProjectContextInstaller.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Zenject;
using System;
public class ProjectContextInstaller : MonoInstaller
{
public override void InstallBindings()
{
Container
.Bind<ProjectContextLogic>()
.To<ProjectContextLogic>()
.AsTransient();
}
}
ProjectContextLogic.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ProjectContextLogic : ILogic
{
public bool Exec()
{
Debug.Log("ProjectContextLogic Exec");
return true;
}
}
ProjectContextMain.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Zenject;
public class ProjectContextMain : MonoBehaviour
{
[Inject] ProjectContextLogic _logic;
void Start()
{
_logic.Exec();
}
}
ProjectContextMainを追加したシーンでは、"ProjectContextLogic Exec"とログが表示され、追加してないシーンでは、ProjectContextが作られるが、ログは出ない。
