public object SpawnInstance(IObjectResolver resolver)
        {
            Component component;
            var       parent = destination.GetParent();

            if (destination.Prefab != null)
            {
                if (destination.Prefab.gameObject.activeSelf)
                {
                    destination.Prefab.gameObject.SetActive(false);
                }
                component = UnityEngine.Object.Instantiate(destination.Prefab, parent);
            }
            else
            {
                var name = string.IsNullOrEmpty(destination.NewGameObjectName)
                    ? ImplementationType.Name
                    : destination.NewGameObjectName;
                var gameObject = new GameObject(name);
                gameObject.SetActive(false);
                if (parent != null)
                {
                    gameObject.transform.SetParent(parent);
                }
                component = gameObject.AddComponent(ImplementationType);
            }
            injector.Inject(component, resolver, Parameters);
            component.gameObject.SetActive(true);
            return(component);
        }
Пример #2
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);
        }