Files
dungeons/Assets/Scripts/Utils/Injector.cs

52 lines
1.4 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static Zenject.ZenAutoInjecter;
using Zenject;
using ModestTree;
public static class Injector
{
static DiContainer LookupContainer(GameObject go, ContainerSources container)
{
if (container == ContainerSources.ProjectContext)
{
return ProjectContext.Instance.Container;
}
if (container == ContainerSources.SceneContext)
{
return GetContainerForCurrentScene(go);
}
Assert.IsEqual(container, ContainerSources.SearchHierarchy);
var parentContext = go.transform.GetComponentInParent<Context>();
if (parentContext != null)
{
return parentContext.Container;
}
return GetContainerForCurrentScene(go);
}
static DiContainer GetContainerForCurrentScene(GameObject go)
{
return ProjectContext.Instance.Container
.Resolve<SceneContextRegistry>()
.GetContainerForScene(go.scene);
}
public static void Inject(this GameObject go, ContainerSources container = ContainerSources.SceneContext)
{
LookupContainer(go, container).InjectGameObject(go);
}
public static void Inject(this MonoBehaviour mb, ContainerSources container = ContainerSources.SceneContext)
{
LookupContainer(mb.gameObject, container).Inject(mb);
}
}