コード例 #1
0
ファイル: TransientProvider.cs プロジェクト: Aszan/Zenject
 public override object GetInstance(InjectContext context)
 {
     var obj = context.Container.InstantiateExplicit(
         GetTypeToInstantiate(context.MemberType), new List<TypeValuePair>(), context, null, true);
     Assert.That(obj != null);
     return obj;
 }
コード例 #2
0
ファイル: InjectableInfo.cs プロジェクト: RainsSoft/Zenject
 public InjectContext CreateInjectContext(
     DiContainer container, InjectContext currentContext, object targetInstance, string concreteIdentifier)
 {
     return new InjectContext(
         container, MemberType, Identifier, Optional,
         ObjectType, targetInstance, MemberName, currentContext, concreteIdentifier);
 }
コード例 #3
0
        public IEnumerable<ZenjectResolveException> ValidateBinding(
            Type componentType, InjectContext context)
        {
            if (!ContainsComponent(componentType))
            {
                yield return new ZenjectResolveException(
                    "Could not find component of type '{0}' in prefab with name '{1}' \nObject graph:\n{2}"
                    .Fmt(componentType.Name(), _id.Prefab.name, context.GetObjectGraphString()));
                yield break;
            }

            // In most cases componentType will be a MonoBehaviour but we also want to allow interfaces
            // And in that case we can't validate it
            if (!componentType.IsAbstract())
            {
                // Note that we always want to cache _container instead of using context.Container
                // since for singletons, the container they are accessed from should not determine
                // the container they are instantiated with
                // Transients can do that but not singletons
                foreach (var err in _container.ValidateObjectGraph(componentType, context))
                {
                    yield return err;
                }
            }
        }
コード例 #4
0
ファイル: TransientProvider.cs プロジェクト: Soren025/Zenject
        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,
                UseAllArgs = false,
            };

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

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

            injectArgs.UseAllArgs = true;

            _container.InjectExplicit(instance, instanceType, injectArgs);
        }
コード例 #5
0
ファイル: CachedProvider.cs プロジェクト: Soren025/Zenject
        public IEnumerator<List<object>> GetAllInstancesWithInjectSplit(InjectContext context, List<TypeValuePair> args)
        {
            Assert.IsNotNull(context);

            if (_instances != null)
            {
                yield return _instances;
                yield break;
            }

            // This should only happen with constructor injection
            // Field or property injection should allow circular dependencies
            Assert.That(!_isCreatingInstance,
            "Found circular dependency when creating type '{0}'",
            _creator.GetInstanceType(context));

            _isCreatingInstance = true;

            var runner = _creator.GetAllInstancesWithInjectSplit(context, args);

            // First get instance
            bool hasMore = runner.MoveNext();

            _instances = runner.Current;
            Assert.IsNotNull(_instances);
            _isCreatingInstance = false;

            yield return _instances;

            // Now do injection
            while (hasMore)
            {
                hasMore = runner.MoveNext();
            }
        }
コード例 #6
0
        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);

            if (!_container.IsValidating || DiContainer.CanCreateOrInjectDuringValidation(_componentType))
            {
                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(),
                UseAllArgs = true,
                Context = context,
                ConcreteIdentifier = _concreteIdentifier,
            };

            _container.InjectExplicit(instance, _componentType, injectArgs);

            Assert.That(injectArgs.ExtraArgs.IsEmpty());
        }
コード例 #7
0
        public override object GetInstance(InjectContext context)
        {
            Assert.That(_componentType.DerivesFromOrEqual(context.MemberType));

            return context.Container.InstantiateComponentOnNewGameObjectExplicit(
                _componentType, _componentType.Name(), new List<TypeValuePair>(), context);
        }
コード例 #8
0
        public IEnumerator<List<object>> GetAllInstancesWithInjectSplit(InjectContext context, List<TypeValuePair> args)
        {
            Assert.IsEmpty(args);
            Assert.IsNotNull(context);

            if (_container.IsValidating)
            {
                yield return new List<object>() { new ValidationMarker(context.MemberType) };
            }
            else
            {
                var result = _method(context);

                if (result == null)
                {
                    Assert.That(context.MemberType.IsPrimitive,
                        "Invalid value returned from FromMethod.  Expected non-null.");
                }
                else
                {
                    Assert.That(result.GetType().DerivesFromOrEqual(context.MemberType));
                }

                yield return new List<object>() { result };
            }
        }
