示例#1
0
        protected override void GetInjectableMonoBehaviours(List <MonoBehaviour> monoBehaviours)
        {
            var scene = gameObject.scene;

            ZenUtilInternal.AddStateMachineBehaviourAutoInjectersInScene(scene);
            ZenUtilInternal.GetInjectableMonoBehavioursInScene(scene, monoBehaviours);
        }
示例#2
0
        protected override void GetInjectableMonoBehaviours(List <MonoBehaviour> monoBehaviours)
        {
            // We inject on all components on the root except ourself
            foreach (var monoBehaviour in GetComponents <MonoBehaviour>())
            {
                if (monoBehaviour == null)
                {
                    // Missing script
                    continue;
                }

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

                if (monoBehaviour == this)
                {
                    continue;
                }

                monoBehaviours.Add(monoBehaviour);
            }

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

                if (child != null)
                {
                    ZenUtilInternal.GetInjectableMonoBehaviours(
                        child.gameObject, monoBehaviours);
                }
            }
        }
示例#3
0
        public void ForceUnloadAllScenes(bool immediate = false)
        {
            // OnApplicationQuit should always be called before OnDestroy
            // (Unless it is destroyed manually)
            Assert.That(!IsDestroyed);

            // Destroy the scene contexts from bottom to top
            // Since this is the reverse order that they were loaded in
            foreach (var sceneContext in ZenUtilInternal.GetAllSceneContexts().Reverse().ToList())
            {
                Log.Debug("Destroying scene context for scene '{0}'", sceneContext.gameObject.scene.name);

                if (immediate)
                {
                    GameObject.DestroyImmediate(sceneContext.gameObject);
                }
                else
                {
                    GameObject.Destroy(sceneContext.gameObject);
                }
            }

            if (immediate)
            {
                Assert.That(!IsDestroyed);
                GameObject.DestroyImmediate(this.gameObject);
                Assert.That(IsDestroyed);
            }
            else
            {
                GameObject.Destroy(this.gameObject);
            }
        }
示例#4
0
        static void InstantiateAndInitialize()
        {
            Assert.That(GameObject.FindObjectsOfType <ProjectContext>().IsEmpty(),
                        "Tried to create multiple instances of ProjectContext!");

            var prefab = TryGetPrefab();

            var prefabWasActive = false;

            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, ZenUtilInternal.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)
            {
                // We always instantiate it as disabled so that Awake and Start events are triggered after inject
                _instance.gameObject.SetActive(true);
            }
        }
示例#5
0
 public static void AssertInstanceDerivesFromOrEqual(object instance, Type baseType)
 {
     if (!ZenUtilInternal.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.Name());
     }
 }
示例#6
0
        public static ProfileBlock Start(string sampleName)
        {
            if (ZenUtilInternal.IsOutsideUnity())
            {
                return(null);
            }

            return(StartInternal(sampleName));
        }
示例#7
0
        public static ProfileBlock Start(string sampleNameFormat, object obj1, object obj2)
        {
            if (ZenUtilInternal.IsOutsideUnity())
            {
                return(null);
            }

            return(StartInternal(string.Format(sampleNameFormat, obj1, obj2)));
        }
示例#8
0
        public static void AssertIsValidGameObject(GameObject gameObject)
        {
            Assert.That(!ZenUtilInternal.IsNull(gameObject), "Received null game object during bind command");

#if UNITY_EDITOR
            Assert.That(PrefabUtility.GetPrefabType(gameObject) != PrefabType.Prefab,
                        "Expected game object but found prefab instead with name '{0}' during bind command", gameObject.name);
#endif
        }
示例#9
0
 public override int GetHashCode()
 {
     unchecked // Overflow is fine, just wrap
     {
         int hash = 17;
         hash = hash * 29 + (this.ConcreteIdentifier == null ? 0 : this.ConcreteIdentifier.GetHashCode());
         hash = hash * 29 + (ZenUtilInternal.IsNull(this.Prefab) ? 0 : this.Prefab.GetHashCode());
         return(hash);
     }
 }
示例#10
0
 public static void AssertInstanceDerivesFromOrEqual(object instance, IEnumerable <Type> parentTypes)
 {
     if (!ZenUtilInternal.IsNull(instance))
     {
         foreach (var baseType in parentTypes)
         {
             AssertInstanceDerivesFromOrEqual(instance, baseType);
         }
     }
 }
