46 lines
1.6 KiB
C#
46 lines
1.6 KiB
C#
using UnityEngine;
|
|
using UnityEngine.Assertions;
|
|
|
|
namespace RebootKit.Engine.Foundation {
|
|
public interface IDependencyInstaller {
|
|
void Install(DIContext context);
|
|
}
|
|
|
|
public abstract class SceneDependencyInstaller : MonoBehaviour, IDependencyInstaller {
|
|
public abstract void Install(DIContext context);
|
|
}
|
|
|
|
[DefaultExecutionOrder(-1000)]
|
|
public class SceneContext : MonoBehaviour, IDependencyInstaller {
|
|
static readonly Logger s_logger = new(nameof(SceneContext));
|
|
|
|
[SerializeField] SceneDependencyInstaller[] m_Installers;
|
|
|
|
void Awake() {
|
|
DIContext context = RR.DIContext;
|
|
|
|
foreach (GameObject root in gameObject.scene.GetRootGameObjects()) {
|
|
s_logger.Info("Injecting root game object: " + root.name);
|
|
context.InjectGameObject(root);
|
|
}
|
|
}
|
|
|
|
public void Install(DIContext context) {
|
|
s_logger.Info("Installing scene dependency installers");
|
|
foreach (SceneDependencyInstaller installer in m_Installers) {
|
|
installer.Install(context);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static class DIContextGameObjectEx {
|
|
public static void InjectGameObject(this DIContext context, GameObject gameObject, bool injectChildren = true) {
|
|
Assert.IsNotNull(gameObject);
|
|
|
|
Component[] components = injectChildren ? gameObject.GetComponentsInChildren<Component>() : gameObject.GetComponents<Component>();
|
|
foreach (Component component in components) {
|
|
context.Inject(component);
|
|
}
|
|
}
|
|
}
|
|
} |