コード例 #9
0
        public IEnumerator<List<object>> GetAllInstancesWithInjectSplit(
            InjectContext context, List<TypeValuePair> args)
        {
            Assert.IsNotNull(context);

            var gameObjectRunner = _prefabInstantiator.Instantiate(args);

            // First get instance
            bool hasMore = gameObjectRunner.MoveNext();

            var gameObject = gameObjectRunner.Current;

            var allComponents = gameObject.GetComponentsInChildren(_componentType);

            Assert.That(allComponents.Length >= 1,
                "Expected to find at least one component with type '{0}' on prefab '{1}'",
                _componentType, _prefabInstantiator.GetPrefab().name);

            yield return allComponents.Cast<object>().ToList();

            // Now do injection
            while (hasMore)
            {
                hasMore = gameObjectRunner.MoveNext();
            }
        }
コード例 #10
0
        public override object GetInstance(InjectContext context)
        {
            Assert.That(_componentType.DerivesFromOrEqual(context.MemberType));

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

                // Note that we always want to cache _container instead of using context.Container
                // since for singletons, the container they are accessed from should not determine
                // the container they are instantiated with
                // Transients can do that but not singletons

                var name = string.IsNullOrEmpty(_name) ? _componentType.Name() : _name;

                // We don't use the generic version here to avoid duplicate generic arguments to binder
                _instance = _container.InstantiateComponentOnNewGameObjectExplicit(
                    _componentType, name, new List<TypeValuePair>(), context);
                Assert.IsNotNull(_instance);
            }

            return _instance;
        }
コード例 #11
0
        public object GetComponent(Type componentType, InjectContext context)
        {
            if (_rootObj == null)
            {
                Assert.IsNotNull(_id.ResourcePath);

                var prefab = (GameObject)Resources.Load(_id.ResourcePath);
                Assert.IsNotNull(prefab, "Could not find prefab at resource path '{0}'", _id.ResourcePath);

                _rootObj = (GameObject)GameObject.Instantiate(prefab);

                // Note that we always want to cache _container instead of using context.Container
                // since for singletons, the container they are accessed from should not determine
                // the container they are instantiated with
                // Transients can do that but not singletons

                _rootObj.transform.SetParent(_container.DefaultParent, false);

                _rootObj.SetActive(true);

                _container.InjectGameObject(_rootObj, true, false, new object[0], context);
            }

            var component = _rootObj.GetComponentInChildren(componentType);

            if (component == null)
            {
                throw new ZenjectResolveException(
                    "Could not find component with type '{0}' in given singleton prefab".Fmt(componentType));
            }

            return component;
        }
コード例 #12
0
ファイル: ResolveProvider.cs プロジェクト: Soren025/Zenject
        InjectContext GetSubContext(InjectContext parent)
        {
            var subContext = parent.CreateSubContext(_contractType, _identifier);

            subContext.Optional = _isOptional;

            return subContext;
        }
コード例 #13
0
ファイル: InstanceProvider.cs プロジェクト: Soren025/Zenject
        public IEnumerator<List<object>> GetAllInstancesWithInjectSplit(InjectContext context, List<TypeValuePair> args)
        {
            Assert.IsEmpty(args);
            Assert.IsNotNull(context);

            Assert.That(_instanceType.DerivesFromOrEqual(context.MemberType));

            yield return new List<object>() { _instance };
        }
コード例 #14
0
ファイル: ResolveProvider.cs プロジェクト: Soren025/Zenject
        public IEnumerator<List<object>> GetAllInstancesWithInjectSplit(InjectContext context, List<TypeValuePair> args)
        {
            Assert.IsEmpty(args);
            Assert.IsNotNull(context);

            Assert.That(_contractType.DerivesFromOrEqual(context.MemberType));

            yield return _container.ResolveAll(GetSubContext(context)).Cast<object>().ToList();
        }
