优化特效-溶解材质
This commit is contained in:
1 parent
5d58cb3743
commit
5bf02177df
32 files changed
+6537
-8
No files matched your search
@@ -1,5 +1,6 @@
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
|
||||
// UIMask 的材质面板:分组折叠 + 中文标签。
|
||||
// shader 里用 CustomEditor "UIMaskShaderGUI" 挂上这个类。
|
||||
@@ -9,6 +10,9 @@ public class UIMaskShaderGUI : ShaderGUI
|
||||
{
|
||||
private const string FoldKeyPrefix = "UIMask.GUI.";
|
||||
|
||||
// 用来读取 shader 里声明的默认值(新建一个隐藏材质读它的初始值)
|
||||
private static Material defaultsMaterial;
|
||||
|
||||
private class Group
|
||||
{
|
||||
public string Label;
|
||||
@@ -45,7 +49,17 @@ public class UIMaskShaderGUI : ShaderGUI
|
||||
{
|
||||
Label = "溶解",
|
||||
Toggle = "_UseDissolve",
|
||||
Props = new[] { "_DissolveTex", "_DissolveChannel", "_Dissolve", "_DissolveWidth" }
|
||||
Props = new[]
|
||||
{
|
||||
"_DissolveTex",
|
||||
"_DissolveChannel",
|
||||
"_Dissolve",
|
||||
"_DissolveWidth",
|
||||
"_DissolveEdgeColor",
|
||||
"_DissolveEdgeSharpness",
|
||||
"_DissolveHighlightColor",
|
||||
"_DissolveHighlightWidth"
|
||||
}
|
||||
},
|
||||
new Group
|
||||
{
|
||||
@@ -82,11 +96,15 @@ public class UIMaskShaderGUI : ShaderGUI
|
||||
EditorGUILayout.BeginVertical(EditorStyles.helpBox);
|
||||
EditorGUILayout.BeginHorizontal();
|
||||
bool newExpanded = EditorGUILayout.Foldout(expanded, group.Label, true, EditorStyles.foldoutHeader);
|
||||
GUILayout.FlexibleSpace();
|
||||
if (toggle != null)
|
||||
{
|
||||
GUILayout.FlexibleSpace();
|
||||
editor.ShaderProperty(toggle, GUIContent.none, 0);
|
||||
}
|
||||
if (GUILayout.Button(new GUIContent("重置", "把本组参数恢复为 shader 默认值"), EditorStyles.miniButton, GUILayout.Width(42f)))
|
||||
{
|
||||
ResetGroup(editor, props, group);
|
||||
}
|
||||
EditorGUILayout.EndHorizontal();
|
||||
|
||||
if (newExpanded != expanded)
|
||||
@@ -104,6 +122,123 @@ public class UIMaskShaderGUI : ShaderGUI
|
||||
EditorGUILayout.EndVertical();
|
||||
}
|
||||
|
||||
// 把某一组的属性恢复成 shader 默认值(启用开关同时同步它的关键字)
|
||||
private static void ResetGroup(MaterialEditor editor, MaterialProperty[] props, Group group)
|
||||
{
|
||||
Material sample = null;
|
||||
foreach (Object target in editor.targets)
|
||||
{
|
||||
sample = target as Material;
|
||||
if (sample != null)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (sample == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (defaultsMaterial == null || defaultsMaterial.shader != sample.shader)
|
||||
{
|
||||
if (defaultsMaterial != null)
|
||||
{
|
||||
Object.DestroyImmediate(defaultsMaterial);
|
||||
}
|
||||
defaultsMaterial = new Material(sample.shader) { hideFlags = HideFlags.HideAndDontSave };
|
||||
}
|
||||
|
||||
Undo.RecordObjects(editor.targets, "Reset " + group.Label);
|
||||
|
||||
foreach (string name in EnumerateNames(group))
|
||||
{
|
||||
MaterialProperty prop = FindProperty(name, props, false);
|
||||
if (prop == null || !defaultsMaterial.HasProperty(name))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (prop.type)
|
||||
{
|
||||
case MaterialProperty.PropType.Color:
|
||||
prop.colorValue = defaultsMaterial.GetColor(name);
|
||||
break;
|
||||
|
||||
case MaterialProperty.PropType.Vector:
|
||||
prop.vectorValue = defaultsMaterial.GetVector(name);
|
||||
break;
|
||||
|
||||
case MaterialProperty.PropType.Texture:
|
||||
prop.textureValue = defaultsMaterial.GetTexture(name);
|
||||
break;
|
||||
|
||||
default:
|
||||
float value = defaultsMaterial.GetFloat(name);
|
||||
prop.floatValue = value;
|
||||
// 开关类属性(_UseXxx)要把关键字一起恢复,否则面板勾选和实际编译状态会对不上
|
||||
if (name.StartsWith("_Use"))
|
||||
{
|
||||
SetKeyword(editor, KeywordOf(name), value > 0.5f);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (Object target in editor.targets)
|
||||
{
|
||||
EditorUtility.SetDirty(target);
|
||||
}
|
||||
}
|
||||
|
||||
private static IEnumerable<string> EnumerateNames(Group group)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(group.Toggle))
|
||||
{
|
||||
yield return group.Toggle;
|
||||
}
|
||||
|
||||
foreach (string row in group.Props)
|
||||
{
|
||||
int separator = row.IndexOf('|');
|
||||
if (separator < 0)
|
||||
{
|
||||
yield return row;
|
||||
}
|
||||
else
|
||||
{
|
||||
yield return row.Substring(0, separator);
|
||||
yield return row.Substring(separator + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static string KeywordOf(string propertyName)
|
||||
{
|
||||
string trimmed = propertyName.StartsWith("_") ? propertyName.Substring(1) : propertyName;
|
||||
return "_" + trimmed.ToUpperInvariant() + "_ON";
|
||||
}
|
||||
|
||||
private static void SetKeyword(MaterialEditor editor, string keyword, bool enabled)
|
||||
{
|
||||
foreach (Object target in editor.targets)
|
||||
{
|
||||
var material = target as Material;
|
||||
if (material == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (enabled)
|
||||
{
|
||||
material.EnableKeyword(keyword);
|
||||
}
|
||||
else
|
||||
{
|
||||
material.DisableKeyword(keyword);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// group.Props 里一项 = 一行;写 "A|B" 表示这两个单值属性并排在同一行
|
||||
private static void DrawProps(MaterialEditor editor, MaterialProperty[] props, string[] rows)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user