Skip to content

Commit

Permalink
Add ApplyMaterialPropertyAndDecoratorDrawers() to fix PassSwitch() no…
Browse files Browse the repository at this point in the history
…t working when editing materials in code
  • Loading branch information
JasonMa0012 committed Sep 13, 2024
1 parent e182fb8 commit 2f5ceb6
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Editor/Helper/MetaDataHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class LWGUIMetaDatas

public void OnValidate()
{
MaterialEditor.ApplyMaterialPropertyDrawers(GetMaterialEditor()?.targets);
UnityEditorExtension.ApplyMaterialPropertyAndDecoratorDrawers(GetMaterialEditor()?.targets);
MetaDataHelper.ForceUpdateMaterialsMetadataCache(GetMaterialEditor()?.targets);
}
}
Expand Down
8 changes: 3 additions & 5 deletions Editor/Helper/PresetHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MaterialPropertyDrawer> 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)
Expand All @@ -73,9 +72,8 @@ public static void ApplyPresetsInMaterial(Material material)
if (activePreset != null)
activePreset.ApplyToDefaultMaterial(material);
}

}
MaterialEditor.ApplyMaterialPropertyDrawers(material);
UnityEditorExtension.ApplyMaterialPropertyAndDecoratorDrawers(material);
}
}
}
2 changes: 1 addition & 1 deletion Editor/ScriptableObject/ShaderPropertyPreset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
56 changes: 56 additions & 0 deletions UnityEditorExtension/UnityEditorExtension.cs
Original file line number Diff line number Diff line change
@@ -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

}
}
3 changes: 3 additions & 0 deletions UnityEditorExtension/UnityEditorExtension.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down

0 comments on commit 2f5ceb6

Please sign in to comment.