コード例 #15
0
ファイル: ResourceProvider.cs プロジェクト: Aszan/Zenject
        public override object GetInstance(InjectContext context)
        {
            var obj = Resources.Load(_resourcePath, _concreteType);

            Assert.IsNotNull(obj,
                "Could not find resource at path '{0}' with type '{1}'", _resourcePath, _concreteType);

            return obj;
        }
コード例 #16
0
        public override object GetInstance(InjectContext context)
        {
            Assert.That(_componentType.DerivesFromOrEqual(context.MemberType));

            var name = string.IsNullOrEmpty(_name) ? _componentType.Name() : _name;

            return context.Container.InstantiateComponentOnNewGameObjectExplicit(
                _componentType, name, new List<TypeValuePair>(), context);
        }
コード例 #17
0
        public object GetInstance(InjectContext context)
        {
            if (_instance == null)
            {
                InitInstance(context);
                Assert.IsNotNull(_instance);
            }

            return _instance;
        }
コード例 #18
0
        public IEnumerator<List<object>> GetAllInstancesWithInjectSplit(InjectContext context, List<TypeValuePair> args)
        {
            Assert.IsNotNull(context);

            var subContainer = _subContainerCreator.CreateSubContainer(args);

            var subContext = CreateSubContext(context, subContainer);

            yield return subContainer.ResolveAll(subContext).Cast<object>().ToList();
        }
コード例 #19
0
        public IEnumerator<List<object>> GetAllInstancesWithInjectSplit(
            InjectContext context, List<TypeValuePair> args)
        {
            Assert.IsEmpty(args);

            yield return new List<object>()
            {
                _container.CreateEmptyGameObject(_gameObjectName, _groupName)
            };
        }
コード例 #20
0
        public override object GetInstance(InjectContext context)
        {
            Assert.That(_returnType.DerivesFromOrEqual(context.MemberType));

            var obj = _method(context);

            Assert.That(obj != null, () =>
                "Method provider returned null when looking up type '{0}'. \nObject graph:\n{1}".Fmt(_returnType.Name(), context.GetObjectGraphString()));

            return obj;
        }
コード例 #21
0
 public IEnumerable<ZenjectResolveException> ValidateBinding(InjectContext context)
 {
     // Note that we always want to cache _container instead of using context.Container
     // since for singletons, the container they are accessed from should not determine
     // the container they are instantiated with
     // Transients can do that but not singletons
     foreach (var err in _container.ValidateObjectGraph(_id.ConcreteType, context))
     {
         yield return err;
     }
 }
コード例 #22
0
        public override IEnumerable<ZenjectResolveException> ValidateBinding(InjectContext context)
        {
            // Can't validate custom methods so assume they work
            if (_creator.HasInstance() || _creator.HasCustomCreateMethod)
            {
                // This would be the case if given an instance at binding time with ToSingle(instance)
                return Enumerable.Empty<ZenjectResolveException>();
            }

            return _container.ValidateObjectGraph(GetInstanceType(), context, _creator.Id.Identifier);
        }
コード例 #23
0
        public override object GetInstance(InjectContext context)
        {
            object instance;

            if (!_instances.TryGetValue(context.MemberType, out instance))
            {
                instance = typeof(Mock).GetMethod("Of", new Type[] { context.MemberType }).Invoke(null, null);
                _instances.Add(context.MemberType, instance);
            }

            return instance;
        }
コード例 #24
0
        InjectContext CreateSubContext(
            InjectContext parent, DiContainer subContainer)
        {
            var subContext = parent.CreateSubContext(_dependencyType, _identifier);

            subContext.Container = subContainer;

            // This is important to avoid infinite loops
            subContext.SourceType = InjectSources.Local;

            return subContext;
        }
コード例 #25
0
        public static object GetInstance(
            this IProvider creator, InjectContext context, List<TypeValuePair> args)
        {
            var allInstances = creator.GetAllInstances(context, args);

            Assert.That(!allInstances.IsEmpty(),
                "Provider returned zero instances when one was expected");

            Assert.That(allInstances.Count == 1,
                "Provider returned multiple instances when only one was expected");

            return allInstances[0];
        }
