facebook twitter hatena line email

Unity/DIフレームワーク/Extenject/ProjectContext

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

ProjectContextとは

全共通して使えるSceneContext

参考:https://www.subarunari.com/entry/2018/01/08/024341

準備

  1. Edit/Zenject/CreateProjectContextで、Project内の、Resources直下に、ProjectContextを作られる
  2. 適当なInstaller(以下例ではProjectContextInstaller)を作り、Prefab化したGameObjectにAddComponentしておく。
  3. ProjectContextのPrefabInstallersに、上記Installerを追加する。
  4. ヒエラルキー上に、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>()
            .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が作られるが、ログは出ない。