Esempio n. 1
0
        public IEnumerator <List <object> > GetAllInstancesWithInjectSplit(InjectContext context, List <TypeValuePair> args)
        {
            Assert.IsNotNull(context);

            bool autoInject = false;

            var instanceType = GetTypeToCreate(context.MemberType);

            var injectArgs = new InjectArgs()
            {
                ExtraArgs          = _extraArguments.Concat(args).ToList(),
                Context            = context,
                ConcreteIdentifier = _concreteIdentifier,
            };

            var instance = _container.InstantiateExplicit(
                instanceType, autoInject, injectArgs);

            // Return before property/field/method injection to allow circular dependencies
            yield return(new List <object>()
            {
                instance
            });

            _container.InjectExplicit(instance, instanceType, injectArgs);
        }
Esempio n. 2
0
        public IEnumerator <List <object> > GetAllInstancesWithInjectSplit(InjectContext context, List <TypeValuePair> args)
        {
            Assert.IsNotNull(context);

            Assert.That(context.ObjectType.DerivesFrom <Component>(),
                        "Object '{0}' can only be injected into MonoBehaviour's since it was bound with 'FromNewComponentSibling'. Attempted to inject into non-MonoBehaviour '{1}'",
                        context.MemberType, context.ObjectType);

            object instance;

            if (!_container.IsValidating || DiContainer.CanCreateOrInjectDuringValidation(_componentType))
            {
                var gameObj = ((Component)context.ObjectInstance).gameObject;

                instance = gameObj.GetComponent(_componentType);

                if (instance != null)
                {
                    yield return(new List <object>()
                    {
                        instance
                    });

                    yield break;
                }

                instance = gameObj.AddComponent(_componentType);
            }
            else
            {
                instance = new ValidationMarker(_componentType);
            }

            // Note that we don't just use InstantiateComponentOnNewGameObjectExplicit here
            // because then circular references don't work
            yield return(new List <object>()
            {
                instance
            });

            var injectArgs = new InjectArgs()
            {
                ExtraArgs          = _extraArguments.Concat(args).ToList(),
                Context            = context,
                ConcreteIdentifier = _concreteIdentifier,
            };

            _container.InjectExplicit(instance, _componentType, injectArgs);

            Assert.That(injectArgs.ExtraArgs.IsEmpty());
        }
Esempio n. 3
0
        public IEnumerator <GameObject> Instantiate(List <TypeValuePair> args)
        {
            var  context = new InjectContext(_container, _argumentTarget, null);
            bool shouldMakeActive;
            var  gameObject = _container.CreateAndParentPrefab(
                GetPrefab(), _gameObjectBindInfo, context, out shouldMakeActive);

            Assert.IsNotNull(gameObject);

            // Return it before inject so we can do circular dependencies
            yield return(gameObject);

            var allArgs = _extraArguments.Concat(args).ToList();

            if (_argumentTarget == null)
            {
                Assert.That(allArgs.IsEmpty(),
                            "Unexpected arguments provided to prefab instantiator.  Arguments are not allowed if binding multiple components in the same binding");
            }

            if (_argumentTarget == null || allArgs.IsEmpty())
            {
                _container.InjectGameObject(gameObject);
            }
            else
            {
                var injectArgs = new InjectArgs()
                {
                    ExtraArgs          = allArgs,
                    Context            = context,
                    ConcreteIdentifier = null,
                };

                _container.InjectGameObjectForComponentExplicit(
                    gameObject, _argumentTarget, injectArgs);
            }

            if (shouldMakeActive)
            {
                gameObject.SetActive(true);
            }
        }
        public IEnumerator <List <object> > GetAllInstancesWithInjectSplit(
            InjectContext context, List <TypeValuePair> args)
        {
            Assert.IsNotNull(context);

            List <object> objects;

            if (_createNew)
            {
                objects = Resources.LoadAll(_resourcePath, _resourceType)
                          .Select(x => ScriptableObject.Instantiate(x)).Cast <object>().ToList();
            }
            else
            {
                objects = Resources.LoadAll(_resourcePath, _resourceType)
                          .Cast <object>().ToList();
            }

            Assert.That(!objects.IsEmpty(),
                        "Could not find resource at path '{0}' with type '{1}'", _resourcePath, _resourceType);

            yield return(objects);

            var injectArgs = new InjectArgs()
            {
                ExtraArgs          = _extraArguments.Concat(args).ToList(),
                Context            = context,
                ConcreteIdentifier = _concreteIdentifier,
            };

            foreach (var obj in objects)
            {
                _container.InjectExplicit(
                    obj, _resourceType, injectArgs);
            }
        }
        public IEnumerator <List <object> > GetAllInstancesWithInjectSplit(InjectContext context, List <TypeValuePair> args)
        {
            Assert.IsNotNull(context);

            object instance;

            // We still want to make sure we can get the game object during validation
            var gameObj = GetGameObject(context);

            var wasActive = gameObj.activeSelf;

            if (wasActive && ShouldToggleActive)
            {
                // We need to do this in some cases to ensure that [Inject] always gets
                // called before awake / start
                gameObj.SetActive(false);
            }

            if (!_container.IsValidating || DiContainer.CanCreateOrInjectDuringValidation(_componentType))
            {
                if (_componentType == typeof(Transform))
                // Treat transform as a special case because it's the one component that's always automatically added
                // Otherwise, calling AddComponent below will fail and return null
                // This is nice to allow doing things like
                //      Container.Bind<Transform>().FromNewComponentOnNewGameObject();
                {
                    instance = gameObj.transform;
                }
                else
                {
                    instance = gameObj.AddComponent(_componentType);
                }

                Assert.IsNotNull(instance);
            }
            else
            {
                instance = new ValidationMarker(_componentType);
            }

            // Note that we don't just use InstantiateComponentOnNewGameObjectExplicit here
            // because then circular references don't work
            yield return(new List <object>()
            {
                instance
            });

            try
            {
                var injectArgs = new InjectArgs()
                {
                    ExtraArgs          = _extraArguments.Concat(args).ToList(),
                    Context            = context,
                    ConcreteIdentifier = _concreteIdentifier,
                };

                _container.InjectExplicit(instance, _componentType, injectArgs);

                Assert.That(injectArgs.ExtraArgs.IsEmpty());
            }
            finally
            {
                if (wasActive && ShouldToggleActive)
                {
                    gameObj.SetActive(true);
                }
            }
        }