From 9fe4cc7624ec5c2c6ffee43a34d7a2adf3be5d12 Mon Sep 17 00:00:00 2001 From: mob-sakai Date: Tue, 15 Jan 2019 15:49:39 +0900 Subject: [PATCH] fix #35; On prefab edit mode, unnecessary UIParticleOverlayCamera is generated in the scene --- UIParticleOverlayCamera.cs | 44 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/UIParticleOverlayCamera.cs b/UIParticleOverlayCamera.cs index 2433e9e..68ed592 100644 --- a/UIParticleOverlayCamera.cs +++ b/UIParticleOverlayCamera.cs @@ -1,6 +1,9 @@ using System.Collections; using System.Collections.Generic; using UnityEngine; +#if UNITY_2018_3_OR_NEWER && UNITY_EDITOR +using PrefabStageUtility = UnityEditor.Experimental.SceneManagement.PrefabStageUtility; +#endif namespace Coffee.UIExtensions { @@ -20,6 +23,28 @@ namespace Coffee.UIExtensions { get { +#if UNITY_2018_3_OR_NEWER && UNITY_EDITOR + // If current scene is prefab mode, create OverlayCamera for editor. + var prefabStage = PrefabStageUtility.GetCurrentPrefabStage (); + if (prefabStage != null && prefabStage.scene.isLoaded) + { + if (!s_InstanceForPrefabMode) + { + // This GameObject is not saved in prefab. + // This GameObject is not shown in the hierarchy view. + // When you exit prefab mode, this GameObject is destroyed automatically. + var go = new GameObject (typeof (UIParticleOverlayCamera).Name + "_ForEditor") + { + hideFlags = HideFlags.HideAndDontSave, + tag = "EditorOnly", + }; + UnityEngine.SceneManagement.SceneManager.MoveGameObjectToScene (go, prefabStage.scene); + s_InstanceForPrefabMode = go.AddComponent (); + } + return s_InstanceForPrefabMode; + } +#endif + // Find instance in scene, or create new one. if (object.ReferenceEquals (s_Instance, null)) { @@ -56,12 +81,24 @@ namespace Coffee.UIExtensions Camera cameraForOvrelay { get { return m_Camera ? m_Camera : (m_Camera = GetComponent ()) ? m_Camera : (m_Camera = gameObject.AddComponent ()); } } Camera m_Camera; static UIParticleOverlayCamera s_Instance; +#if UNITY_2018_3_OR_NEWER && UNITY_EDITOR + static UIParticleOverlayCamera s_InstanceForPrefabMode; +#endif /// /// Awake is called when the script instance is being loaded. /// void Awake () { +#if UNITY_2018_3_OR_NEWER && UNITY_EDITOR + // OverlayCamera for editor. + if (hideFlags == HideFlags.HideAndDontSave || s_InstanceForPrefabMode == this) + { + s_InstanceForPrefabMode = GetComponent (); + return; + } +#endif + // Hold the instance. if (s_Instance == null) { @@ -100,6 +137,13 @@ namespace Coffee.UIExtensions /// void OnDestroy () { +#if UNITY_2018_3_OR_NEWER && UNITY_EDITOR + if (s_InstanceForPrefabMode == this) + { + s_InstanceForPrefabMode = null; + } +#endif + // Clear instance on destroy. if (s_Instance == this) {