public void Awake() { Log.Debug("Zenject Started"); _container = CreateContainer(false, GlobalCompositionRoot.Instance.Container); InjectionHelper.InjectChildGameObjects(_container, gameObject); _dependencyRoot = _container.Resolve <IDependencyRoot>(); }
void Resolve() { InjectionHelper.InjectChildGameObjects(_container, gameObject); if (_container.HasBinding <IDependencyRoot>()) { _dependencyRoot = _container.Resolve <IDependencyRoot>(); _dependencyRoot.Start(); } else { Debug.LogWarning("No dependency root found"); } }
// Create a new game object from a given prefab // Without returning any particular monobehaviour public GameObject Instantiate(GameObject template, params object[] args) { var gameObj = (GameObject)GameObject.Instantiate(template); // By default parent to comp root // This is good so that the entire object graph is // contained underneath it, which is useful for cases // where you need to delete the entire object graph gameObj.transform.parent = _rootTransform; gameObj.SetActive(true); InjectionHelper.InjectChildGameObjects(_container, gameObj, args); return(gameObj); }
// This method can be used to load the given scene and perform injection on its contents // Note that the scene we're loading can have [Inject] flags however it should not have // its own composition root public static IEnumerator LoadSceneAdditiveWithContainer( string levelName, DiContainer parentContainer) { var rootObjectsBeforeLoad = GameObject.FindObjectsOfType <Transform>().Where(x => x.parent == null).ToList(); Application.LoadLevelAdditive(levelName); // Wait one frame for objects to be added to the scene heirarchy yield return(null); var rootObjectsAfterLoad = GameObject.FindObjectsOfType <Transform>().Where(x => x.parent == null).ToList(); foreach (var newObject in rootObjectsAfterLoad.Except(rootObjectsBeforeLoad).Select(x => x.gameObject)) { Assert.That(newObject.GetComponent <CompositionRoot>() == null, "LoadSceneAdditiveWithContainer does not expect a container to exist in the loaded scene"); InjectionHelper.InjectChildGameObjects(parentContainer, newObject); } }