예제 #1
0
        public static RegistrationBuilder RegisterComponentInHierarchy <T>(this IContainerBuilder builder)
        {
            var lifetimeScope = (LifetimeScope)builder.ApplicationOrigin;
            var scene         = lifetimeScope.gameObject.scene;
            var component     = default(T);

            var gameObjectBuffer = UnityEngineObjectListBuffer <GameObject> .Get();

            scene.GetRootGameObjects(gameObjectBuffer);
            foreach (var gameObject in gameObjectBuffer)
            {
                component = gameObject.GetComponentInChildren <T>(true);
                if (component != null)
                {
                    break;
                }
            }

            if (component == null)
            {
                throw new VContainerException(typeof(T), $"Component {typeof(T)} is not in this scene {scene.path}");
            }

            var registrationBuilder = builder.RegisterInstance(component).As(typeof(T));

            if (component is MonoBehaviour monoBehaviour)
            {
                registrationBuilder.As <MonoBehaviour>();
                builder.RegisterBuildCallback(container => container.Inject(monoBehaviour));
            }
            return(registrationBuilder);
        }
        public static void InjectGameObject(this IObjectResolver resolver, GameObject gameObject)
        {
            var buffer = UnityEngineObjectListBuffer <MonoBehaviour> .Get();

            void InjectGameObjectRecursive(GameObject current)
            {
                if (current == null)
                {
                    return;
                }

                buffer.Clear();
                current.GetComponents(buffer);
                foreach (var monoBehaviour in buffer)
                {
                    resolver.Inject(monoBehaviour);
                }

                var transform = current.transform;

                for (var i = 0; i < transform.childCount; i++)
                {
                    var child = transform.GetChild(i);
                    InjectGameObjectRecursive(child.gameObject);
                }
            }

            InjectGameObjectRecursive(gameObject);
        }
예제 #3
0
        Component FindComponent(IObjectResolver resolver)
        {
            Component component = null;
            var       parent    = destination.GetParent();

            if (parent != null)
            {
                component = parent.GetComponentInChildren(ImplementationType);
                if (component == null)
                {
                    throw new VContainerException(ImplementationType, $"Component {ImplementationType} is not in the parent {parent.name}");
                }
            }
            else if (destination.Scene.IsValid())
            {
                var gameObjectBuffer = UnityEngineObjectListBuffer <GameObject> .Get();

                destination.Scene.GetRootGameObjects(gameObjectBuffer);
                foreach (var gameObject in gameObjectBuffer)
                {
                    component = gameObject.GetComponentInChildren(ImplementationType, true);
                    if (component != null)
                    {
                        break;
                    }
                }
                if (component == null)
                {
                    throw new VContainerException(ImplementationType, $"Component {ImplementationType} is not in this scene {destination.Scene.path}");
                }
            }
            else
            {
                throw new VContainerException(ImplementationType, "Invalid Component find target");
            }

            if (component is MonoBehaviour monoBehaviour)
            {
                injector.Inject(monoBehaviour, resolver, Parameters);
            }
            return(component);
        }