Пример #1
0
        protected override void GetInjectableMonoBehaviours(List <MonoBehaviour> monoBehaviours)
        {
            var scene = gameObject.scene;

            UniDiUtilInternal.AddStateMachineBehaviourAutoInjectersInScene(scene);
            UniDiUtilInternal.GetInjectableMonoBehavioursInScene(scene, monoBehaviours);
        }
Пример #2
0
 public static void AssertInstanceDerivesFromOrEqual(object instance, Type baseType)
 {
     if (!UniDiUtilInternal.IsNull(instance))
     {
         Assert.That(instance.GetType().DerivesFromOrEqual(baseType),
                     "Invalid type given during bind command.  Expected type '{0}' to derive from type '{1}'", instance.GetType(), baseType);
     }
 }
Пример #3
0
 public static void AssertInstanceDerivesFromOrEqual(object instance, IEnumerable <Type> parentTypes)
 {
     if (!UniDiUtilInternal.IsNull(instance))
     {
         foreach (var baseType in parentTypes)
         {
             AssertInstanceDerivesFromOrEqual(instance, baseType);
         }
     }
 }
Пример #4
0
        public static void AssertIsValidGameObject(GameObject gameObject)
        {
            Assert.That(!UniDiUtilInternal.IsNull(gameObject), "Received null game object during bind command");

#if UNITY_EDITOR
            // Unfortunately we can't do this check because asset bundles return PrefabType.None here
            // as discussed here: https://github.com/svermeulen/UniDi/issues/269#issuecomment-323419408
            //Assert.That(PrefabUtility.GetPrefabType(gameObject) != PrefabType.Prefab,
            //"Expected game object but found prefab instead with name '{0}' during bind command", gameObject.name);
#endif
        }
Пример #5
0
        protected override void GetInjectableMonoBehaviours(List <MonoBehaviour> monoBehaviours)
        {
            UniDiUtilInternal.AddStateMachineBehaviourAutoInjectersUnderGameObject(gameObject);

            // We inject on all components on the root except ourself
            foreach (var monoBehaviour in GetComponents <MonoBehaviour>())
            {
                if (monoBehaviour == null)
                {
                    // Missing script
                    continue;
                }

                if (!UniDiUtilInternal.IsInjectableMonoBehaviourType(monoBehaviour.GetType()))
                {
                    continue;
                }

                if (monoBehaviour == this)
                {
                    continue;
                }

                monoBehaviours.Add(monoBehaviour);
            }

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

                if (child != null)
                {
                    UniDiUtilInternal.GetInjectableMonoBehavioursUnderGameObject(
                        child.gameObject, monoBehaviours);
                }
            }
        }
Пример #6
0
        DiContainer CreateTempContainer(List <TypeValuePair> args)
        {
            var tempSubContainer = Container.CreateSubContainer();

            var allInjectables = GetAllInjectableIncludingBaseTypes();

            foreach (var argPair in args)
            {
                // We need to intelligently match on the exact parameters here to avoid the issue
                // brought up in github issue #217
                var match = allInjectables
                            .Where(x => argPair.Type.DerivesFromOrEqual(x.MemberType))
                            .OrderBy(x => UniDiUtilInternal.GetInheritanceDelta(argPair.Type, x.MemberType)).FirstOrDefault();

                Assert.That(match != null,
                            "Could not find match for argument type '{0}' when injecting into sub container installer '{1}'",
                            argPair.Type, _installerType);

                tempSubContainer.Bind(match.MemberType)
                .FromInstance(argPair.Value).WhenInjectedInto(_installerType);
            }

            return(tempSubContainer);
        }
Пример #7
0
        static void InstantiateAndInitialize()
        {
#if UNITY_EDITOR
            ProfileBlock.UnityMainThread = Thread.CurrentThread;
#endif

            Assert.That(FindObjectsOfType <ProjectContext>().IsEmpty(),
                        "Tried to create multiple instances of ProjectContext!");

            var prefab = TryGetPrefab();

            var prefabWasActive = false;

#if UNIDI_INTERNAL_PROFILING
            using (ProfileTimers.CreateTimedBlock("GameObject.Instantiate"))
#endif
            {
                if (prefab == null)
                {
                    _instance = new GameObject("ProjectContext")
                                .AddComponent <ProjectContext>();
                }
                else
                {
                    prefabWasActive = prefab.activeSelf;

                    GameObject gameObjectInstance;
#if UNITY_EDITOR
                    if (prefabWasActive)
                    {
                        // This ensures the prefab's Awake() methods don't fire (and, if in the editor, that the prefab file doesn't get modified)
                        gameObjectInstance = GameObject.Instantiate(prefab, UniDiUtilInternal.GetOrCreateInactivePrefabParent());
                        gameObjectInstance.SetActive(false);
                        gameObjectInstance.transform.SetParent(null, false);
                    }
                    else
                    {
                        gameObjectInstance = GameObject.Instantiate(prefab);
                    }
#else
                    if (prefabWasActive)
                    {
                        prefab.SetActive(false);
                        gameObjectInstance = GameObject.Instantiate(prefab);
                        prefab.SetActive(true);
                    }
                    else
                    {
                        gameObjectInstance = GameObject.Instantiate(prefab);
                    }
#endif

                    _instance = gameObjectInstance.GetComponent <ProjectContext>();

                    Assert.IsNotNull(_instance,
                                     "Could not find ProjectContext component on prefab 'Resources/{0}.prefab'", ProjectContextResourcePath);
                }
            }

            // Note: We use Initialize instead of awake here in case someone calls
            // ProjectContext.Instance while ProjectContext is initializing
            _instance.Initialize();

            if (prefabWasActive)
            {
#if UNIDI_INTERNAL_PROFILING
                using (ProfileTimers.CreateTimedBlock("User Code"))
#endif
                {
                    // We always instantiate it as disabled so that Awake and Start events are triggered after inject
                    _instance.gameObject.SetActive(true);
                }
            }
        }
Пример #8
0
 protected override void GetInjectableMonoBehaviours(List <MonoBehaviour> monoBehaviours)
 {
     UniDiUtilInternal.AddStateMachineBehaviourAutoInjectersUnderGameObject(gameObject);
     UniDiUtilInternal.GetInjectableMonoBehavioursUnderGameObject(gameObject, monoBehaviours);
 }
Пример #9
0
 public override IEnumerable <GameObject> GetRootGameObjects()
 {
     return(UniDiUtilInternal.GetRootGameObjects(gameObject.scene));
 }