Пример #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);

            InjectionHelper.InjectChildGameObjects(_container, gameObject);
            _dependencyRoot = _container.Resolve <IDependencyRoot>();
        }
Пример #3
0
        void Resolve()
        {
            InjectionHelper.InjectChildGameObjects(_container, gameObject);

            if (_container.HasBinding <IDependencyRoot>())
            {
                _dependencyRoot = _container.Resolve <IDependencyRoot>();
                _dependencyRoot.Start();
            }
            else
            {
                Debug.LogWarning("No dependency root found");
            }
        }
Пример #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.parent = _rootTransform;

            gameObj.SetActive(true);

            InjectionHelper.InjectChildGameObjects(_container, gameObj, 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);
            }
        }
Пример #8
0
        // Create from prefab
        // Return specific monobehaviour
        public T Instantiate <T>(
            GameObject template, params object[] args) where T : Component
        {
            Assert.That(template != null, "Null template found when instantiating game object");

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

            T requestedScript = null;

            foreach (var component in gameObj.GetComponentsInChildren <Component>())
            {
                var extraArgs = Enumerable.Empty <object>();

                if (component.GetType() == typeof(T))
                {
                    Assert.IsNull(requestedScript,
                                  "Found multiple matches with type '{0}' when instantiating new game object", typeof(T));
                    requestedScript = (T)component;
                    extraArgs       = args;
                }

                InjectionHelper.InjectMonoBehaviour(_container, component, extraArgs);
            }

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

            return(requestedScript);
        }