示例#11
0
        public static void AssertIsValidPrefab(UnityEngine.Object prefab)
        {
            Assert.That(!ZenUtilInternal.IsNull(prefab), "Received null prefab during bind command");

//#if UNITY_EDITOR
//            // This won't execute in dll builds sadly
//            Assert.That(PrefabUtility.GetPrefabType(prefab) == PrefabType.Prefab,
//                "Expected prefab but found game object with name '{0}' during bind command", prefab.name);
//#endif
        }
示例#12
0
        public static void AssertIsValidPrefab(UnityEngine.Object prefab)
        {
            Assert.That(!ZenUtilInternal.IsNull(prefab), "Received null prefab 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/Zenject/issues/269#issuecomment-323419408
            //Assert.That(PrefabUtility.GetPrefabType(prefab) == PrefabType.Prefab,
            //"Expected prefab but found game object with name '{0}' during bind command", prefab.name);
#endif
        }
示例#13
0
        public static void AssertIsValidGameObject(GameObject gameObject)
        {
            Assert.That(!ZenUtilInternal.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/modesttree/Zenject/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
        }
    public U_PrefabFacade Create(Object prefab, C c)
    {
        // This is marchingninja's solution:
        var instance   = (GameObject)Object.Instantiate(prefab);
        var components = new List <MonoBehaviour>();

        ZenUtilInternal.GetInjectableMonoBehavioursUnderGameObject(instance, components);
        foreach (MonoBehaviour component in components)
        {
            _container.Inject(component, new[] { c });
        }

        return(instance.GetComponent <U_PrefabFacade>());
    }
示例#15
0
        protected ScopeBinder FromInstanceBase(object instance, bool allowNull)
        {
            if (!allowNull)
            {
                Assert.That(!ZenUtilInternal.IsNull(instance),
                            "Found null instance for type '{0}' in FromInstance method",
                            ConcreteTypes.First().Name());
            }

            BindingUtil.AssertInstanceDerivesFromOrEqual(instance, AllParentTypes);

            SubFinalizer = new ScopableBindingFinalizer(
                BindInfo, SingletonTypes.ToInstance, instance,
                (container, type) => new InstanceProvider(container, type, instance));

            return(new ScopeBinder(BindInfo));
        }
        DiContainer CreateTempContainer(List <TypeValuePair> args)
        {
            var tempSubContainer = Container.CreateSubContainer();

            var installerInjectables = TypeAnalyzer.GetInfo(_installerType);

            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 = installerInjectables.AllInjectables
                            .Where(x => argPair.Type.DerivesFromOrEqual(x.MemberType))
                            .OrderBy(x => ZenUtilInternal.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);
        }
示例#17
0
        public static IEnumerable <Component> GetInjectableComponents(GameObject gameObject)
        {
            foreach (var component in ZenUtilInternal.GetInjectableComponentsBottomUp(gameObject, true))
            {
                if (component == null)
                {
                    // This warning about fiBackupSceneStorage appears in normal cases so just ignore
                    // Not sure what it is
                    if (gameObject.name != "fiBackupSceneStorage")
                    {
                        Log.Warn("Zenject: Found null component on game object '{0}'.  Possible missing script.", gameObject.name);
                    }
                    continue;
                }

                if (component.GetType().DerivesFrom <MonoInstaller>())
                {
                    // Do not inject on installers since these are always injected before they are installed
                    continue;
                }

                yield return(component);
            }
        }
示例#18
0
 protected override void GetInjectableMonoBehaviours(List <MonoBehaviour> monoBehaviours)
 {
     ZenUtilInternal.AddStateMachineBehaviourAutoInjectersUnderGameObject(this.gameObject);
     ZenUtilInternal.GetInjectableMonoBehavioursUnderGameObject(this.gameObject, monoBehaviours);
 }
示例#19
0
 protected override void GetInjectableMonoBehaviours(List <MonoBehaviour> monoBehaviours)
 {
     ZenUtilInternal.GetInjectableMonoBehaviours(this.gameObject.scene, monoBehaviours);
 }
示例#20
0
 public override IEnumerable <GameObject> GetRootGameObjects()
 {
     return(ZenUtilInternal.GetRootGameObjects(gameObject.scene));
 }
示例#21
0
 protected override IEnumerable <MonoBehaviour> GetInjectableMonoBehaviours()
 {
     return(ZenUtilInternal.GetInjectableMonoBehaviours(this.gameObject.scene));
 }