Example #1
0
        public void Awake()
        {
            Log.Debug("Zenject Started");

            _container = CreateContainer(
                false, GlobalCompositionRoot.Instance.Container, _staticInstallers);
            _staticInstallers.Clear();

            InjectionHelper.InjectChildGameObjects(_container, gameObject, !OnlyInjectWhenActive);
            _dependencyRoot = _container.Resolve <IDependencyRoot>();
        }
Example #2
0
        // 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.SetParent(_rootTransform, false);

            gameObj.SetActive(true);

            InjectionHelper.InjectChildGameObjects(_container, gameObj, false, args);

            return(gameObj);
        }
Example #3
0
        // 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);
            }
        }