From 2f5ceb60107bac70dac973d3fb224031a37902d6 Mon Sep 17 00:00:00 2001 From: Jason Ma <1312119064@qq.com> Date: Fri, 13 Sep 2024 16:01:36 +0800 Subject: [PATCH] Add ApplyMaterialPropertyAndDecoratorDrawers() to fix PassSwitch() not working when editing materials in code --- Editor/Helper/MetaDataHelper.cs | 2 +- Editor/Helper/PresetHelper.cs | 8 +-- .../ScriptableObject/ShaderPropertyPreset.cs | 2 +- .../LwguiGradientEditor.cs | 2 +- UnityEditorExtension/UnityEditorExtension.cs | 56 +++++++++++++++++++ .../UnityEditorExtension.cs.meta | 3 + package.json | 2 +- 7 files changed, 66 insertions(+), 9 deletions(-) create mode 100644 UnityEditorExtension/UnityEditorExtension.cs create mode 100644 UnityEditorExtension/UnityEditorExtension.cs.meta diff --git a/Editor/Helper/MetaDataHelper.cs b/Editor/Helper/MetaDataHelper.cs index cecdf63..cac4f91 100644 --- a/Editor/Helper/MetaDataHelper.cs +++ b/Editor/Helper/MetaDataHelper.cs @@ -52,7 +52,7 @@ public class LWGUIMetaDatas public void OnValidate() { - MaterialEditor.ApplyMaterialPropertyDrawers(GetMaterialEditor()?.targets); + UnityEditorExtension.ApplyMaterialPropertyAndDecoratorDrawers(GetMaterialEditor()?.targets); MetaDataHelper.ForceUpdateMaterialsMetadataCache(GetMaterialEditor()?.targets); } } diff --git a/Editor/Helper/PresetHelper.cs b/Editor/Helper/PresetHelper.cs index 001c94a..779f65c 100644 --- a/Editor/Helper/PresetHelper.cs +++ b/Editor/Helper/PresetHelper.cs @@ -60,11 +60,10 @@ public static ShaderPropertyPreset GetPresetFile(string presetFileName) // For Developers: Call this after a material has modified in code public static void ApplyPresetsInMaterial(Material material) { - var props = MaterialEditor.GetMaterialProperties(new[] { material }); + var props = MaterialEditor.GetMaterialProperties(new UnityEngine.Object[] { material }); foreach (var prop in props) { - List decoratorDrawers; - var drawer = ReflectionHelper.GetPropertyDrawer(material.shader, prop, out decoratorDrawers); + var drawer = ReflectionHelper.GetPropertyDrawer(material.shader, prop, out _); // Apply active preset if (drawer != null && drawer is IBasePresetDrawer) @@ -73,9 +72,8 @@ public static void ApplyPresetsInMaterial(Material material) if (activePreset != null) activePreset.ApplyToDefaultMaterial(material); } - } - MaterialEditor.ApplyMaterialPropertyDrawers(material); + UnityEditorExtension.ApplyMaterialPropertyAndDecoratorDrawers(material); } } } \ No newline at end of file diff --git a/Editor/ScriptableObject/ShaderPropertyPreset.cs b/Editor/ScriptableObject/ShaderPropertyPreset.cs index 938373a..9e97417 100644 --- a/Editor/ScriptableObject/ShaderPropertyPreset.cs +++ b/Editor/ScriptableObject/ShaderPropertyPreset.cs @@ -75,7 +75,7 @@ public void Apply(Material material, bool isDefaultMaterial, PerMaterialData per break; } - MaterialEditor.ApplyMaterialPropertyDrawers(material); + UnityEditorExtension.ApplyMaterialPropertyAndDecoratorDrawers(material); } // is Property Primary Material else if (perMaterialData != null) diff --git a/UnityEditorExtension/LwguiGradientEditor/LwguiGradientEditor.cs b/UnityEditorExtension/LwguiGradientEditor/LwguiGradientEditor.cs index 9395a4d..fdb5377 100644 --- a/UnityEditorExtension/LwguiGradientEditor/LwguiGradientEditor.cs +++ b/UnityEditorExtension/LwguiGradientEditor/LwguiGradientEditor.cs @@ -602,7 +602,7 @@ Keyframe CheckNewKeyTime(CurveWrapper cw, Keyframe newKey, float oldKeyTime = 0) else newKey.time -= 0.00001f; } - catch (InvalidOperationException ex) { } + catch (InvalidOperationException) { } return newKey; } diff --git a/UnityEditorExtension/UnityEditorExtension.cs b/UnityEditorExtension/UnityEditorExtension.cs new file mode 100644 index 0000000..899c36c --- /dev/null +++ b/UnityEditorExtension/UnityEditorExtension.cs @@ -0,0 +1,56 @@ +// Copyright (c) Jason Ma + +using UnityEditor; +using UnityEngine; +using Object = UnityEngine.Object; + +namespace LWGUI +{ + public static class UnityEditorExtension + { + + #region MaterialEditor + + // For Developers: Call this after a material has modified in code + public static void ApplyMaterialPropertyAndDecoratorDrawers(Material material) + { + var objs = new Object[] { material }; + ApplyMaterialPropertyAndDecoratorDrawers(objs); + } + + public static void ApplyMaterialPropertyAndDecoratorDrawers(Object[] targets) + { + if (!EditorMaterialUtility.disableApplyMaterialPropertyDrawers) + { + if (targets == null || targets.Length == 0) + return; + var target = targets[0] as Material; + if (target == null) + return; + + var shader = target.shader; + string[] propNames = MaterialEditor.GetMaterialPropertyNames(targets); + for (int i = 0; i < propNames.Length; i++) + { + var prop = MaterialEditor.GetMaterialProperty(targets, i); + var drawer = ReflectionHelper.GetPropertyDrawer(shader, prop, out var decoratorDrawers); + + if (drawer != null) + { + drawer.Apply(prop); + } + if (decoratorDrawers != null) + { + foreach (var decoratorDrawer in decoratorDrawers) + { + decoratorDrawer.Apply(prop); + } + } + } + } + } + + #endregion + + } +} \ No newline at end of file diff --git a/UnityEditorExtension/UnityEditorExtension.cs.meta b/UnityEditorExtension/UnityEditorExtension.cs.meta new file mode 100644 index 0000000..1e7d5b1 --- /dev/null +++ b/UnityEditorExtension/UnityEditorExtension.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: c461331302de45948d270b0842238473 +timeCreated: 1726212440 \ No newline at end of file diff --git a/package.json b/package.json index 2038cff..c193c37 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "com.jasonma.lwgui", - "version": "1.18.7", + "version": "1.18.8", "displayName": "LWGUI", "description": "A Lightweight, Flexible, Powerful Shader GUI System for Unity.", "keywords": [