Files
ParticleEffectForUGUI/Packages/src/Editor/AnimatablePropertyEditor.cs

215 lines
7.5 KiB
C#
Raw Normal View History

2023-08-17 09:43:02 +09:00
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEngine;
using ShaderPropertyType = Coffee.UIExtensions.AnimatableProperty.ShaderPropertyType;
2023-08-17 09:43:02 +09:00
namespace Coffee.UIExtensions
{
internal static class AnimatablePropertyEditor
{
private static readonly GUIContent s_ContentNothing = new GUIContent("Nothing");
private static readonly GUIContent s_ContentCustom = new GUIContent("Add Custom...");
2023-08-17 09:43:02 +09:00
private static readonly List<string> s_ActiveNames = new List<string>();
private static readonly StringBuilder s_Sb = new StringBuilder();
private static readonly HashSet<string> s_Names = new HashSet<string>();
private static ShaderProperty s_CustomProperty = new ShaderProperty("", ShaderPropertyType.None);
private static bool s_ShowCustomProperty = false;
2023-08-17 09:43:02 +09:00
private static string CollectActiveNames(SerializedProperty sp, List<string> result)
{
result.Clear();
for (var i = 0; i < sp.arraySize; i++)
{
var spName = sp.GetArrayElementAtIndex(i).FindPropertyRelative("m_Name");
if (spName == null) continue;
result.Add(spName.stringValue);
}
s_Sb.Length = 0;
if (result.Count == 0)
{
s_Sb.Append("Nothing");
}
else
{
2024-06-27 11:38:28 +09:00
result.Aggregate(s_Sb, (a, b) =>
{
s_Sb.Append(b);
return s_Sb.Append(", ");
});
2023-08-17 09:43:02 +09:00
s_Sb.Length -= 2;
}
return s_Sb.ToString();
}
2024-06-27 11:38:28 +09:00
public static void Draw(SerializedProperty sp, List<Material> mats)
2023-08-17 09:43:02 +09:00
{
2024-06-27 11:38:28 +09:00
var pos = EditorGUILayout.GetControlRect(true);
var label = new GUIContent(sp.displayName, sp.tooltip);
var rect = EditorGUI.PrefixLabel(pos, label);
var text = sp.hasMultipleDifferentValues
? "-"
: CollectActiveNames(sp, s_ActiveNames);
2023-08-17 09:43:02 +09:00
if (GUI.Button(rect, text, EditorStyles.popup))
{
ShowMenu(sp, mats);
}
2023-08-17 09:43:02 +09:00
if (s_ShowCustomProperty)
{
DrawCustomProperty(sp, ref s_CustomProperty);
}
}
private static void ShowMenu(SerializedProperty sp, List<Material> mats)
{
2023-08-17 09:43:02 +09:00
var gm = new GenericMenu();
2024-06-27 11:38:28 +09:00
gm.AddItem(s_ContentNothing, s_ActiveNames.Count == 0, x =>
2023-08-17 09:43:02 +09:00
{
2024-06-27 11:38:28 +09:00
var current = (SerializedProperty)x;
current.ClearArray();
current.serializedObject.ApplyModifiedProperties();
}, sp);
2023-08-17 09:43:02 +09:00
gm.AddItem(s_ContentCustom, s_ShowCustomProperty, () =>
{
s_ShowCustomProperty = !s_ShowCustomProperty;
s_CustomProperty.Reset();
});
gm.AddSeparator("");
2023-08-17 09:43:02 +09:00
if (!sp.hasMultipleDifferentValues)
{
for (var i = 0; i < sp.arraySize; i++)
{
var p = sp.GetArrayElementAtIndex(i);
var name = p.FindPropertyRelative("m_Name").stringValue;
var type = (ShaderPropertyType)p.FindPropertyRelative("m_Type").intValue;
2023-08-17 09:43:02 +09:00
AddMenu(gm, sp, new ShaderProperty(name, type), false);
}
}
s_Names.Clear();
2024-06-27 11:38:28 +09:00
for (var j = 0; j < mats.Count; j++)
2023-08-17 09:43:02 +09:00
{
var mat = mats[j];
2026-03-24 17:32:31 +09:00
if (mat == null || mat.shader == null) continue;
2023-08-17 09:43:02 +09:00
#if UNITY_6000_5_OR_NEWER
for (var i = 0; i < mat.shader.GetPropertyCount(); i++)
#else
2023-08-17 09:43:02 +09:00
for (var i = 0; i < ShaderUtil.GetPropertyCount(mat.shader); i++)
#endif
2023-08-17 09:43:02 +09:00
{
#if UNITY_6000_5_OR_NEWER
var name = mat.shader.GetPropertyName(i);
var type = (ShaderPropertyType)mat.shader.GetPropertyType(i);
#else
2023-08-17 09:43:02 +09:00
var name = ShaderUtil.GetPropertyName(mat.shader, i);
var type = (ShaderPropertyType)ShaderUtil.GetPropertyType(mat.shader, i);
#endif
2024-06-27 11:38:28 +09:00
if (!s_Names.Add(name)) continue;
2023-08-17 09:43:02 +09:00
AddMenu(gm, sp, new ShaderProperty(name, type), true);
if (type != ShaderPropertyType.Texture) continue;
2023-08-17 09:43:02 +09:00
AddMenu(gm, sp, new ShaderProperty($"{name}_ST"), true);
AddMenu(gm, sp, new ShaderProperty($"{name}_HDR"), true);
AddMenu(gm, sp, new ShaderProperty($"{name}_TexelSize"), true);
}
}
gm.ShowAsContext();
}
private static void AddMenu(GenericMenu menu, SerializedProperty sp, ShaderProperty prop, bool add)
{
if (add && s_ActiveNames.Contains(prop.name)) return;
var label = new GUIContent($"{prop.name} ({prop.type})");
menu.AddItem(label, s_ActiveNames.Contains(prop.name), () => AddProp(sp, prop));
}
private static void DrawCustomProperty(SerializedProperty sp, ref ShaderProperty prop)
{
var r = EditorGUILayout.GetControlRect(false, EditorGUIUtility.singleLineHeight + 8);
r.xMin += 60;
GUI.Label(r, (Texture)null, EditorStyles.helpBox);
r = new Rect(r.x + 4, r.y + 4, r.width - 8 - 100 - 16, r.height - 8);
prop.name = EditorGUI.TextField(r, prop.name);
r.x += r.width + 2;
r.width = 100 - 2;
prop.type = (ShaderPropertyType)EditorGUI.EnumPopup(r, prop.type);
r.x += r.width;
r.width = 16;
EditorGUI.BeginDisabledGroup(!prop.IsValid(s_ActiveNames));
if (GUI.Button(r, EditorGUIUtility.IconContent("Toolbar Plus"), EditorStyles.label))
2023-08-17 09:43:02 +09:00
{
GUI.FocusControl("");
AddProp(sp, prop);
prop.Reset();
}
2023-08-17 09:43:02 +09:00
EditorGUI.EndDisabledGroup();
}
private static void AddProp(SerializedProperty sp, ShaderProperty prop)
{
var index = s_ActiveNames.IndexOf(prop.name);
if (0 <= index)
{
sp.DeleteArrayElementAtIndex(index);
}
else
{
sp.InsertArrayElementAtIndex(sp.arraySize);
var p = sp.GetArrayElementAtIndex(sp.arraySize - 1);
p.FindPropertyRelative("m_Name").stringValue = prop.name;
p.FindPropertyRelative("m_Type").intValue = (int)prop.type;
}
sp.serializedObject.ApplyModifiedProperties();
2023-08-17 09:43:02 +09:00
}
private struct ShaderProperty
{
public string name;
public ShaderPropertyType type;
2023-08-17 09:43:02 +09:00
public ShaderProperty(string name)
{
this.name = name;
type = ShaderPropertyType.Vector;
2023-08-17 09:43:02 +09:00
}
public ShaderProperty(string name, ShaderPropertyType type)
2023-08-17 09:43:02 +09:00
{
this.name = name;
this.type = type;
}
public void Reset()
{
name = "";
type = ShaderPropertyType.None;
}
public bool IsValid(List<string> activeNames)
{
if (string.IsNullOrEmpty(name)) return false;
if (type == ShaderPropertyType.None) return false;
if (activeNames.Contains(name)) return false;
return true;
}
2023-08-17 09:43:02 +09:00
}
}
}