From a07c79a7fc8a340617d49b2919a0745eeca18e0b Mon Sep 17 00:00:00 2001 From: Jason Ma <1312119064@qq.com> Date: Sun, 9 Oct 2022 17:51:51 +0800 Subject: [PATCH] fix #8 --- Editor/DrawerUtility.cs | 156 +++++++++++++++++++++------------------- Editor/ShaderDrawer.cs | 4 +- package.json | 2 +- 3 files changed, 86 insertions(+), 76 deletions(-) diff --git a/Editor/DrawerUtility.cs b/Editor/DrawerUtility.cs index 9831b05..53ddc39 100644 --- a/Editor/DrawerUtility.cs +++ b/Editor/DrawerUtility.cs @@ -13,7 +13,7 @@ namespace LWGUI { /// when LwguiEventType.Init: get all metadata from drawer /// when LwguiEventType.Repaint: LWGUI decides how to draw each prop according to metadata - internal enum EventType + internal enum LwguiEventType { Init, Repaint @@ -37,7 +37,7 @@ internal class LWGUI : ShaderGUI public SearchMode searchMode = SearchMode.All; public SearchMode lastSearchMode = SearchMode.All; public bool updateSearchMode = false; - public EventType eventType = EventType.Init; + public LwguiEventType lwguiEventType = LwguiEventType.Init; public Shader shader; /// @@ -50,14 +50,14 @@ public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] pro this.props = props; this.materialEditor = materialEditor; this.shader = (materialEditor.target as Material).shader; - this.eventType = RevertableHelper.InitAndHasShaderChanged(shader) ? EventType.Init : EventType.Repaint; + this.lwguiEventType = RevertableHelper.InitAndHasShaderChanged(shader, materialEditor.target) ? LwguiEventType.Init : LwguiEventType.Repaint; // drawer register metadata - if (eventType == EventType.Init) + if (lwguiEventType == LwguiEventType.Init) { // reset all caches MetaDataHelper.ClearCaches(shader); - searchResult = MetaDataHelper.SearchProperties(shader, props, String.Empty, SearchMode.All); + searchResult = null; lastSearchingText = searchingText = string.Empty; lastSearchMode = searchMode = SearchMode.All; updateSearchMode = false; @@ -73,7 +73,7 @@ public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] pro } } // draw with metadata and searchingText - else if (eventType == EventType.Repaint) + else if (lwguiEventType == LwguiEventType.Repaint) { // Search Field if (searchResult == null) @@ -93,78 +93,75 @@ public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] pro updateSearchMode = false; } - // base.OnGUI(materialEditor, props); - { - // move fields left to make rect for Revert Button - materialEditor.SetDefaultGUIWidths(); - EditorGUIUtility.fieldWidth += RevertableHelper.revertButtonWidth; - EditorGUIUtility.labelWidth -= RevertableHelper.revertButtonWidth; - RevertableHelper.fieldWidth = EditorGUIUtility.fieldWidth; - RevertableHelper.labelWidth = EditorGUIUtility.labelWidth; + // move fields left to make rect for Revert Button + materialEditor.SetDefaultGUIWidths(); + EditorGUIUtility.fieldWidth += RevertableHelper.revertButtonWidth; + EditorGUIUtility.labelWidth -= RevertableHelper.revertButtonWidth; + RevertableHelper.fieldWidth = EditorGUIUtility.fieldWidth; + RevertableHelper.labelWidth = EditorGUIUtility.labelWidth; - foreach (var prop in props) + foreach (var prop in props) + { + if ((prop.flags & MaterialProperty.PropFlags.HideInInspector) == 0 && searchResult[prop.name]) { - if ((prop.flags & MaterialProperty.PropFlags.HideInInspector) == 0 && searchResult[prop.name]) + var height = materialEditor.GetPropertyHeight(prop, prop.displayName); + + // ignored when in Folding Group + if (height <= 0) continue; + + // helpbox + int lineCount; + var helpboxStr = MetaDataHelper.GetPropertyHelpbox(shader, prop, out lineCount); + if (!string.IsNullOrEmpty(helpboxStr)) { - var height = materialEditor.GetPropertyHeight(prop, prop.displayName); - - // ignored when in Folding Group - if (height <= 0) continue; - - // helpbox - int lineCount; - var helpboxStr = MetaDataHelper.GetPropertyHelpbox(shader, prop, out lineCount); - if (!string.IsNullOrEmpty(helpboxStr)) + var helpboxRect = EditorGUILayout.GetControlRect(true, 30f + Mathf.Max(0, lineCount - 2f) * helpboxSingleLineHeight); + if (MetaDataHelper.IsSubProperty(shader, prop)) { - var helpboxRect = EditorGUILayout.GetControlRect(true, 30f + Mathf.Max(0, lineCount - 2f) * helpboxSingleLineHeight); - if (MetaDataHelper.IsSubProperty(shader, prop)) - { - EditorGUI.indentLevel++; - helpboxRect = EditorGUI.IndentedRect(helpboxRect); - EditorGUI.indentLevel--; - } - helpboxRect.xMax -= RevertableHelper.revertButtonWidth; - EditorGUI.HelpBox(helpboxRect, helpboxStr, MessageType.Info); + EditorGUI.indentLevel++; + helpboxRect = EditorGUI.IndentedRect(helpboxRect); + EditorGUI.indentLevel--; } - - var rect = EditorGUILayout.GetControlRect(true, height, EditorStyles.layerMaskField); - var revertButtonRect = RevertableHelper.GetRevertButtonRect(prop, rect); - rect.xMax -= RevertableHelper.revertButtonWidth; - - // Process some builtin types display misplaced - switch (prop.type) - { - case MaterialProperty.PropType.Texture: - case MaterialProperty.PropType.Range: - materialEditor.SetDefaultGUIWidths(); - break; - default: - RevertableHelper.SetRevertableGUIWidths(); - break; - } - - RevertableHelper.DrawRevertableProperty(revertButtonRect, prop, materialEditor, shader); - var label = new GUIContent(prop.displayName, MetaDataHelper.GetPropertyTooltip(shader, prop)); - materialEditor.ShaderProperty(rect, prop, label); + helpboxRect.xMax -= RevertableHelper.revertButtonWidth; + EditorGUI.HelpBox(helpboxRect, helpboxStr, MessageType.Info); } - } - materialEditor.SetDefaultGUIWidths(); + + var rect = EditorGUILayout.GetControlRect(true, height, EditorStyles.layerMaskField); + var revertButtonRect = RevertableHelper.GetRevertButtonRect(prop, rect); + rect.xMax -= RevertableHelper.revertButtonWidth; - EditorGUILayout.Space(); - EditorGUILayout.Space(); - if (SupportedRenderingFeatures.active.editableMaterialRenderQueue) - { - materialEditor.RenderQueueField(); + // Process some builtin types display misplaced + switch (prop.type) + { + case MaterialProperty.PropType.Texture: + case MaterialProperty.PropType.Range: + materialEditor.SetDefaultGUIWidths(); + break; + default: + RevertableHelper.SetRevertableGUIWidths(); + break; + } + + RevertableHelper.DrawRevertableProperty(revertButtonRect, prop, materialEditor, shader); + var label = new GUIContent(prop.displayName, MetaDataHelper.GetPropertyTooltip(shader, prop)); + materialEditor.ShaderProperty(rect, prop, label); } - materialEditor.EnableInstancingField(); - materialEditor.DoubleSidedGIField(); } - - - // LWGUI logo - EditorGUILayout.Space(); - Helper.DrawLogo(); } + + materialEditor.SetDefaultGUIWidths(); + + EditorGUILayout.Space(); + EditorGUILayout.Space(); + if (SupportedRenderingFeatures.active.editableMaterialRenderQueue) + { + materialEditor.RenderQueueField(); + } + materialEditor.EnableInstancingField(); + materialEditor.DoubleSidedGIField(); + + // LWGUI logo + EditorGUILayout.Space(); + Helper.DrawLogo(); } /// @@ -302,8 +299,9 @@ internal class RevertableHelper private static Dictionary> _defaultProps = new Dictionary>(); - private static Dictionary _initTimers = new Dictionary(); - private const int INIT_PER_FRAMES = 0; + private static Dictionary _initTimers = new Dictionary(); + private static Dictionary _lastShaders = new Dictionary(); + private const int INIT_PER_FRAMES = 1; #region Init @@ -320,26 +318,38 @@ private static void CheckProperty(Shader shader, MaterialProperty prop) /// /// Detect Shader changes to know when to initialize /// - public static bool InitAndHasShaderChanged(Shader shader) + public static bool InitAndHasShaderChanged(Shader shader, Object material = null) { bool equals = true; + + if (material != null && !_lastShaders.ContainsKey(material)) + _lastShaders.Add(material, null); // Init every few frames if (_initTimers.ContainsKey(shader)) _initTimers[shader]++; else _initTimers[shader] = 0; - + // fast refresh can better capture the shadeer modification - if (_initTimers[shader] > INIT_PER_FRAMES + if (_initTimers[shader] >= INIT_PER_FRAMES || !_defaultProps.ContainsKey(shader) || ShaderUtil.GetPropertyCount(shader) != _defaultProps[shader].Count + || (material != null && _lastShaders[material] != shader) ) { + if (material != null) + { + if (_lastShaders[material] != shader) equals = false; + _lastShaders[material] = shader; + } _initTimers[shader] = 0; } else + { + if (material != null) _lastShaders[material] = shader; return false; + } // Get and cache new props var newProps = MaterialEditor.GetMaterialProperties(new[] { new Material(shader) }); diff --git a/Editor/ShaderDrawer.cs b/Editor/ShaderDrawer.cs index 6aa4ace..ce09cf2 100644 --- a/Editor/ShaderDrawer.cs +++ b/Editor/ShaderDrawer.cs @@ -49,7 +49,7 @@ public override void OnGUI(Rect position, MaterialProperty prop, GUIContent labe var lwgui = Helper.GetLWGUI(editor); var toggleValue = prop.floatValue > 0; - if (lwgui.eventType == EventType.Init) + if (lwgui.lwguiEventType == LwguiEventType.Init) { MetaDataHelper.RegisterMainProp(lwgui.shader, prop, _group); MetaDataHelper.RegisterPropertyDefaultValueText(lwgui.shader, prop, @@ -131,7 +131,7 @@ public override void OnGUI(Rect position, MaterialProperty prop, GUIContent labe props = lwgui.props; shader = lwgui.shader; - if (lwgui.eventType == EventType.Init) + if (lwgui.lwguiEventType == LwguiEventType.Init) { Init(position, prop, label, editor); return; diff --git a/package.json b/package.json index 5d9bd16..30d087f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "com.jasonma.lwgui", - "version": "1.2.5", + "version": "1.2.6", "displayName": "LWGUI", "description": "A Lightweight, Flexible, Powerful Shader GUI System for Unity.", "keywords": [