예제 #1
0
        // Add a monobehaviour to an existing game object, using Type rather than a generic
        // Note: gameobject here is not a prefab prototype, it is an instance
        public Component AddMonobehaviour(
            Type behaviourType, GameObject gameObject, params object[] args)
        {
            Assert.That(behaviourType.DerivesFrom <Component>());
            var monoBehaviour = (Component)gameObject.AddComponent(behaviourType);

            InjectionHelper.InjectMonoBehaviour(_container, monoBehaviour, args);
            return(monoBehaviour);
        }
예제 #2
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>();
        }
예제 #3
0
        // Create from prefab
        // Return specific monobehaviour
        public object Instantiate(
            Type componentType, GameObject template, params object[] args)
        {
            Assert.That(template != null, "Null template found when instantiating game object");

            // It could be an interface so this may fail in valid cases:
            Assert.That(componentType.DerivesFrom <Component>());

            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);

            Component requestedScript = null;

            // Inject on the children first since the parent objects are more likely to use them in their post inject methods
            foreach (var component in UnityUtil.GetComponentsInChildrenDepthFirst <Component>(gameObj, false))
            {
                if (component != null)
                {
                    var extraArgs = Enumerable.Empty <object>();

                    if (component.GetType().DerivesFromOrEqual(componentType))
                    {
                        Assert.IsNull(requestedScript,
                                      "Found multiple matches with type '{0}' when instantiating new game object from template '{1}'", componentType, template.name);
                        requestedScript = component;
                        extraArgs       = args;
                    }

                    InjectionHelper.InjectMonoBehaviour(_container, component, extraArgs);
                }
                else
                {
                    Log.Warn("Found null component while instantiating prefab '{0}'.  Possible missing script.", template.name);
                }
            }

            if (requestedScript == null)
            {
                throw new ZenjectResolveException(
                          "Could not find component with type '{0}' when instantiating new game object".Fmt(componentType));
            }

            return(requestedScript);
        }
예제 #4
0
        public object Instantiate(Type type, string name)
        {
            var gameObj = new GameObject(name);

            gameObj.transform.parent = _rootTransform;

            var component = gameObj.AddComponent(type);

            if (type.DerivesFrom(typeof(Component)))
            {
                InjectionHelper.InjectMonoBehaviour(_container, (Component)component);
            }

            return(component);
        }
예제 #5
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);
        }
예제 #6
0
        public override object GetInstance(Type contractType, InjectContext context)
        {
            Assert.That(_componentType.DerivesFromOrEqual(contractType));

            if (_instance == null)
            {
                Assert.That(!_container.AllowNullBindings,
                            "Tried to instantiate a MonoBehaviour with type '{0}' during validation. Object graph: {1}", _componentType, DiContainer.GetCurrentObjectGraph());

                _instance = _gameObject.AddComponent(_componentType);
                Assert.That(_instance != null);

                InjectionHelper.InjectMonoBehaviour(_container, _instance);
            }

            return(_instance);
        }
예제 #7
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);
            }
        }