コード例 #26
0
ファイル: InjectContext.cs プロジェクト: IllusiveS/Asteroids
 public InjectContext(
     DiContainer container, Type memberType, string identifier, bool optional,
     Type objectType, object objectInstance, string memberName, InjectContext parentContext)
 {
     ObjectType = objectType;
     ObjectInstance = objectInstance;
     Identifier = identifier;
     MemberName = memberName;
     MemberType = memberType;
     Optional = optional;
     Container = container;
     BindingId = new BindingId(memberType, identifier);
     ParentContext = parentContext;
 }
コード例 #27
0
ファイル: ResourceProvider.cs プロジェクト: Soren025/Zenject
        public IEnumerator<List<object>> GetAllInstancesWithInjectSplit(InjectContext context, List<TypeValuePair> args)
        {
            Assert.IsEmpty(args);

            Assert.IsNotNull(context);

            var obj = Resources.Load(_resourcePath, _resourceType);

            Assert.IsNotNull(obj,
                "Could not find resource at path '{0}' with type '{1}'", _resourcePath, _resourceType);

            yield return new List<object>() { obj };

            // Are there any resource types which can be injected?
        }
コード例 #28
0
        public override IEnumerable<ZenjectResolveException> ValidateBinding(InjectContext context)
        {
            if (!_creator.ContainsComponent(_concreteType))
            {
                yield return new ZenjectResolveException(
                    "Could not find component of type '{0}' in prefab with name '{1}' \nObject graph:\n{2}"
                    .Fmt(_concreteType.Name(), _creator.Prefab.name, context.GetObjectGraphString()));
                yield break;
            }

            foreach (var err in BindingValidator.ValidateObjectGraph(_container, _concreteType, context, null))
            {
                yield return err;
            }
        }
コード例 #29
0
        public override object GetInstance(InjectContext context)
        {
            Assert.That(_componentType.DerivesFromOrEqual(context.MemberType));

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

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

                _container.Inject(_instance);
            }

            return _instance;
        }
コード例 #30
0
        void InitInstance(InjectContext context)
        {
            var concreteType = GetTypeToInstantiate(context.MemberType);

            bool autoInject = false;

            _instance = _container.InstantiateExplicit(
                concreteType, new List<TypeValuePair>(), context, _id.ConcreteIdentifier, autoInject);

            Assert.IsNotNull(_instance);

            // Inject after we've instantiated and set _instance so that we can support circular dependencies
            // as PostInject or field parameters
            _container.InjectExplicit(
                _instance, Enumerable.Empty<TypeValuePair>(), true,
                TypeAnalyzer.GetInfo(_instance.GetType()), context, _id.ConcreteIdentifier);
        }
コード例 #31
0
 public Type GetInstanceType(InjectContext context)
 {
     return(_componentType);
 }
コード例 #32
0
 public abstract IEnumerator <List <object> > GetAllInstancesWithInjectSplit(
     InjectContext context, List <TypeValuePair> args);
コード例 #33
0
ファイル: Lazy.cs プロジェクト: krishx007/Zenject
 public Lazy(DiContainer container, InjectContext context)
 {
     _container = container;
     _context   = context;
 }
コード例 #34
0
 static void OnDespawned(InjectContext that)
 {
     that.SetDefaults();
 }
コード例 #35
0
 protected abstract GameObject GetGameObject(InjectContext context);
コード例 #36
0
 public Type GetInstanceType(InjectContext context)
 {
     return(_dependencyType);
 }
コード例 #37
0
 public Type GetInstanceType(InjectContext context)
 {
     return(typeof(TContract));
 }
コード例 #38
0
 public override object GetInstance(InjectContext context)
 {
     Assert.That(typeof(T).DerivesFromOrEqual(context.MemberType));
     return(Instantiator.Instantiate <T>(_template));
 }
