// NOTE: This method will not return components that are within a GameObjectContext // It returns monobehaviours in a bottom-up order public static void GetInjectableMonoBehavioursUnderGameObject( GameObject gameObject, List <MonoBehaviour> injectableComponents) { #if ZEN_INTERNAL_PROFILING using (ProfileTimers.CreateTimedBlock("Searching Hierarchy")) #endif { GetInjectableMonoBehavioursUnderGameObjectInternal(gameObject, injectableComponents); } }
public void Awake() { #if ZEN_INTERNAL_PROFILING ProfileTimers.ResetAll(); using (ProfileTimers.CreateTimedBlock("Other")) #endif { Initialize(); } }
protected override void UpdateItem(IFixedTickable task) { #if ZEN_INTERNAL_PROFILING using (ProfileTimers.CreateTimedBlock("User Code")) #endif #if UNITY_EDITOR using (ProfileBlock.Start("{0}.FixedTick()", task.GetType())) #endif { task.FixedTick(); } }
public override void InstallBindings() { foreach (var installer in _leafInstallers) { Container.Inject(installer); #if ZEN_INTERNAL_PROFILING using (ProfileTimers.CreateTimedBlock("User Code")) #endif { installer.InstallBindings(); } } }
public static void GetInjectableMonoBehavioursInScene( Scene scene, List <MonoBehaviour> monoBehaviours) { #if ZEN_INTERNAL_PROFILING using (ProfileTimers.CreateTimedBlock("Searching Hierarchy")) #endif { foreach (var rootObj in GetRootGameObjects(scene)) { if (rootObj != null) { GetInjectableMonoBehavioursUnderGameObjectInternal(rootObj, monoBehaviours); } } } }
// Call this before calling GetInjectableMonoBehavioursUnderGameObject to ensure that the StateMachineBehaviour's // also get injected properly // The StateMachineBehaviour's cannot be retrieved until after the Start() method so we // need to use ZenjectStateMachineBehaviourAutoInjecter to do the injection at that // time for us public static void AddStateMachineBehaviourAutoInjectersUnderGameObject(GameObject root) { #if ZEN_INTERNAL_PROFILING using (ProfileTimers.CreateTimedBlock("Searching Hierarchy")) #endif { var animators = root.GetComponentsInChildren <Animator>(true); foreach (var animator in animators) { if (animator.gameObject.GetComponent <ZenjectStateMachineBehaviourAutoInjecter>() == null) { animator.gameObject.AddComponent <ZenjectStateMachineBehaviourAutoInjecter>(); } } } }
public TValue Spawn() { var item = GetInternal(); if (!Container.IsValidating) { #if ZEN_INTERNAL_PROFILING using (ProfileTimers.CreateTimedBlock("User Code")) #endif #if UNITY_EDITOR using (ProfileBlock.Start("{0}.Reinitialize", GetType())) #endif { Reinitialize(item); } } return(item); }
public void TriggerOnSpawned() { Assert.That(!_isSpawned); _isSpawned = true; for (int i = 0; i < _poolables.Count; i++) { #if ZEN_INTERNAL_PROFILING using (ProfileTimers.CreateTimedBlock("User Code")) #endif #if UNITY_EDITOR using (ProfileBlock.Start("{0}.OnSpawned", _poolables[i].GetType())) #endif { _poolables[i].OnSpawned(); } } }
public void TriggerOnDespawned() { Assert.That(_isSpawned); _isSpawned = false; // Call OnDespawned in the reverse order just like how dispose works for (int i = _poolables.Count - 1; i >= 0; i--) { #if ZEN_INTERNAL_PROFILING using (ProfileTimers.CreateTimedBlock("User Code")) #endif #if UNITY_EDITOR using (ProfileBlock.Start("{0}.OnDespawned", _poolables[i].GetType())) #endif { _poolables[i].OnDespawned(); } } }
public void OnGui() { foreach (var renderable in _renderables) { try { #if ZEN_INTERNAL_PROFILING using (ProfileTimers.CreateTimedBlock("User Code")) #endif #if UNITY_EDITOR using (ProfileBlock.Start("{0}.GuiRender()", renderable.Renderable.GetType())) #endif { renderable.Renderable.GuiRender(); } } catch (Exception e) { throw Assert.CreateException( e, "Error occurred while calling {0}.GuiRender", renderable.Renderable.GetType()); } } }
public void Despawn(TContract item) { Assert.That(!_inactiveItems.Contains(item), "Tried to return an item to pool {0} twice", GetType()); _activeCount--; _inactiveItems.Push(item); #if ZEN_INTERNAL_PROFILING using (ProfileTimers.CreateTimedBlock("User Code")) #endif #if UNITY_EDITOR using (ProfileBlock.Start("{0}.OnDespawned", GetType())) #endif { OnDespawned(item); } if (_inactiveItems.Count > _settings.MaxSize) { Resize(_settings.MaxSize); } }
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 ZEN_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, 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) { #if ZEN_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); } } }
public GameObject Instantiate(List <TypeValuePair> args, out Action injectAction) { var context = new InjectContext(_container, _argumentTarget, null); bool shouldMakeActive; var gameObject = _container.CreateAndParentPrefab( GetPrefab(), _gameObjectBindInfo, context, out shouldMakeActive); Assert.IsNotNull(gameObject); injectAction = () => { var allArgs = ZenPools.SpawnList <TypeValuePair>(); allArgs.AllocFreeAddRange(_extraArguments); allArgs.AllocFreeAddRange(args); if (_argumentTarget == null) { Assert.That( allArgs.IsEmpty(), "Unexpected arguments provided to prefab instantiator. Arguments are not allowed if binding multiple components in the same binding"); } Component targetComponent = null; if (_argumentTarget == null || allArgs.IsEmpty()) { _container.InjectGameObject(gameObject); } else { targetComponent = _container.InjectGameObjectForComponentExplicit( gameObject, _argumentTarget, allArgs, context, null); Assert.That(allArgs.Count == 0); } ZenPools.DespawnList <TypeValuePair>(allArgs); if (shouldMakeActive && !_container.IsValidating) { #if ZEN_INTERNAL_PROFILING using (ProfileTimers.CreateTimedBlock("User Code")) #endif { gameObject.SetActive(true); } } if (_instantiateCallback != null && _argumentTarget != null) { if (targetComponent == null) { targetComponent = gameObject.GetComponentInChildren(_argumentTarget); } if (targetComponent != null) { _instantiateCallback(context, targetComponent); } } }; return(gameObject); }
protected void InstallInstallers( List <InstallerBase> normalInstallers, List <Type> normalInstallerTypes, List <ScriptableObjectInstaller> scriptableObjectInstallers, List <MonoInstaller> installers, List <MonoInstaller> installerPrefabs) { CheckInstallerPrefabTypes(installers, installerPrefabs); // Ideally we would just have one flat list of all the installers // since that way the user has complete control over the order, but // that's not possible since Unity does not allow serializing lists of interfaces // (and it has to be an inteface since the scriptable object installers only share // the interface) // // So the best we can do is have a hard-coded order in terms of the installer type // // The order is: // - Normal installers given directly via code // - ScriptableObject installers // - MonoInstallers in the scene // - Prefab Installers // // We put ScriptableObject installers before the MonoInstallers because // ScriptableObjectInstallers are often used for settings (including settings // that are injected into other installers like MonoInstallers) var allInstallers = normalInstallers.Cast <IInstaller>() .Concat(scriptableObjectInstallers.Cast <IInstaller>()) .Concat(installers.Cast <IInstaller>()).ToList(); foreach (var installerPrefab in installerPrefabs) { Assert.IsNotNull(installerPrefab, "Found null installer prefab in '{0}'", GetType()); GameObject installerGameObject; #if ZEN_INTERNAL_PROFILING using (ProfileTimers.CreateTimedBlock("GameObject.Instantiate")) #endif { installerGameObject = GameObject.Instantiate(installerPrefab.gameObject); } installerGameObject.transform.SetParent(transform, false); var installer = installerGameObject.GetComponent <MonoInstaller>(); Assert.IsNotNull(installer, "Could not find installer component on prefab '{0}'", installerPrefab.name); allInstallers.Add(installer); } foreach (var installerType in normalInstallerTypes) { var installer = (InstallerBase)Container.Instantiate(installerType); #if ZEN_INTERNAL_PROFILING using (ProfileTimers.CreateTimedBlock("User Code")) #endif { installer.InstallBindings(); } } foreach (var installer in allInstallers) { Assert.IsNotNull(installer, "Found null installer in '{0}'", GetType()); Container.Inject(installer); #if ZEN_INTERNAL_PROFILING using (ProfileTimers.CreateTimedBlock("User Code")) #endif { installer.InstallBindings(); } } }
internal static void InitializeAndLoad() { Assert.That(FindObjectsOfType <NetworkClientManager>().IsEmpty(), "Tried to create multiple instances of NetworkClientManager!"); var prefab = TryGetPrefab(); var prefabWasActive = false; #if ZEN_INTERNAL_PROFILING using (ProfileTimers.CreateTimedBlock("GameObject.Instantiate")) #endif { if (prefab == null) { instance = new GameObject("NetworkClientManager") .AddComponent <NetworkClientManager>(); } 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 /* ,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 <NetworkClientManager>(); Assert.IsNotNull(instance, "Could not find NetworkClientManager component on prefab 'Resources/{0}.prefab'", path); } if (prefabWasActive) { { // We always instantiate it as disabled so that Awake and Start events are triggered after inject instance.gameObject.SetActive(true); } } } }