From bcc01b0bb4a29ed702761ca7d017db5f86c33dfe Mon Sep 17 00:00:00 2001 From: Jason Ma <1312119064@qq.com> Date: Thu, 12 Dec 2024 11:55:15 +0800 Subject: [PATCH 1/2] Add: Presets now support enable/disable Passes Add: Main() and SubToggle() now support switch preset by value. Change: Preset() filtered out by ShowIf() will not set default values --- Editor/Helper/Helper.cs | 9 +- Editor/Helper/PresetHelper.cs | 3 + Editor/MetaData/PerMaterialData.cs | 23 +- .../ScriptableObject/ShaderPropertyPreset.cs | 18 +- Editor/ShaderDrawer.cs | 271 +++++++++++------- README.md | 38 +-- README_CN.md | 49 ++-- ...set.asset => LWGUI_Preset_BlendMode.asset} | 2 +- ...meta => LWGUI_Preset_BlendMode.asset.meta} | 0 Test/LWGUI_Preset_Toggle.asset | 29 ++ Test/LWGUI_Preset_Toggle.asset.meta | 8 + Test/SampleDrawer 1.shader | 1 + Test/SampleDrawer.shader | 2 +- package.json | 2 +- 14 files changed, 294 insertions(+), 161 deletions(-) rename Test/{LWGUI_BlendModePreset.asset => LWGUI_Preset_BlendMode.asset} (98%) rename Test/{LWGUI_BlendModePreset.asset.meta => LWGUI_Preset_BlendMode.asset.meta} (100%) create mode 100644 Test/LWGUI_Preset_Toggle.asset create mode 100644 Test/LWGUI_Preset_Toggle.asset.meta diff --git a/Editor/Helper/Helper.cs b/Editor/Helper/Helper.cs index e379b6b..97266c6 100644 --- a/Editor/Helper/Helper.cs +++ b/Editor/Helper/Helper.cs @@ -899,11 +899,16 @@ public static void DoPropertyContextMenus(Rect rect, MaterialProperty prop, LWGU menus.AddSeparator(""); foreach (var activePresetData in perMaterialData.activePresetDatas) { + // Cull self if (activePresetData.property == prop) continue; var activePreset = activePresetData.preset; - var presetAsset = perShaderData.propStaticDatas[activePresetData.property.name].propertyPresetAsset; - var presetPropDisplayName = perShaderData.propStaticDatas[activePresetData.property.name].displayName; + var (presetPropStaticData, presetPropDynamicData) = metaDatas.GetPropDatas(activePresetData.property); + var presetAsset = presetPropStaticData.propertyPresetAsset; + var presetPropDisplayName = presetPropStaticData.displayName; + + // Cull invisible presets + if (!presetPropDynamicData.isShowing) continue; if (activePreset.GetPropertyValue(prop.name) != null) { diff --git a/Editor/Helper/PresetHelper.cs b/Editor/Helper/PresetHelper.cs index 779f65c..68d79c2 100644 --- a/Editor/Helper/PresetHelper.cs +++ b/Editor/Helper/PresetHelper.cs @@ -45,6 +45,9 @@ public static void AddPreset(ShaderPropertyPreset preset) public static ShaderPropertyPreset GetPresetFile(string presetFileName) { + if (string.IsNullOrEmpty(presetFileName)) + return null; + if (!_loadedPresets.ContainsKey(presetFileName) || !_loadedPresets[presetFileName]) ForceInit(); diff --git a/Editor/MetaData/PerMaterialData.cs b/Editor/MetaData/PerMaterialData.cs index 881fe69..c8d5ca0 100644 --- a/Editor/MetaData/PerMaterialData.cs +++ b/Editor/MetaData/PerMaterialData.cs @@ -62,10 +62,15 @@ public void Init(Shader shader, Material material, MaterialProperty[] props, Per // Get active presets foreach (var prop in props) { - var activePreset = perShaderData.propStaticDatas[prop.name].presetDrawer - ?.GetActivePreset(prop, perShaderData.propStaticDatas[prop.name].propertyPresetAsset); - if (activePreset != null) + var propStaticData = perShaderData.propStaticDatas[prop.name]; + var activePreset = propStaticData.presetDrawer?.GetActivePreset(prop, propStaticData.propertyPresetAsset); + if (activePreset != null + // Filter invisible preset properties + && (propStaticData.showIfDatas.Count == 0 + || ShowIfDecorator.GetShowIfResultFromMaterial(propStaticData.showIfDatas, this.material))) + { activePresetDatas.Add(new PersetDynamicData(activePreset, prop)); + } } { @@ -85,16 +90,6 @@ public void Init(Shader shader, Material material, MaterialProperty[] props, Per var defaultProperties = MaterialEditor.GetMaterialProperties(new[] { defaultMaterial }); Debug.Assert(defaultProperties.Length == props.Length); - // Override default value - for (int i = 0; i < props.Length; i++) - { - Debug.Assert(props[i].name == defaultProperties[i].name); - Debug.Assert(!propDynamicDatas.ContainsKey(props[i].name)); - - perShaderData.propStaticDatas[props[i].name].baseDrawers - ?.ForEach(baseDrawer => baseDrawer.OverrideDefaultValue(shader, props[i], defaultProperties[i], perShaderData)); - } - // Init propDynamicDatas for (int i = 0; i < props.Length; i++) { @@ -132,7 +127,7 @@ public void Init(Shader shader, Material material, MaterialProperty[] props, Per } } - // Store Show Modified Props Only Cache + // Store "Show Modified Props Only" Caches { if (perShaderData.displayModeData.showOnlyModifiedGroups || perShaderData.displayModeData.showOnlyModifiedProperties) { diff --git a/Editor/ScriptableObject/ShaderPropertyPreset.cs b/Editor/ScriptableObject/ShaderPropertyPreset.cs index b5ec959..90da58a 100644 --- a/Editor/ScriptableObject/ShaderPropertyPreset.cs +++ b/Editor/ScriptableObject/ShaderPropertyPreset.cs @@ -1,6 +1,7 @@ // Copyright (c) Jason Ma using System; using System.Collections.Generic; +using System.Linq; using UnityEngine; using UnityEditor; using Object = UnityEngine.Object; @@ -145,6 +146,8 @@ public class Preset public List propertyValues = new List(); public List enabledKeywords = new List(); public List disabledKeywords = new List(); + public List enabledPasses = new List(); + public List disabledPasses = new List(); public int renderQueue = -1; @@ -156,11 +159,15 @@ public void ApplyToDefaultMaterial(Material material) material.EnableKeyword(enabledKeyword); foreach (var disabledKeyword in disabledKeywords) material.DisableKeyword(disabledKeyword); + + Helper.SetShaderPassEnabled(new Object[] { material }, enabledPasses.Select(s => s.ToUpper()).ToArray(), true); + Helper.SetShaderPassEnabled(new Object[] { material }, disabledPasses.Select(s => s.ToUpper()).ToArray(), false); + if (renderQueue >= 0) material.renderQueue = renderQueue; } - public void ApplyToEditingMaterial(UnityEngine.Object[] materials, PerMaterialData perMaterialData) + public void ApplyToEditingMaterial(Object[] materials, PerMaterialData perMaterialData) { for (int i = 0; i < materials.Length; i++) { @@ -171,12 +178,16 @@ public void ApplyToEditingMaterial(UnityEngine.Object[] materials, PerMaterialDa material.EnableKeyword(enabledKeyword); foreach (var disabledKeyword in disabledKeywords) material.DisableKeyword(disabledKeyword); + if (renderQueue >= 0) material.renderQueue = renderQueue; } + + Helper.SetShaderPassEnabled(materials, enabledPasses.Select(s => s.ToUpper()).ToArray(), true); + Helper.SetShaderPassEnabled(materials, disabledPasses.Select(s => s.ToUpper()).ToArray(), false); } - public void ApplyKeywordsToMaterials(UnityEngine.Object[] materials) + public void ApplyKeywordsAndPassesToMaterials(Object[] materials) { for (int i = 0; i < materials.Length; i++) { @@ -186,6 +197,9 @@ public void ApplyKeywordsToMaterials(UnityEngine.Object[] materials) foreach (var disabledKeyword in disabledKeywords) material.DisableKeyword(disabledKeyword); } + + Helper.SetShaderPassEnabled(materials, enabledPasses.Select(s => s.ToUpper()).ToArray(), true); + Helper.SetShaderPassEnabled(materials, disabledPasses.Select(s => s.ToUpper()).ToArray(), false); } public PropertyValue GetPropertyValue(string propName) diff --git a/Editor/ShaderDrawer.cs b/Editor/ShaderDrawer.cs index 81d53a2..a8e6039 100644 --- a/Editor/ShaderDrawer.cs +++ b/Editor/ShaderDrawer.cs @@ -18,9 +18,6 @@ public interface IBaseDrawer void BuildStaticMetaData(Shader inShader, MaterialProperty inProp, MaterialProperty[] inProps, PropertyStaticData inoutPropertyStaticData){} void GetDefaultValueDescription(Shader inShader, MaterialProperty inProp, MaterialProperty inDefaultProp, PerShaderData inPerShaderData, PerMaterialData inoutPerMaterialData){} - - void OverrideDefaultValue(Shader inShader, MaterialProperty inProp, MaterialProperty inDefaultProp, PerShaderData inPerShaderData){} - } public interface IBasePresetDrawer @@ -41,22 +38,25 @@ public partial class PropertyStaticData /// /// Create a Folding Group - /// group:group name (Default: Property Name) - /// keyword:keyword used for toggle, "_" = ignore, none or "__" = Property Name + "_ON", always Upper (Default: none) + /// group: group name (Default: Property Name) + /// keyword: keyword used for toggle, "_" = ignore, none or "__" = Property Name + "_ON", always Upper (Default: none) /// default Folding State: "on" or "off" (Default: off) /// default Toggle Displayed: "on" or "off" (Default: on) + /// preset File Name: "Shader Property Preset" asset name, see Preset() for detail (Default: none) /// Target Property Type: FLoat, express Toggle value /// - public class MainDrawer : MaterialPropertyDrawer, IBaseDrawer + public class MainDrawer : MaterialPropertyDrawer, IBaseDrawer, IBasePresetDrawer { protected LWGUIMetaDatas metaDatas; + private static readonly float _height = 28f; + private bool _isFolding; private string _group; private string _keyword; private bool _defaultFoldingState; private bool _defaultToggleDisplayed; - private static readonly float _height = 28f; + private string _presetFileName; public MainDrawer() : this(String.Empty) { } @@ -65,13 +65,16 @@ public MainDrawer(string group) : this(group, String.Empty) { } public MainDrawer(string group, string keyword) : this(group, keyword, "off") { } public MainDrawer(string group, string keyword, string defaultFoldingState) : this(group, keyword, defaultFoldingState, "on") { } + + public MainDrawer(string group, string keyword, string defaultFoldingState, string defaultToggleDisplayed) : this(group, keyword, defaultFoldingState, defaultToggleDisplayed, String.Empty) { } - public MainDrawer(string group, string keyword, string defaultFoldingState, string defaultToggleDisplayed) + public MainDrawer(string group, string keyword, string defaultFoldingState, string defaultToggleDisplayed, string presetFileName) { this._group = group; this._keyword = keyword; this._defaultFoldingState = defaultFoldingState.ToLower() == "on"; this._defaultToggleDisplayed = defaultToggleDisplayed.ToLower() == "on"; + this._presetFileName = presetFileName; } public virtual void BuildStaticMetaData(Shader inShader, MaterialProperty inProp, MaterialProperty[] inProps, PropertyStaticData inoutPropertyStaticData) @@ -80,6 +83,7 @@ public virtual void BuildStaticMetaData(Shader inShader, MaterialProperty inProp inoutPropertyStaticData.isMain = true; inoutPropertyStaticData.isExpanding = _defaultFoldingState; PerShaderData.DecodeMetaDataFromDisplayName(inProp, inoutPropertyStaticData); + PresetDrawer.SetPresetAssetToStaticData(inoutPropertyStaticData, _presetFileName); } public virtual void GetDefaultValueDescription(Shader inShader, MaterialProperty inProp, MaterialProperty inDefaultProp, PerShaderData inPerShaderData, PerMaterialData inoutPerMaterialData) @@ -87,6 +91,9 @@ public virtual void GetDefaultValueDescription(Shader inShader, MaterialProperty inoutPerMaterialData.propDynamicDatas[inProp.name].defaultValueDescription = inDefaultProp.floatValue > 0 ? "On" : "Off"; } + public ShaderPropertyPreset.Preset GetActivePreset(MaterialProperty inProp, ShaderPropertyPreset shaderPropertyPreset) => + PresetDrawer.GetActivePresetFromFloatProperty(inProp, shaderPropertyPreset); + public override void OnGUI(Rect position, MaterialProperty prop, GUIContent label, MaterialEditor editor) { metaDatas = Helper.GetLWGUIMetadatas(editor); @@ -101,6 +108,7 @@ public override void OnGUI(Rect position, MaterialProperty prop, GUIContent labe { prop.floatValue = toggleResult ? 1.0f : 0.0f; Helper.SetShaderKeyWord(editor.targets, Helper.GetKeyWord(_keyword, prop.name), toggleResult); + PresetHelper.GetPresetFile(_presetFileName)?.presets[(int)prop.floatValue].ApplyToEditingMaterial(prop.targets, metaDatas.perMaterialData); } EditorGUI.showMixedValue = showMixedValue; } @@ -115,17 +123,14 @@ public override float GetPropertyHeight(MaterialProperty prop, string label, Mat public override void Apply(MaterialProperty prop) { base.Apply(prop); - if (!prop.hasMixedValue - && (prop.type == MaterialProperty.PropType.Float - || prop.type == MaterialProperty.PropType.Int - )) - Helper.SetShaderKeyWord(prop.targets, Helper.GetKeyWord(_keyword, prop.name), prop.floatValue > 0f); + Helper.SetShaderKeyWord(prop.targets, Helper.GetKeyWord(_keyword, prop.name), prop.floatValue > 0f); + PresetDrawer.ApplyPreset(_presetFileName, prop); } } /// /// Draw a property with default style in the folding group - /// group:father group name, support suffix keyword for conditional display (Default: none) + /// group: father group name, support suffix keyword for conditional display (Default: none) /// Target Property Type: Any /// public class SubDrawer : MaterialPropertyDrawer, IBaseDrawer @@ -156,8 +161,6 @@ public virtual void BuildStaticMetaData(Shader inShader, MaterialProperty inProp public virtual void GetDefaultValueDescription(Shader inShader, MaterialProperty inProp, MaterialProperty inDefaultProp, PerShaderData inPerShaderData, PerMaterialData inoutPerMaterialData) { } - public virtual void OverrideDefaultValue(Shader inShader, MaterialProperty inProp, MaterialProperty inDefaultProp, PerShaderData inPerShaderData) { } - public override void OnGUI(Rect position, MaterialProperty prop, GUIContent label, MaterialEditor editor) { metaDatas = Helper.GetLWGUIMetadatas(editor); @@ -188,30 +191,44 @@ public virtual void DrawProp(Rect position, MaterialProperty prop, GUIContent la /// /// Similar to builtin Toggle() - /// group:father group name, support suffix keyword for conditional display (Default: none) - /// keyword:keyword used for toggle, "_" = ignore, none or "__" = Property Name + "_ON", always Upper (Default: none) + /// group: father group name, support suffix keyword for conditional display (Default: none) + /// keyword: keyword used for toggle, "_" = ignore, none or "__" = Property Name + "_ON", always Upper (Default: none) + /// preset File Name: "Shader Property Preset" asset name, see Preset() for detail (Default: none) /// Target Property Type: FLoat /// - public class SubToggleDrawer : SubDrawer + public class SubToggleDrawer : SubDrawer, IBasePresetDrawer { - private string _keyWord = String.Empty; + private string _keyWord = String.Empty; + private string _presetFileName = String.Empty; public SubToggleDrawer() { } - public SubToggleDrawer(string group) : this(group, String.Empty) { } + public SubToggleDrawer(string group) : this(group, String.Empty, String.Empty) { } + + public SubToggleDrawer(string group, string keyWord) : this(group, keyWord, String.Empty) { } - public SubToggleDrawer(string group, string keyWord) + public SubToggleDrawer(string group, string keyWord, string presetFileName) { this.group = group; this._keyWord = keyWord; + this._presetFileName = presetFileName; } protected override bool IsMatchPropType(MaterialProperty property) { return property.type == MaterialProperty.PropType.Float; } + public override void BuildStaticMetaData(Shader inShader, MaterialProperty inProp, MaterialProperty[] inProps, PropertyStaticData inoutPropertyStaticData) + { + base.BuildStaticMetaData(inShader, inProp, inProps, inoutPropertyStaticData); + PresetDrawer.SetPresetAssetToStaticData(inoutPropertyStaticData, _presetFileName); + } + public override void GetDefaultValueDescription(Shader inShader, MaterialProperty inProp, MaterialProperty inDefaultProp, PerShaderData inPerShaderData, PerMaterialData inoutPerMaterialData) { inoutPerMaterialData.propDynamicDatas[inProp.name].defaultValueDescription = inDefaultProp.floatValue > 0 ? "On" : "Off"; } + + public ShaderPropertyPreset.Preset GetActivePreset(MaterialProperty inProp, ShaderPropertyPreset shaderPropertyPreset) => + PresetDrawer.GetActivePresetFromFloatProperty(inProp, shaderPropertyPreset); public override void DrawProp(Rect position, MaterialProperty prop, GUIContent label, MaterialEditor editor) { @@ -223,6 +240,7 @@ public override void DrawProp(Rect position, MaterialProperty prop, GUIContent l { prop.floatValue = value ? 1.0f : 0.0f; Helper.SetShaderKeyWord(editor.targets, k, value); + PresetHelper.GetPresetFile(_presetFileName)?.presets[(int)prop.floatValue].ApplyToEditingMaterial(prop.targets, metaDatas.perMaterialData); } EditorGUI.showMixedValue = false; } @@ -230,14 +248,14 @@ public override void DrawProp(Rect position, MaterialProperty prop, GUIContent l public override void Apply(MaterialProperty prop) { base.Apply(prop); - if (!prop.hasMixedValue && IsMatchPropType(prop)) - Helper.SetShaderKeyWord(prop.targets, Helper.GetKeyWord(_keyWord, prop.name), prop.floatValue > 0f); + Helper.SetShaderKeyWord(prop.targets, Helper.GetKeyWord(_keyWord, prop.name), prop.floatValue > 0f); + PresetDrawer.ApplyPreset(_presetFileName, prop); } } /// /// Similar to builtin PowerSlider() - /// group:father group name, support suffix keyword for conditional display (Default: none) + /// group: father group name, support suffix keyword for conditional display (Default: none) /// power: power of slider (Default: 1) /// Target Property Type: Range /// @@ -267,7 +285,7 @@ public override void DrawProp(Rect position, MaterialProperty prop, GUIContent l /// /// Similar to builtin IntRange() - /// group:father group name, support suffix keyword for conditional display (Default: none) + /// group: father group name, support suffix keyword for conditional display (Default: none) /// Target Property Type: Range /// public class SubIntRangeDrawer : SubDrawer @@ -306,7 +324,7 @@ public override void DrawProp(Rect position, MaterialProperty prop, GUIContent l /// /// Draw a min max slider - /// group:father group name, support suffix keyword for conditional display (Default: none) + /// group: father group name, support suffix keyword for conditional display (Default: none) /// minPropName: Output Min Property Name /// maxPropName: Output Max Property Name /// Target Property Type: Range, range limits express the MinMaxSlider value range @@ -418,7 +436,7 @@ public override void DrawProp(Rect position, MaterialProperty prop, GUIContent l /// /// Similar to builtin Enum() / KeywordEnum() - /// group:father group name, support suffix keyword for conditional display (Default: none) + /// group: father group name, support suffix keyword for conditional display (Default: none) /// n(s): display name /// k(s): keyword /// v(s): value @@ -543,8 +561,7 @@ public override void DrawProp(Rect position, MaterialProperty prop, GUIContent l public override void Apply(MaterialProperty prop) { base.Apply(prop); - if (!prop.hasMixedValue && IsMatchPropType(prop)) - Helper.SetShaderKeyWord(prop.targets, GetKeywords(prop), (int)prop.floatValue); + Helper.SetShaderKeyWord(prop.targets, GetKeywords(prop), (int)prop.floatValue); } } @@ -626,7 +643,7 @@ public SubKeywordEnumDrawer(string group, string kw1, string kw2, string kw3, st /// /// Draw a Texture property in single line with a extra property - /// group:father group name, support suffix keyword for conditional display (Default: none) + /// group: father group name, support suffix keyword for conditional display (Default: none) /// extraPropName: extra property name (Default: none) /// Target Property Type: Texture /// Extra Property Type: Color, Vector @@ -708,7 +725,7 @@ public override void DrawProp(Rect position, MaterialProperty prop, GUIContent l /// /// Draw an image preview. /// display name: The path of the image file relative to the Unity project, such as: "Assets/test.png", "Doc/test.png", "../test.png" - /// group:father group name, support suffix keyword for conditional display (Default: none) + /// group: father group name, support suffix keyword for conditional display (Default: none) /// public class ImageDrawer : SubDrawer { @@ -763,7 +780,7 @@ public override void DrawProp(Rect position, MaterialProperty prop, GUIContent l /// /// Display up to 4 colors in a single line - /// group:father group name, support suffix keyword for conditional display (Default: none) + /// group: father group name, support suffix keyword for conditional display (Default: none) /// color2-4: extra color property name /// Target Property Type: Color /// @@ -848,7 +865,7 @@ public override void DrawProp(Rect position, MaterialProperty prop, GUIContent l /// RGB Average = (1f / 3f, 1f / 3f, 1f / 3f, 0) /// RGB Luminance = (0.2126f, 0.7152f, 0.0722f, 0) /// None = (0, 0, 0, 0) - /// group:father group name, support suffix keyword for conditional display (Default: none) + /// group: father group name, support suffix keyword for conditional display (Default: none) /// Target Property Type: Vector, used to dot() with Texture Sample Value /// public class ChannelDrawer : SubDrawer @@ -1118,7 +1135,7 @@ void OnSwitchRampMapEvent(Texture2D newRampMap) /// /// Popping a menu, you can select the Shader Property Preset, the Preset values will replaces the default values - /// group:father group name, support suffix keyword for conditional display (Default: none) + /// group: father group name, support suffix keyword for conditional display (Default: none) /// presetFileName: "Shader Property Preset" asset name, you can create new Preset by /// "Right Click > Create > LWGUI > Shader Property Preset" in Project window, /// *any Preset in the entire project cannot have the same name* @@ -1135,12 +1152,40 @@ public PresetDrawer(string group, string presetFileName) this.presetFileName = presetFileName; } + public static void SetPresetAssetToStaticData(PropertyStaticData inoutPropertyStaticData, string presetFileName) + { + inoutPropertyStaticData.propertyPresetAsset = PresetHelper.GetPresetFile(presetFileName); + } + + public static ShaderPropertyPreset.Preset GetActivePresetFromFloatProperty(MaterialProperty inProp, ShaderPropertyPreset shaderPropertyPreset) + { + ShaderPropertyPreset.Preset preset = null; + var index = (int)inProp.floatValue; + if (shaderPropertyPreset && index >= 0 && index < shaderPropertyPreset.presets.Count) + { + preset = shaderPropertyPreset.presets[index]; + } + return preset; + } + + public static void ApplyPreset(string presetFileName, MaterialProperty prop) + { + var presetFile = PresetHelper.GetPresetFile(presetFileName); + if (presetFile != null + && prop.floatValue < presetFile.presets.Count + && ShowIfDecorator.GetShowIfResultToFilterDrawerApplying(prop) + ) + { + presetFile.presets[(int)prop.floatValue].ApplyKeywordsAndPassesToMaterials(prop.targets); + } + } + protected override bool IsMatchPropType(MaterialProperty property) { return property.type == MaterialProperty.PropType.Float; } public override void BuildStaticMetaData(Shader inShader, MaterialProperty inProp, MaterialProperty[] inProps, PropertyStaticData inoutPropertyStaticData) { base.BuildStaticMetaData(inShader, inProp, inProps, inoutPropertyStaticData); - inoutPropertyStaticData.propertyPresetAsset = PresetHelper.GetPresetFile(presetFileName); + SetPresetAssetToStaticData(inoutPropertyStaticData, presetFileName); } public override void GetDefaultValueDescription(Shader inShader, MaterialProperty inProp, MaterialProperty inDefaultProp, PerShaderData inPerShaderData, PerMaterialData inoutPerMaterialData) @@ -1152,16 +1197,8 @@ public override void GetDefaultValueDescription(Shader inShader, MaterialPropert inoutPerMaterialData.propDynamicDatas[inProp.name].defaultValueDescription = propertyPreset.presets[index].presetName; } - public ShaderPropertyPreset.Preset GetActivePreset(MaterialProperty inProp, ShaderPropertyPreset shaderPropertyPreset) - { - ShaderPropertyPreset.Preset preset = null; - var index = (int)inProp.floatValue; - if (shaderPropertyPreset && index >= 0 && index < shaderPropertyPreset.presets.Count) - { - preset = shaderPropertyPreset.presets[index]; - } - return preset; - } + public ShaderPropertyPreset.Preset GetActivePreset(MaterialProperty inProp, ShaderPropertyPreset shaderPropertyPreset) => + GetActivePresetFromFloatProperty(inProp, shaderPropertyPreset); public override void DrawProp(Rect position, MaterialProperty prop, GUIContent label, MaterialEditor editor) { @@ -1199,9 +1236,7 @@ public override void DrawProp(Rect position, MaterialProperty prop, GUIContent l public override void Apply(MaterialProperty prop) { base.Apply(prop); - var presetFile = PresetHelper.GetPresetFile(presetFileName); - if (presetFile != null && prop.floatValue < presetFile.presets.Count) - presetFile.presets[(int)prop.floatValue].ApplyKeywordsToMaterials(prop.targets); + ApplyPreset(presetFileName, prop); } } @@ -1222,7 +1257,7 @@ public override void Apply(MaterialProperty prop) /// The full example: /// [Button(_)] _button0 ("URL Button@URL:https://github.com/JasonMa0012/LWGUI@C#:LWGUI.ButtonDrawer.TestMethod(1234, abcd)", Float) = 0 /// - /// group:father group name, support suffix keyword for conditional display (Default: none) + /// group: father group name, support suffix keyword for conditional display (Default: none) /// public class ButtonDrawer : SubDrawer { @@ -1370,7 +1405,7 @@ public static void TestMethod(MaterialProperty prop, MaterialEditor editor, LWGU /// /// Similar to Header() - /// group:father group name, support suffix keyword for conditional display (Default: none) + /// group: father group name, support suffix keyword for conditional display (Default: none) /// header: string to display, "SpaceLine" or "_" = none (Default: none) /// height: line height (Default: 22) /// @@ -1410,7 +1445,7 @@ public override void DrawProp(Rect position, MaterialProperty prop, GUIContent l /// /// Similar to Title() - /// group:father group name, support suffix keyword for conditional display (Default: none) + /// group: father group name, support suffix keyword for conditional display (Default: none) /// header: string to display, "SpaceLine" or "_" = none (Default: none) /// height: line height (Default: 22) /// @@ -1424,7 +1459,7 @@ public SubTitleDecorator(string group, string header, float height) : base(group /// /// Tooltip, describes the details of the property. (Default: property.name and property default value) /// You can also use "#Text" in DisplayName to add Tooltip that supports Multi-Language. - /// tooltip:a single-line string to display, support up to 4 ','. (Default: Newline) + /// tooltip: a single-line string to display, support up to 4 ','. (Default: Newline) /// public class TooltipDecorator : SubDrawer { @@ -1461,7 +1496,7 @@ public override void DrawProp(Rect position, MaterialProperty prop, GUIContent l /// /// Display a Helpbox on the property /// You can also use "%Text" in DisplayName to add Helpbox that supports Multi-Language. - /// message:a single-line string to display, support up to 4 ','. (Default: Newline) + /// message: a single-line string to display, support up to 4 ','. (Default: Newline) /// public class HelpboxDecorator : TooltipDecorator { @@ -1544,7 +1579,7 @@ public override void DrawProp(Rect position, MaterialProperty prop, GUIContent l public override void Apply(MaterialProperty prop) { base.Apply(prop); - if (!prop.hasMixedValue && IsMatchPropType(prop)) + if (ShowIfDecorator.GetShowIfResultToFilterDrawerApplying(prop)) Helper.SetShaderPassEnabled(prop.targets, _lightModeNames, prop.floatValue > 0); } } @@ -1645,7 +1680,8 @@ public class ShowIfData public float value = 0; } - private ShowIfData _showIfData = new(); + public ShowIfData showIfData = new(); + private readonly Dictionary _compareFunctionLUT = new() { { "Less", "Less" }, @@ -1669,65 +1705,104 @@ public ShowIfDecorator(string propName, string comparisonMethod, float value) : public ShowIfDecorator(string logicalOperator, string propName, string compareFunction, float value) { - _showIfData.logicalOperator = logicalOperator.ToLower() == "or" ? LogicalOperator.Or : LogicalOperator.And; - _showIfData.targetPropertyName = propName; + showIfData.logicalOperator = logicalOperator.ToLower() == "or" ? LogicalOperator.Or : LogicalOperator.And; + showIfData.targetPropertyName = propName; if (!_compareFunctionLUT.ContainsKey(compareFunction) || !Enum.IsDefined(typeof(CompareFunction), _compareFunctionLUT[compareFunction])) Debug.LogError("LWGUI: Invalid compareFunction: '" + compareFunction + "', Must be one of the following: Less (L) | Equal (E) | LessEqual (LEqual / LE) | Greater (G) | NotEqual (NEqual / NE) | GreaterEqual (GEqual / GE)."); else - _showIfData.compareFunction = (CompareFunction)Enum.Parse(typeof(CompareFunction), _compareFunctionLUT[compareFunction]); - _showIfData.value = value; + showIfData.compareFunction = (CompareFunction)Enum.Parse(typeof(CompareFunction), _compareFunctionLUT[compareFunction]); + showIfData.value = value; } - protected override float GetVisibleHeight(MaterialProperty prop) { return 0; } - - public override void BuildStaticMetaData(Shader inShader, MaterialProperty inProp, MaterialProperty[] inProps, PropertyStaticData inoutPropertyStaticData) + private static void Compare(ShowIfData showIfData, float targetValue, ref bool result) { - inoutPropertyStaticData.showIfDatas.Add(_showIfData); - } + bool compareResult; - public override void DrawProp(Rect position, MaterialProperty prop, GUIContent label, MaterialEditor editor) { } + switch (showIfData.compareFunction) + { + case CompareFunction.Less: + compareResult = targetValue < showIfData.value; + break; + case CompareFunction.LessEqual: + compareResult = targetValue <= showIfData.value; + break; + case CompareFunction.Greater: + compareResult = targetValue > showIfData.value; + break; + case CompareFunction.NotEqual: + compareResult = targetValue != showIfData.value; + break; + case CompareFunction.GreaterEqual: + compareResult = targetValue >= showIfData.value; + break; + default: + compareResult = targetValue == showIfData.value; + break; + } - public static void GetShowIfResult(PropertyStaticData propStaticData, PropertyDynamicData propDynamicData, PerMaterialData perMaterialData) - { - foreach (var showIfData in propStaticData.showIfDatas) + switch (showIfData.logicalOperator) { - var propCurrentValue = perMaterialData.propDynamicDatas[showIfData.targetPropertyName].property.floatValue; - bool compareResult; + case LogicalOperator.And: + result &= compareResult; + break; + case LogicalOperator.Or: + result |= compareResult; + break; + } + } - switch (showIfData.compareFunction) + public static bool GetShowIfResultToFilterDrawerApplying(MaterialProperty prop) + { + var material = prop.targets[0] as Material; + var showIfDatas = new List(); + { + var drawer = ReflectionHelper.GetPropertyDrawer(material.shader, prop, out var decoratorDrawers); + if (decoratorDrawers != null && decoratorDrawers.Count > 0) { - case CompareFunction.Less: - compareResult = propCurrentValue < showIfData.value; - break; - case CompareFunction.LessEqual: - compareResult = propCurrentValue <= showIfData.value; - break; - case CompareFunction.Greater: - compareResult = propCurrentValue > showIfData.value; - break; - case CompareFunction.NotEqual: - compareResult = propCurrentValue != showIfData.value; - break; - case CompareFunction.GreaterEqual: - compareResult = propCurrentValue >= showIfData.value; - break; - default: - compareResult = propCurrentValue == showIfData.value; - break; + foreach (ShowIfDecorator showIfDecorator in decoratorDrawers.Where(drawer => drawer is ShowIfDecorator)) + { + showIfDatas.Add(showIfDecorator.showIfData); + } } - - switch (showIfData.logicalOperator) + else { - case LogicalOperator.And: - propDynamicData.isShowing &= compareResult; - break; - case LogicalOperator.Or: - propDynamicData.isShowing |= compareResult; - break; + return true; } } + + return GetShowIfResultFromMaterial(showIfDatas, material); } + + public static bool GetShowIfResultFromMaterial(List showIfDatas, Material material) + { + bool result = true; + foreach (var showIfData in showIfDatas) + { + var targetValue = material.GetFloat(showIfData.targetPropertyName); + Compare(showIfData, targetValue, ref result); + } + + return result; + } + + public static void GetShowIfResult(PropertyStaticData propStaticData, PropertyDynamicData propDynamicData, PerMaterialData perMaterialData) + { + foreach (var showIfData in propStaticData.showIfDatas) + { + var targetValue = perMaterialData.propDynamicDatas[showIfData.targetPropertyName].property.floatValue; + Compare(showIfData, targetValue, ref propDynamicData.isShowing); + } + } + + protected override float GetVisibleHeight(MaterialProperty prop) { return 0; } + + public override void BuildStaticMetaData(Shader inShader, MaterialProperty inProp, MaterialProperty[] inProps, PropertyStaticData inoutPropertyStaticData) + { + inoutPropertyStaticData.showIfDatas.Add(showIfData); + } + + public override void DrawProp(Rect position, MaterialProperty prop, GUIContent label, MaterialEditor editor) { } } } //namespace LWGUI \ No newline at end of file diff --git a/README.md b/README.md index c982a53..b2f269f 100644 --- a/README.md +++ b/README.md @@ -109,8 +109,8 @@ Having been validated through numerous large-scale commercial projects, employin ```c# /// Create a Folding Group -/// group:group name (Default: Property Name) -/// keyword:keyword used for toggle, "_" = ignore, none or "__" = Property Name + "_ON", always Upper (Default: none) +/// group: group name (Default: Property Name) +/// keyword: keyword used for toggle, "_" = ignore, none or "__" = Property Name + "_ON", always Upper (Default: none) /// default Folding State: "on" or "off" (Default: off) /// default Toggle Displayed: "on" or "off" (Default: on) /// Target Property Type: FLoat, express Toggle value @@ -124,7 +124,7 @@ public MainDrawer(string group, string keyword, string defaultFoldingState, stri ```c# /// Draw a property with default style in the folding group -/// group:father group name, support suffix keyword for conditional display (Default: none) +/// group: father group name, support suffix keyword for conditional display (Default: none) /// Target Property Type: Any public SubDrawer() { } public SubDrawer(string group) @@ -178,8 +178,8 @@ Then change values: ```c# /// Similar to builtin Toggle() -/// group:father group name, support suffix keyword for conditional display (Default: none) -/// keyword:keyword used for toggle, "_" = ignore, none or "__" = Property Name + "_ON", always Upper (Default: none) +/// group: father group name, support suffix keyword for conditional display (Default: none) +/// keyword: keyword used for toggle, "_" = ignore, none or "__" = Property Name + "_ON", always Upper (Default: none) /// Target Property Type: FLoat public SubToggleDrawer() { } public SubToggleDrawer(string group) : this(group, String.Empty) { } @@ -193,7 +193,7 @@ public SubToggleDrawer(string group, string keyWord) ```c# /// Similar to builtin PowerSlider() -/// group:father group name, support suffix keyword for conditional display (Default: none) +/// group: father group name, support suffix keyword for conditional display (Default: none) /// power: power of slider (Default: 1) /// Target Property Type: Range public SubPowerSliderDrawer(float power) : this("_", power) { } @@ -205,7 +205,7 @@ public SubPowerSliderDrawer(string group, float power) ```c# /// Similar to builtin IntRange() -/// group:father group name, support suffix keyword for conditional display (Default: none) +/// group: father group name, support suffix keyword for conditional display (Default: none) /// Target Property Type: Range public SubIntRangeDrawer(string group) @@ -217,7 +217,7 @@ public SubIntRangeDrawer(string group) ```c# /// Draw a min max slider -/// group:father group name, support suffix keyword for conditional display (Default: none) +/// group: father group name, support suffix keyword for conditional display (Default: none) /// minPropName: Output Min Property Name /// maxPropName: Output Max Property Name /// Target Property Type: Range, range limits express the MinMaxSlider value range @@ -248,7 +248,7 @@ Result: ```c# /// /// Similar to builtin Enum() / KeywordEnum() -/// group:father group name, support suffix keyword for conditional display (Default: none) +/// group: father group name, support suffix keyword for conditional display (Default: none) /// n(s): display name /// k(s): keyword /// v(s): value @@ -300,7 +300,7 @@ public SubKeywordEnumDrawer(string group, string kw1, string kw2, string kw3, st ```c# /// Draw a Texture property in single line with a extra property -/// group:father group name, support suffix keyword for conditional display (Default: none) +/// group: father group name, support suffix keyword for conditional display (Default: none) /// extraPropName: extra property name (Default: none) /// Target Property Type: Texture /// Extra Property Type: Color, Vector @@ -312,7 +312,7 @@ public TexDrawer(string group, string extraPropName) ```c# /// Display up to 4 colors in a single line -/// group:father group name, support suffix keyword for conditional display (Default: none) +/// group: father group name, support suffix keyword for conditional display (Default: none) /// color2-4: extra color property name /// Target Property Type: Color public ColorDrawer(string group, string color2) : this(group, color2, String.Empty, String.Empty) { } @@ -346,7 +346,7 @@ Result: ```c# /// Draw an image preview. /// display name: The path of the image file relative to the Unity project, such as: "Assets/test.png", "Doc/test.png", "../test.png" -/// group:father group name, support suffix keyword for conditional display (Default: none) +/// group: father group name, support suffix keyword for conditional display (Default: none) public ImageDrawer() { } public ImageDrawer(string group) ``` @@ -366,7 +366,7 @@ Result: /// RGB Average = (1f / 3f, 1f / 3f, 1f / 3f, 0) /// RGB Luminance = (0.2126f, 0.7152f, 0.0722f, 0) /// None = (0, 0, 0, 0) -/// group:father group name, support suffix keyword for conditional display (Default: none) +/// group: father group name, support suffix keyword for conditional display (Default: none) /// Target Property Type: Vector, used to dot() with Texture Sample Value public ChannelDrawer() { } public ChannelDrawer(string group) @@ -479,7 +479,7 @@ The new LWGUI Gradient Editor integrates with Unity's built-in [Gradient Editor] ```c# /// Popping a menu, you can select the Shader Property Preset, the Preset values will replaces the default values -/// group:father group name, support suffix keyword for conditional display (Default: none) +/// group: father group name, support suffix keyword for conditional display (Default: none) /// presetFileName: "Shader Property Preset" asset name, you can create new Preset by /// "Right Click > Create > LWGUI > Shader Property Preset" in Project window, /// *any Preset in the entire project cannot have the same name* @@ -545,7 +545,7 @@ The Property Value in the selected Preset will be the default value /// The full example: /// [Button(_)] _button0 ("URL Button@URL:https://github.com/JasonMa0012/LWGUI@C#:LWGUI.ButtonDrawer.TestMethod(1234, abcd)", Float) = 0 /// -/// group:father group name, support suffix keyword for conditional display (Default: none) +/// group: father group name, support suffix keyword for conditional display (Default: none) public ButtonDrawer() { } public ButtonDrawer(string group) ``` @@ -568,7 +568,7 @@ Example: ```c# /// Similar to Header() -/// group:father group name, support suffix keyword for conditional display (Default: none) +/// group: father group name, support suffix keyword for conditional display (Default: none) /// header: string to display, "SpaceLine" or "_" = none (Default: none) /// height: line height (Default: 22) public TitleDecorator(string header) : this("_", header, DefaultHeight) {} @@ -578,7 +578,7 @@ public TitleDecorator(string group, string header, float height) /// Similar to Title() -/// group:father group name, support suffix keyword for conditional display (Default: none) +/// group: father group name, support suffix keyword for conditional display (Default: none) /// header: string to display, "SpaceLine" or "_" = none (Default: none) /// height: line height (Default: 22) public SubTitleDecorator(string group, string header) : base(group, header, DefaultHeight) {} @@ -591,7 +591,7 @@ public SubTitleDecorator(string group, string header, float height) : base(group ```c# /// Tooltip, describes the details of the property. (Default: property.name and property default value) /// You can also use "#Text" in DisplayName to add Tooltip that supports Multi-Language. -/// tooltip:a single-line string to display, support up to 4 ','. (Default: Newline) +/// tooltip: a single-line string to display, support up to 4 ','. (Default: Newline) public TooltipDecorator() : this(string.Empty) {} public TooltipDecorator(string tooltip) { this._tooltip = tooltip; } public TooltipDecorator(string s1, string s2) : this(s1 + ", " + s2) { } @@ -605,7 +605,7 @@ public TooltipDecorator(string s1, string s2, string s3, string s4, string s5) : ```c# /// Display a Helpbox on the property /// You can also use "%Text" in DisplayName to add Helpbox that supports Multi-Language. -/// message:a single-line string to display, support up to 4 ','. (Default: Newline) +/// message: a single-line string to display, support up to 4 ','. (Default: Newline) public HelpboxDecorator() : this(string.Empty) {} public HelpboxDecorator(string message) { this._message = message; } public HelpboxDecorator(string s1, string s2) : this(s1 + ", " + s2) { } diff --git a/README_CN.md b/README_CN.md index eb01caf..d1fb002 100644 --- a/README_CN.md +++ b/README_CN.md @@ -106,22 +106,24 @@ ```c# /// Create a Folding Group -/// group:group name (Default: Property Name) -/// keyword:keyword used for toggle, "_" = ignore, none or "__" = Property Name + "_ON", always Upper (Default: none) +/// group: group name (Default: Property Name) +/// keyword: keyword used for toggle, "_" = ignore, none or "__" = Property Name + "_ON", always Upper (Default: none) /// default Folding State: "on" or "off" (Default: off) /// default Toggle Displayed: "on" or "off" (Default: on) +/// preset File Name: "Shader Property Preset" asset name, see Preset() for detail (Default: none) /// Target Property Type: FLoat, express Toggle value public MainDrawer() : this(String.Empty) { } public MainDrawer(string group) : this(group, String.Empty) { } public MainDrawer(string group, string keyword) : this(group, keyword, "off") { } public MainDrawer(string group, string keyword, string defaultFoldingState) : this(group, keyword, defaultFoldingState, "on") { } -public MainDrawer(string group, string keyword, string defaultFoldingState, string defaultToggleDisplayed) +public MainDrawer(string group, string keyword, string defaultFoldingState, string defaultToggleDisplayed) : this(group, keyword, defaultFoldingState, defaultToggleDisplayed, String.Empty) { } +public MainDrawer(string group, string keyword, string defaultFoldingState, string defaultToggleDisplayed, string presetFileName) ``` ```c# /// Draw a property with default style in the folding group -/// group:father group name, support suffix keyword for conditional display (Default: none) +/// group: father group name, support suffix keyword for conditional display (Default: none) /// Target Property Type: Any public SubDrawer() { } public SubDrawer(string group) @@ -175,13 +177,14 @@ Then change values: ```c# /// Similar to builtin Toggle() -/// group:father group name, support suffix keyword for conditional display (Default: none) -/// keyword:keyword used for toggle, "_" = ignore, none or "__" = Property Name + "_ON", always Upper (Default: none) +/// group: father group name, support suffix keyword for conditional display (Default: none) +/// keyword: keyword used for toggle, "_" = ignore, none or "__" = Property Name + "_ON", always Upper (Default: none) +/// preset File Name: "Shader Property Preset" asset name, see Preset() for detail (Default: none) /// Target Property Type: FLoat public SubToggleDrawer() { } -public SubToggleDrawer(string group) : this(group, String.Empty) { } -public SubToggleDrawer(string group, string keyWord) - +public SubToggleDrawer(string group) : this(group, String.Empty, String.Empty) { } +public SubToggleDrawer(string group, string keyWord) : this(group, keyWord, String.Empty) { } +public SubToggleDrawer(string group, string keyWord, string presetFileName) ``` @@ -190,7 +193,7 @@ public SubToggleDrawer(string group, string keyWord) ```c# /// Similar to builtin PowerSlider() -/// group:father group name, support suffix keyword for conditional display (Default: none) +/// group: father group name, support suffix keyword for conditional display (Default: none) /// power: power of slider (Default: 1) /// Target Property Type: Range public SubPowerSliderDrawer(float power) : this("_", power) { } @@ -202,7 +205,7 @@ public SubPowerSliderDrawer(string group, float power) ```c# /// Similar to builtin IntRange() -/// group:father group name, support suffix keyword for conditional display (Default: none) +/// group: father group name, support suffix keyword for conditional display (Default: none) /// Target Property Type: Range public SubIntRangeDrawer(string group) @@ -214,7 +217,7 @@ public SubIntRangeDrawer(string group) ```c# /// Draw a min max slider -/// group:father group name, support suffix keyword for conditional display (Default: none) +/// group: father group name, support suffix keyword for conditional display (Default: none) /// minPropName: Output Min Property Name /// maxPropName: Output Max Property Name /// Target Property Type: Range, range limits express the MinMaxSlider value range @@ -245,7 +248,7 @@ Result: ```c# /// /// Similar to builtin Enum() / KeywordEnum() -/// group:father group name, support suffix keyword for conditional display (Default: none) +/// group: father group name, support suffix keyword for conditional display (Default: none) /// n(s): display name /// k(s): keyword /// v(s): value @@ -297,7 +300,7 @@ public SubKeywordEnumDrawer(string group, string kw1, string kw2, string kw3, st ```c# /// Draw a Texture property in single line with a extra property -/// group:father group name, support suffix keyword for conditional display (Default: none) +/// group: father group name, support suffix keyword for conditional display (Default: none) /// extraPropName: extra property name (Default: none) /// Target Property Type: Texture /// Extra Property Type: Color, Vector @@ -309,7 +312,7 @@ public TexDrawer(string group, string extraPropName) ```c# /// Display up to 4 colors in a single line -/// group:father group name, support suffix keyword for conditional display (Default: none) +/// group: father group name, support suffix keyword for conditional display (Default: none) /// color2-4: extra color property name /// Target Property Type: Color public ColorDrawer(string group, string color2) : this(group, color2, String.Empty, String.Empty) { } @@ -345,7 +348,7 @@ Result: ```c# /// Draw an image preview. /// display name: The path of the image file relative to the Unity project, such as: "Assets/test.png", "Doc/test.png", "../test.png" -/// group:father group name, support suffix keyword for conditional display (Default: none) +/// group: father group name, support suffix keyword for conditional display (Default: none) public ImageDrawer() { } public ImageDrawer(string group) ``` @@ -365,7 +368,7 @@ Result: /// RGB Average = (1f / 3f, 1f / 3f, 1f / 3f, 0) /// RGB Luminance = (0.2126f, 0.7152f, 0.0722f, 0) /// None = (0, 0, 0, 0) -/// group:father group name, support suffix keyword for conditional display (Default: none) +/// group: father group name, support suffix keyword for conditional display (Default: none) /// Target Property Type: Vector, used to dot() with Texture Sample Value public ChannelDrawer() { } public ChannelDrawer(string group) @@ -475,7 +478,7 @@ Result: ```c# /// Popping a menu, you can select the Shader Property Preset, the Preset values will replaces the default values -/// group:father group name, support suffix keyword for conditional display (Default: none) +/// group: father group name, support suffix keyword for conditional display (Default: none) /// presetFileName: "Shader Property Preset" asset name, you can create new Preset by /// "Right Click > Create > LWGUI > Shader Property Preset" in Project window, /// *any Preset in the entire project cannot have the same name* @@ -539,7 +542,7 @@ Result: /// The full example: /// [Button(_)] _button0 ("URL Button@URL:https://github.com/JasonMa0012/LWGUI@C#:LWGUI.ButtonDrawer.TestMethod(1234, abcd)", Float) = 0 /// -/// group:father group name, support suffix keyword for conditional display (Default: none) +/// group: father group name, support suffix keyword for conditional display (Default: none) public ButtonDrawer() { } public ButtonDrawer(string group) ``` @@ -562,7 +565,7 @@ Example: ```c# /// Similar to Header() -/// group:father group name, support suffix keyword for conditional display (Default: none) +/// group: father group name, support suffix keyword for conditional display (Default: none) /// header: string to display, "SpaceLine" or "_" = none (Default: none) /// height: line height (Default: 22) public TitleDecorator(string header) : this("_", header, DefaultHeight) {} @@ -572,7 +575,7 @@ public TitleDecorator(string group, string header, float height) /// Similar to Title() -/// group:father group name, support suffix keyword for conditional display (Default: none) +/// group: father group name, support suffix keyword for conditional display (Default: none) /// header: string to display, "SpaceLine" or "_" = none (Default: none) /// height: line height (Default: 22) public SubTitleDecorator(string group, string header) : base(group, header, DefaultHeight) {} @@ -585,7 +588,7 @@ public SubTitleDecorator(string group, string header, float height) : base(group ```c# /// Tooltip, describes the details of the property. (Default: property.name and property default value) /// You can also use "#Text" in DisplayName to add Tooltip that supports Multi-Language. -/// tooltip:a single-line string to display, support up to 4 ','. (Default: Newline) +/// tooltip: a single-line string to display, support up to 4 ','. (Default: Newline) public TooltipDecorator() : this(string.Empty) {} public TooltipDecorator(string tooltip) { this._tooltip = tooltip; } public TooltipDecorator(string s1, string s2) : this(s1 + ", " + s2) { } @@ -599,7 +602,7 @@ public TooltipDecorator(string s1, string s2, string s3, string s4, string s5) : ```c# /// Display a Helpbox on the property /// You can also use "%Text" in DisplayName to add Helpbox that supports Multi-Language. -/// message:a single-line string to display, support up to 4 ','. (Default: Newline) +/// message: a single-line string to display, support up to 4 ','. (Default: Newline) public HelpboxDecorator() : this(string.Empty) {} public HelpboxDecorator(string message) { this._message = message; } public HelpboxDecorator(string s1, string s2) : this(s1 + ", " + s2) { } diff --git a/Test/LWGUI_BlendModePreset.asset b/Test/LWGUI_Preset_BlendMode.asset similarity index 98% rename from Test/LWGUI_BlendModePreset.asset rename to Test/LWGUI_Preset_BlendMode.asset index 986a4a0..5cf0fea 100644 --- a/Test/LWGUI_BlendModePreset.asset +++ b/Test/LWGUI_Preset_BlendMode.asset @@ -10,7 +10,7 @@ MonoBehaviour: m_Enabled: 1 m_EditorHideFlags: 0 m_Script: {fileID: 11500000, guid: 28fbcbca3fb14507af6ed5c104c40b84, type: 3} - m_Name: LWGUI_BlendModePreset + m_Name: LWGUI_Preset_BlendMode m_EditorClassIdentifier: presets: - presetName: Opaque diff --git a/Test/LWGUI_BlendModePreset.asset.meta b/Test/LWGUI_Preset_BlendMode.asset.meta similarity index 100% rename from Test/LWGUI_BlendModePreset.asset.meta rename to Test/LWGUI_Preset_BlendMode.asset.meta diff --git a/Test/LWGUI_Preset_Toggle.asset b/Test/LWGUI_Preset_Toggle.asset new file mode 100644 index 0000000..7be4bf7 --- /dev/null +++ b/Test/LWGUI_Preset_Toggle.asset @@ -0,0 +1,29 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &11400000 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 28fbcbca3fb14507af6ed5c104c40b84, type: 3} + m_Name: LWGUI_Preset_Toggle + m_EditorClassIdentifier: + presets: + - presetName: Disable + propertyValues: [] + enabledKeywords: [] + disabledKeywords: [] + enabledPasses: [] + disabledPasses: [] + renderQueue: 2000 + - presetName: Enable + propertyValues: [] + enabledKeywords: [] + disabledKeywords: [] + enabledPasses: [] + disabledPasses: [] + renderQueue: 2001 diff --git a/Test/LWGUI_Preset_Toggle.asset.meta b/Test/LWGUI_Preset_Toggle.asset.meta new file mode 100644 index 0000000..b094f4e --- /dev/null +++ b/Test/LWGUI_Preset_Toggle.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: c3b3b9433213824419f41452830ee4ea +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 11400000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Test/SampleDrawer 1.shader b/Test/SampleDrawer 1.shader index 19a2de2..d618b59 100644 --- a/Test/SampleDrawer 1.shader +++ b/Test/SampleDrawer 1.shader @@ -12,6 +12,7 @@ [Main(Group1, _KEYWORD, on)] _group1 ("Group - Default Open", float) = 1 [Preset(Group1, LWGUI_ShaderPropertyPreset)] _preset ("Preset Sample", float) = 0 [Preset(Group1, LWGUI_ShaderPropertyPreset1)] _preset1 ("Preset Sample 1", float) = 0 + [SubToggle(Group1, _, LWGUI_Preset_Toggle)] _preset_toggle ("Preset Toggle Sample", float) = 0 [Sub(Group1)] _float1 ("Sub Float", float) = 0 [Sub(Group1)] _vector1 ("Sub Vector", vector) = (1, 1, 1, 1) [Sub(Group1)] [HDR] _color1 ("Sub HDR Color", color) = (0.7, 0.7, 1, 1) diff --git a/Test/SampleDrawer.shader b/Test/SampleDrawer.shader index aed9602..220d88a 100644 --- a/Test/SampleDrawer.shader +++ b/Test/SampleDrawer.shader @@ -44,7 +44,7 @@ [Main(Preset, _, on, off)] _PresetGroup ("Preset Samples", float) = 0 - [Preset(Preset, LWGUI_BlendModePreset)] _BlendMode ("Blend Mode Preset", float) = 0 + [Preset(Preset, LWGUI_Preset_BlendMode)] _BlendMode ("Blend Mode Preset", float) = 0 [SubEnum(Preset, UnityEngine.Rendering.CullMode)] _Cull ("Cull", Float) = 2 [SubEnum(Preset, UnityEngine.Rendering.BlendMode)] _SrcBlend ("SrcBlend", Float) = 1 [SubEnum(Preset, UnityEngine.Rendering.BlendMode)] _DstBlend ("DstBlend", Float) = 0 diff --git a/package.json b/package.json index a95194a..f933781 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "com.jasonma.lwgui", - "version": "1.20.2", + "version": "1.21.0", "displayName": "LWGUI", "description": "A Lightweight, Flexible, Powerful Shader GUI System for Unity.", "keywords": [ From 8bed3ae8d9b8195163fc238a7b589e68eb693a4d Mon Sep 17 00:00:00 2001 From: Jason Ma <1312119064@qq.com> Date: Wed, 8 Jan 2025 18:25:15 +0800 Subject: [PATCH 2/2] Fixed keywords and presets being applied incorrectly when editing multiple materials --- Editor/LWGUI.cs | 1 - Editor/ShaderDrawer.cs | 33 ++++++++++++++------ UnityEditorExtension/UnityEditorExtension.cs | 1 + package.json | 2 +- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/Editor/LWGUI.cs b/Editor/LWGUI.cs index 15bd16d..7cede9a 100644 --- a/Editor/LWGUI.cs +++ b/Editor/LWGUI.cs @@ -195,7 +195,6 @@ public static void OnValidate(Object[] materials) public static void OnValidate(LWGUIMetaDatas metaDatas) { OnValidate(metaDatas?.GetMaterialEditor()?.targets); - OnValidate(metaDatas?.GetMaterialEditor()?.targets); } // Called after edit or undo diff --git a/Editor/ShaderDrawer.cs b/Editor/ShaderDrawer.cs index a8e6039..754159d 100644 --- a/Editor/ShaderDrawer.cs +++ b/Editor/ShaderDrawer.cs @@ -119,12 +119,15 @@ public override float GetPropertyHeight(MaterialProperty prop, string label, Mat return _height; } - // Call when creating new material, used to set keywords + // Call when create/edit/undo materials, used to set keywords and presets public override void Apply(MaterialProperty prop) { base.Apply(prop); - Helper.SetShaderKeyWord(prop.targets, Helper.GetKeyWord(_keyword, prop.name), prop.floatValue > 0f); - PresetDrawer.ApplyPreset(_presetFileName, prop); + if (!prop.hasMixedValue) + { + Helper.SetShaderKeyWord(prop.targets, Helper.GetKeyWord(_keyword, prop.name), prop.floatValue > 0f); + PresetDrawer.ApplyPreset(_presetFileName, prop); + } } } @@ -248,8 +251,11 @@ public override void DrawProp(Rect position, MaterialProperty prop, GUIContent l public override void Apply(MaterialProperty prop) { base.Apply(prop); - Helper.SetShaderKeyWord(prop.targets, Helper.GetKeyWord(_keyWord, prop.name), prop.floatValue > 0f); - PresetDrawer.ApplyPreset(_presetFileName, prop); + if (!prop.hasMixedValue) + { + Helper.SetShaderKeyWord(prop.targets, Helper.GetKeyWord(_keyWord, prop.name), prop.floatValue > 0f); + PresetDrawer.ApplyPreset(_presetFileName, prop); + } } } @@ -561,7 +567,10 @@ public override void DrawProp(Rect position, MaterialProperty prop, GUIContent l public override void Apply(MaterialProperty prop) { base.Apply(prop); - Helper.SetShaderKeyWord(prop.targets, GetKeywords(prop), (int)prop.floatValue); + if (!prop.hasMixedValue) + { + Helper.SetShaderKeyWord(prop.targets, GetKeywords(prop), (int)prop.floatValue); + } } } @@ -1236,7 +1245,10 @@ public override void DrawProp(Rect position, MaterialProperty prop, GUIContent l public override void Apply(MaterialProperty prop) { base.Apply(prop); - ApplyPreset(presetFileName, prop); + if (!prop.hasMixedValue) + { + ApplyPreset(presetFileName, prop); + } } } @@ -1579,8 +1591,11 @@ public override void DrawProp(Rect position, MaterialProperty prop, GUIContent l public override void Apply(MaterialProperty prop) { base.Apply(prop); - if (ShowIfDecorator.GetShowIfResultToFilterDrawerApplying(prop)) - Helper.SetShaderPassEnabled(prop.targets, _lightModeNames, prop.floatValue > 0); + if (!prop.hasMixedValue) + { + if (ShowIfDecorator.GetShowIfResultToFilterDrawerApplying(prop)) + Helper.SetShaderPassEnabled(prop.targets, _lightModeNames, prop.floatValue > 0); + } } } diff --git a/UnityEditorExtension/UnityEditorExtension.cs b/UnityEditorExtension/UnityEditorExtension.cs index 1db5a11..a24c73c 100644 --- a/UnityEditorExtension/UnityEditorExtension.cs +++ b/UnityEditorExtension/UnityEditorExtension.cs @@ -18,6 +18,7 @@ public static void ApplyMaterialPropertyAndDecoratorDrawers(Material material) ApplyMaterialPropertyAndDecoratorDrawers(objs); } + // Called after edit or undo public static void ApplyMaterialPropertyAndDecoratorDrawers(Object[] targets) { if (!EditorMaterialUtility.disableApplyMaterialPropertyDrawers) diff --git a/package.json b/package.json index f933781..33bc3e2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "com.jasonma.lwgui", - "version": "1.21.0", + "version": "1.21.1", "displayName": "LWGUI", "description": "A Lightweight, Flexible, Powerful Shader GUI System for Unity.", "keywords": [