コード例 #39
0
        public static IEnumerable <ZenjectResolveException> ValidateContract(DiContainer container, InjectContext context)
        {
            var matches = container.GetProviderMatches(context);

            if (matches.Count == 1)
            {
                foreach (var error in matches.Single().ValidateBinding(context))
                {
                    yield return(error);
                }
            }
            else
            {
                if (ReflectionUtil.IsGenericList(context.MemberType))
                {
                    var subContext = context.ChangeMemberType(context.MemberType.GetGenericArguments().Single());

                    matches = container.GetProviderMatches(subContext);

                    if (matches.IsEmpty())
                    {
                        if (!context.Optional)
                        {
                            if (container.FallbackProvider != null)
                            {
                                foreach (var error in container.FallbackProvider.ValidateBinding(context))
                                {
                                    yield return(error);
                                }
                            }
                            else
                            {
                                yield return(new ZenjectResolveException(
                                                 "Could not find dependency with type 'List<{0}>'{1}.  If the empty list is also valid, you can allow this by using the [InjectOptional] attribute.' \nObject graph:\n{2}"
                                                 .Fmt(
                                                     subContext.MemberType.Name(),
                                                     (context.ObjectType == null ? "" : " when injecting into '{0}'".Fmt(context.ObjectType.Name())),
                                                     context.GetObjectGraphString())));
                            }
                        }
                    }
                    else
                    {
                        foreach (var match in matches)
                        {
                            foreach (var error in match.ValidateBinding(context))
                            {
                                yield return(error);
                            }
                        }
                    }
                }
                else
                {
                    if (!context.Optional)
                    {
                        if (matches.IsEmpty())
                        {
                            if (container.FallbackProvider != null)
                            {
                                foreach (var error in container.FallbackProvider.ValidateBinding(context))
                                {
                                    yield return(error);
                                }
                            }
                            else
                            {
                                yield return(new ZenjectResolveException(
                                                 "Could not find required dependency with type '{0}'{1} \nObject graph:\n{2}"
                                                 .Fmt(
                                                     context.MemberType.Name(),
                                                     (context.ObjectType == null ? "" : " when injecting into '{0}'".Fmt(context.ObjectType.Name())),
                                                     context.GetObjectGraphString())));
                            }
                        }
                        else
                        {
                            yield return(new ZenjectResolveException(
                                             "Found multiple matches when only one was expected for dependency with type '{0}'{1} \nObject graph:\n{2}"
                                             .Fmt(
                                                 context.MemberType.Name(),
                                                 (context.ObjectType == null ? "" : " when injecting into '{0}'".Fmt(context.ObjectType.Name())),
                                                 context.GetObjectGraphString())));
                        }
                    }
                }
            }
        }
コード例 #40
0
 public Type GetInstanceType(InjectContext context)
 {
     return(context.MemberType);
 }
 protected override GameObject GetGameObject(InjectContext context)
 {
     return(_gameObject);
 }
コード例 #42
0
 public Type GetInstanceType(InjectContext context)
 {
     return(_concreteType);
 }
コード例 #43
0
 Foo GetFoo(InjectContext ctx)
 {
     return(new Foo());
 }
コード例 #44
0
        public void GetAllInstancesWithInjectSplit(
            InjectContext context, List <TypeValuePair> args, out Action injectAction, List <object> buffer)
        {
            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 || TypeAnalyzer.ShouldAllowDuringValidation(_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);
            }

            injectAction = () =>
            {
                try
                {
                    var extraArgs = ZenPools.SpawnList <TypeValuePair>();

                    extraArgs.AllocFreeAddRange(_extraArguments);
                    extraArgs.AllocFreeAddRange(args);

                    _container.InjectExplicit(instance, _componentType, extraArgs, context, _concreteIdentifier);

                    Assert.That(extraArgs.Count == 0);

                    ZenPools.DespawnList <TypeValuePair>(extraArgs);

                    if (_instantiateCallback != null)
                    {
                        _instantiateCallback(context, instance);
                    }
                }
                finally
                {
                    if (wasActive && ShouldToggleActive)
                    {
                        gameObj.SetActive(true);
                    }
                }
            };

            buffer.Add(instance);
        }
コード例 #45
0
 public override IEnumerable <ZenjectResolveException> ValidateBinding(InjectContext context)
 {
     return(BindingValidator.ValidateObjectGraph(_container, typeof(T)));
 }
コード例 #46
0
 public abstract List <object> GetAllInstancesWithInjectSplit(
     InjectContext context, List <TypeValuePair> args, out Action injectAction);
