示例#1
0
        void InstallBindings(
            InstallerExtraArgs installerExtraArgs)
        {
            _container.DefaultParent = this.transform;

            _container.Bind <Context>().FromInstance(this);
            _container.Bind <GameObjectContext>().FromInstance(this);

            if (_kernel == null)
            {
                _container.Bind <MonoKernel>()
                .To <DefaultGameObjectKernel>().FromNewComponentOn(this.gameObject).AsSingle().NonLazy();
            }
            else
            {
                _container.Bind <MonoKernel>().FromInstance(_kernel).AsSingle().NonLazy();
            }

            InstallSceneBindings();

            var extraArgsMap = new Dictionary <Type, List <TypeValuePair> >();

            if (installerExtraArgs != null)
            {
                extraArgsMap.Add(
                    installerExtraArgs.InstallerType, installerExtraArgs.ExtraArgs);
            }

            InstallInstallers();
        }
示例#2
0
        public void Construct(
            DiContainer parentContainer,
            [InjectOptional]
            InstallerExtraArgs installerExtraArgs)
        {
            Assert.IsNull(_container);

            _container = parentContainer.CreateSubContainer();

            _container.LazyInstanceInjector
            .AddInstances(GetInjectableMonoBehaviours().Cast <object>());

            foreach (var instance in _container.LazyInstanceInjector.Instances)
            {
                if (instance is MonoKernel)
                {
                    Assert.That(ReferenceEquals(instance, _kernel),
                                "Found MonoKernel derived class that is not hooked up to GameObjectContext.  If you use MonoKernel, you must indicate this to GameObjectContext by dragging and dropping it to the Kernel field in the inspector");
                }
            }

            _container.IsInstalling = true;

            try
            {
                InstallBindings(installerExtraArgs);
            }
            finally
            {
                _container.IsInstalling = false;
            }

            Log.Debug("GameObjectContext: Injecting into child components...");

            _container.LazyInstanceInjector.LazyInjectAll();

            Assert.That(_dependencyRoots.IsEmpty());
            _dependencyRoots.AddRange(_container.ResolveDependencyRoots());

            if (_container.IsValidating)
            {
                // The root-level Container has its ValidateValidatables method
                // called explicitly - however, this is not so for sub-containers
                // so call it here instead
                _container.ValidateValidatables();
            }

            Log.Debug("GameObjectContext: Initialized successfully");
        }
示例#3
0
        public void Construct(
            DiContainer parentContainer,
            [InjectOptional]
            InstallerExtraArgs installerExtraArgs)
        {
            Assert.IsNull(_container);

            _container = parentContainer.CreateSubContainer();

            var componentInjecter = new InitialComponentsInjecter(
                _container, GetInjectableComponents().ToList());

            foreach (var component in componentInjecter.Components)
            {
                if (component is MonoKernel)
                {
                    Assert.That(component == _kernel,
                                "Found MonoKernel derived class that is not hooked up to GameObjectContext.  If you use MonoKernel, you must indicate this to GameObjectContext by dragging and dropping it to the Kernel field in the inspector");
                }
            }

            _container.IsInstalling = true;

            try
            {
                InstallBindings(installerExtraArgs, componentInjecter);
            }
            finally
            {
                _container.IsInstalling = false;
            }

            Log.Debug("GameObjectContext: Injecting into child components...");

            componentInjecter.LazyInjectComponents();

            Assert.That(_dependencyRoots.IsEmpty());
            _dependencyRoots.AddRange(_container.ResolveDependencyRoots());

            Log.Debug("GameObjectContext: Initialized successfully");
        }
示例#4
0
        public void Construct(
            DiContainer parentContainer,
            [InjectOptional]
            InstallerExtraArgs installerExtraArgs)
        {
            Assert.IsNull(_container);

            _container = parentContainer.CreateSubContainer();

            foreach (var instance in GetInjectableMonoBehaviours().Cast <object>())
            {
                if (instance is MonoKernel)
                {
                    Assert.That(ReferenceEquals(instance, _kernel),
                                "Found MonoKernel derived class that is not hooked up to GameObjectContext.  If you use MonoKernel, you must indicate this to GameObjectContext by dragging and dropping it to the Kernel field in the inspector");
                }

                _container.QueueForInject(instance);
            }

            _container.IsInstalling = true;

            try
            {
                InstallBindings(installerExtraArgs);
            }
            finally
            {
                _container.IsInstalling = false;
            }

            Log.Debug("GameObjectContext: Injecting into child components...");

            _container.FlushInjectQueue();

            Assert.That(_dependencyRoots.IsEmpty());
            _dependencyRoots.AddRange(_container.ResolveDependencyRoots());

            if (_container.IsValidating)
            {
                // The root-level Container has its ValidateValidatables method
                // called explicitly - however, this is not so for sub-containers
                // so call it here instead
                _container.ValidateValidatables();
            }

            Log.Debug("GameObjectContext: Initialized successfully");

            // Normally, the IInitializable.Initialize method would be called during MonoKernel.Start
            // However, this behaviour is undesirable for dynamically created objects, since Unity
            // has the strange behaviour of waiting until the end of the frame to call Start() on
            // dynamically created objects, which means that any GameObjectContext that is created
            // dynamically via a factory cannot be used immediately after calling Create(), since
            // it will not have been initialized
            // So we have chosen to diverge from Unity behaviour here and trigger IInitializable.Initialize
            // immediately - but only when the GameObjectContext is created dynamically.  For any
            // GameObjectContext's that are placed in the scene, we still want to execute
            // IInitializable.Initialize during Start()
            if (gameObject.scene.isLoaded && !_container.IsValidating)
            {
                _kernel = _container.Resolve <MonoKernel>();
                _kernel.ForceInitialize();
            }
        }
示例#5
0
        void InstallBindings(
            InstallerExtraArgs installerExtraArgs, InitialComponentsInjecter componentInjecter)
        {
            _container.DefaultParent = this.transform;

            _container.Bind<Context>().FromInstance(this);

            if (_kernel == null)
            {
                _container.Bind<MonoKernel>()
                    .To<DefaultGameObjectKernel>().FromComponent(this.gameObject).AsSingle().NonLazy();
            }
            else
            {
                _container.Bind<MonoKernel>().FromInstance(_kernel).AsSingle().NonLazy();
            }

            InstallSceneBindings(componentInjecter);

            var extraArgsMap = new Dictionary<Type, List<TypeValuePair>>();

            if (installerExtraArgs != null)
            {
                extraArgsMap.Add(
                    installerExtraArgs.InstallerType, installerExtraArgs.ExtraArgs);
            }

            InstallInstallers();
        }