From 5639013d511057c67c2f66b18656559ef02a9cc7 Mon Sep 17 00:00:00 2001 From: Mane Function Date: Mon, 13 May 2019 17:35:42 +0300 Subject: [PATCH] Improved work in prefab view --- .../Scripts/Editor/SoftMaskEditor.cs | 9 ++-- .../Scripts/Editor/SoftMaskableEditor.cs | 41 +++++++++++++++---- .../SoftMaskForUGUI/Scripts/Editor/Utils.cs | 20 +++++++++ 3 files changed, 59 insertions(+), 11 deletions(-) create mode 100644 Assets/Coffee/UIExtensions/SoftMaskForUGUI/Scripts/Editor/Utils.cs diff --git a/Assets/Coffee/UIExtensions/SoftMaskForUGUI/Scripts/Editor/SoftMaskEditor.cs b/Assets/Coffee/UIExtensions/SoftMaskForUGUI/Scripts/Editor/SoftMaskEditor.cs index 64543ea..80a1c1c 100644 --- a/Assets/Coffee/UIExtensions/SoftMaskForUGUI/Scripts/Editor/SoftMaskEditor.cs +++ b/Assets/Coffee/UIExtensions/SoftMaskForUGUI/Scripts/Editor/SoftMaskEditor.cs @@ -1,4 +1,3 @@ -using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; @@ -34,7 +33,7 @@ namespace Coffee.UIExtensions.Editors if (0 < fixTargets.Count) { GUILayout.BeginHorizontal (); - EditorGUILayout.HelpBox ("There are child Graphicss that does not have a SoftMaskable component.\nAdd SoftMaskable component to them.", MessageType.Warning); + EditorGUILayout.HelpBox ("There are child Graphics that does not have a SoftMaskable component.\nAdd SoftMaskable component to them.", MessageType.Warning); GUILayout.BeginVertical (); if (GUILayout.Button ("Fix")) { @@ -42,6 +41,8 @@ namespace Coffee.UIExtensions.Editors { p.gameObject.AddComponent (); } + + Utils.MarkPrefabDirty (); } if (GUILayout.Button ("Ping")) { @@ -60,8 +61,8 @@ namespace Coffee.UIExtensions.Editors if (s_Preview) { var tex = current.softMaskBuffer; - var wtdth = tex.width * 64 / tex.height; - EditorGUI.DrawPreviewTexture (GUILayoutUtility.GetRect (wtdth, 64), tex, null, ScaleMode.ScaleToFit); + var width = tex.width * 64 / tex.height; + EditorGUI.DrawPreviewTexture (GUILayoutUtility.GetRect (width, 64), tex, null, ScaleMode.ScaleToFit); Repaint (); } GUILayout.FlexibleSpace (); diff --git a/Assets/Coffee/UIExtensions/SoftMaskForUGUI/Scripts/Editor/SoftMaskableEditor.cs b/Assets/Coffee/UIExtensions/SoftMaskForUGUI/Scripts/Editor/SoftMaskableEditor.cs index 085bf46..3f1fd41 100644 --- a/Assets/Coffee/UIExtensions/SoftMaskForUGUI/Scripts/Editor/SoftMaskableEditor.cs +++ b/Assets/Coffee/UIExtensions/SoftMaskForUGUI/Scripts/Editor/SoftMaskableEditor.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEditor; @@ -71,7 +71,7 @@ namespace Coffee.UIExtensions.Editors List _materialEditors = new List (); SerializedProperty _spMaskInteraction; - private void OnEnable () + void OnEnable () { _spMaskInteraction = serializedObject.FindProperty("m_MaskInteraction"); _custom = (maskInteraction == MaskInteraction.Custom); @@ -98,8 +98,8 @@ namespace Coffee.UIExtensions.Editors s_MaskWarning = new GUIContent(EditorGUIUtility.FindTexture("console.warnicon.sml"), "This component is not SoftMask. Use SoftMask instead of Mask."); } - - private void OnDisable () + + void OnDisable () { ClearMaterialEditors (); } @@ -201,15 +201,14 @@ namespace Coffee.UIExtensions.Editors - -// var current = target as SoftMaskable; var current = target as SoftMaskable; + current.GetComponentsInChildren (true, s_Graphics); var fixTargets = s_Graphics.Where (x => x.gameObject != current.gameObject && !x.GetComponent () && (!x.GetComponent () || x.GetComponent ().showMaskGraphic)).ToList (); if (0 < fixTargets.Count) { GUILayout.BeginHorizontal (); - EditorGUILayout.HelpBox ("There are child Graphicss that does not have a SoftMaskable component.\nAdd SoftMaskable component to them.", MessageType.Warning); + EditorGUILayout.HelpBox ("There are child Graphics that does not have a SoftMaskable component.\nAdd SoftMaskable component to them.", MessageType.Warning); GUILayout.BeginVertical (); if (GUILayout.Button ("Fix")) { @@ -236,6 +235,34 @@ namespace Coffee.UIExtensions.Editors ShowMaterialEditors (fontSharedMaterials, 1, fontSharedMaterials.Length - 1); } } + + if (!DetectMask (current.transform.parent)) + { + GUILayout.BeginHorizontal (); + EditorGUILayout.HelpBox ("This is unnecessary SoftMaskable.\nCan't find any SoftMask components above.", MessageType.Warning); + if (GUILayout.Button ("Remove", GUILayout.Height (40))) + { + DestroyImmediate (current); + + Utils.MarkPrefabDirty (); + } + GUILayout.EndHorizontal (); + } + } + + static bool DetectMask (Transform transform) + { + if (transform == null) + { + return false; + } + + if (transform.GetComponent () != null) + { + return true; + } + + return DetectMask (transform.parent); } void ClearMaterialEditors () diff --git a/Assets/Coffee/UIExtensions/SoftMaskForUGUI/Scripts/Editor/Utils.cs b/Assets/Coffee/UIExtensions/SoftMaskForUGUI/Scripts/Editor/Utils.cs new file mode 100644 index 0000000..6a3887b --- /dev/null +++ b/Assets/Coffee/UIExtensions/SoftMaskForUGUI/Scripts/Editor/Utils.cs @@ -0,0 +1,20 @@ +using UnityEditor.Experimental.SceneManagement; +using UnityEditor.SceneManagement; + + +namespace Coffee.UIExtensions.Editors +{ + public static class Utils + { + public static void MarkPrefabDirty () + { + #if UNITY_2018_3_OR_NEWER + var prefabStage = PrefabStageUtility.GetCurrentPrefabStage (); + if (prefabStage != null) + { + EditorSceneManager.MarkSceneDirty (prefabStage.scene); + } + #endif + } + } +} \ No newline at end of file