コード例 #47
0
 public override IEnumerable <ZenjectResolveException> ValidateBinding(InjectContext context)
 {
     return(BindingValidator.ValidateObjectGraph(_container, _componentType, context, null));
 }
コード例 #48
0
 public override IEnumerable <ZenjectResolveException> ValidateBinding(Type contractType, InjectContext context)
 {
     return(Enumerable.Empty <ZenjectResolveException>());
 }
コード例 #49
0
 public override IEnumerable <ZenjectResolveException> ValidateBinding(InjectContext context)
 {
     // Can't do much here if it's a method
     yield break;
 }
コード例 #50
0
ファイル: CachedProvider.cs プロジェクト: skistua/Zenject
 public Type GetInstanceType(InjectContext context)
 {
     return(_creator.GetInstanceType(context));
 }
コード例 #51
0
 public IEnumerable <ZenjectResolveException> ValidateBinding(InjectContext context)
 {
     return(_container.ValidateObjectGraph(ComponentType, context));
 }
コード例 #52
0
 public Type GetInstanceType(InjectContext context)
 {
     return(_resourceType);
 }
コード例 #53
0
 public Type GetInstanceType(InjectContext context)
 {
     return(typeof(GameObject));
 }
コード例 #54
0
 public override IEnumerable <ZenjectResolveException> ValidateBinding(InjectContext context)
 {
     return(_lazyCreator.ValidateBinding(_componentType, context));
 }
コード例 #55
0
        public override object GetInstance(InjectContext context)
        {
            Assert.That(_concreteType.DerivesFromOrEqual(context.MemberType));

            return(_container.InstantiatePrefabResourceForComponent(_concreteType, _resourcePath));
        }
コード例 #56
0
 public override object GetInstance(InjectContext context)
 {
     return(_lazyCreator.GetComponent(_componentType, context));
 }
コード例 #57
0
 public override IEnumerable <ZenjectResolveException> ValidateBinding(InjectContext context)
 {
     return(_creator.ValidateBinding(context));
 }
コード例 #58
0
 public override object GetInstance(InjectContext context)
 {
     return(_creator.GetInstance(context));
 }
コード例 #59
0
        public GameObject Instantiate(InjectContext context, List <TypeValuePair> args, out Action injectAction)
        {
            Assert.That(_argumentTarget == null || _argumentTarget.DerivesFromOrEqual(context.MemberType));

            bool shouldMakeActive;
            var  gameObject = _container.CreateAndParentPrefab(
                GetPrefab(), _gameObjectBindInfo, context, out shouldMakeActive);

            Assert.IsNotNull(gameObject);

            injectAction = () =>
            {
                var allArgs = ZenPools.SpawnList <TypeValuePair>();

                allArgs.AllocFreeAddRange(_extraArguments);
                allArgs.AllocFreeAddRange(args);

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

                Component targetComponent = null;

                if (_argumentTarget == null || allArgs.IsEmpty())
                {
                    _container.InjectGameObject(gameObject);
                }
                else
                {
                    targetComponent = _container.InjectGameObjectForComponentExplicit(
                        gameObject, _argumentTarget, allArgs, context, null);

                    Assert.That(allArgs.Count == 0);
                }

                ZenPools.DespawnList <TypeValuePair>(allArgs);

                if (shouldMakeActive && !_container.IsValidating)
                {
#if ZEN_INTERNAL_PROFILING
                    using (ProfileTimers.CreateTimedBlock("User Code"))
#endif
                    {
                        gameObject.SetActive(true);
                    }
                }

                if (_instantiateCallback != null)
                {
                    var callbackObjects = ZenPools.SpawnHashSet <object>();

                    foreach (var type in _instantiateCallbackTypes)
                    {
                        var obj = gameObject.GetComponentInChildren(type);

                        if (obj != null)
                        {
                            callbackObjects.Add(obj);
                        }
                    }

                    foreach (var obj in callbackObjects)
                    {
                        _instantiateCallback(context, obj);
                    }

                    ZenPools.DespawnHashSet(callbackObjects);
                }
            };

            return(gameObject);
        }
コード例 #60
0
 public Type GetInstanceType(InjectContext context)
 {
     return(typeof(TValue));
 }