AnimatedPropertiesEditor.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using UnityEditor;
  2. using UnityEngine;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using ShaderPropertyType = Coffee.UIExtensions.AnimatableProperty.ShaderPropertyType;
  6. namespace Coffee.UIExtensions
  7. {
  8. internal class AnimatedPropertiesEditor
  9. {
  10. private static readonly List<string> s_ActiveNames = new List<string>();
  11. private static readonly System.Text.StringBuilder s_Sb = new System.Text.StringBuilder();
  12. private static readonly HashSet<string> s_Names = new HashSet<string>();
  13. private string _name;
  14. private ShaderPropertyType _type;
  15. static string CollectActiveNames(SerializedProperty sp, List<string> result)
  16. {
  17. result.Clear();
  18. for (var i = 0; i < sp.arraySize; i++)
  19. {
  20. var spName = sp.GetArrayElementAtIndex(i).FindPropertyRelative("m_Name");
  21. if (spName == null) continue;
  22. result.Add(spName.stringValue);
  23. }
  24. s_Sb.Length = 0;
  25. if (result.Count == 0)
  26. {
  27. s_Sb.Append("Nothing");
  28. }
  29. else
  30. {
  31. result.Aggregate(s_Sb, (a, b) => s_Sb.AppendFormat("{0}, ", b));
  32. s_Sb.Length -= 2;
  33. }
  34. return s_Sb.ToString();
  35. }
  36. public static void DrawAnimatableProperties(SerializedProperty sp, Material[] mats)
  37. {
  38. bool isClicked;
  39. using (new EditorGUILayout.HorizontalScope(GUILayout.ExpandWidth(false)))
  40. {
  41. var r = EditorGUI.PrefixLabel(EditorGUILayout.GetControlRect(true), new GUIContent(sp.displayName, sp.tooltip));
  42. var text = sp.hasMultipleDifferentValues ? "-" : CollectActiveNames(sp, s_ActiveNames);
  43. isClicked = GUI.Button(r, text, EditorStyles.popup);
  44. }
  45. if (!isClicked) return;
  46. var gm = new GenericMenu();
  47. gm.AddItem(new GUIContent("Nothing"), s_ActiveNames.Count == 0, () =>
  48. {
  49. sp.ClearArray();
  50. sp.serializedObject.ApplyModifiedProperties();
  51. });
  52. if (!sp.hasMultipleDifferentValues)
  53. {
  54. for (var i = 0; i < sp.arraySize; i++)
  55. {
  56. var p = sp.GetArrayElementAtIndex(i);
  57. var name = p.FindPropertyRelative("m_Name").stringValue;
  58. var type = (ShaderPropertyType) p.FindPropertyRelative("m_Type").intValue;
  59. AddMenu(gm, sp, new AnimatedPropertiesEditor {_name = name, _type = type}, false);
  60. }
  61. }
  62. s_Names.Clear();
  63. foreach (var mat in mats)
  64. {
  65. if (!mat || !mat.shader) continue;
  66. for (var i = 0; i < ShaderUtil.GetPropertyCount(mat.shader); i++)
  67. {
  68. var pName = ShaderUtil.GetPropertyName(mat.shader, i);
  69. var type = (ShaderPropertyType) ShaderUtil.GetPropertyType(mat.shader, i);
  70. var name = string.Format("{0} ({1})", pName, type);
  71. if (s_Names.Contains(name)) continue;
  72. s_Names.Add(name);
  73. AddMenu(gm, sp, new AnimatedPropertiesEditor {_name = pName, _type = type}, true);
  74. if (type != ShaderPropertyType.Texture) continue;
  75. AddMenu(gm, sp, new AnimatedPropertiesEditor {_name = pName + "_ST", _type = ShaderPropertyType.Vector}, true);
  76. AddMenu(gm, sp, new AnimatedPropertiesEditor {_name = pName + "_HDR", _type = ShaderPropertyType.Vector}, true);
  77. AddMenu(gm, sp, new AnimatedPropertiesEditor {_name = pName + "_TexelSize", _type = ShaderPropertyType.Vector}, true);
  78. }
  79. }
  80. gm.ShowAsContext();
  81. }
  82. private static void AddMenu(GenericMenu menu, SerializedProperty sp, AnimatedPropertiesEditor property, bool add)
  83. {
  84. if (add && s_ActiveNames.Contains(property._name)) return;
  85. menu.AddItem(new GUIContent(string.Format("{0} ({1})", property._name, property._type)), s_ActiveNames.Contains(property._name), () =>
  86. {
  87. var index = s_ActiveNames.IndexOf(property._name);
  88. if (0 <= index)
  89. {
  90. sp.DeleteArrayElementAtIndex(index);
  91. }
  92. else
  93. {
  94. sp.InsertArrayElementAtIndex(sp.arraySize);
  95. var p = sp.GetArrayElementAtIndex(sp.arraySize - 1);
  96. p.FindPropertyRelative("m_Name").stringValue = property._name;
  97. p.FindPropertyRelative("m_Type").intValue = (int) property._type;
  98. }
  99. sp.serializedObject.ApplyModifiedProperties();
  100. });
  101. }
  102. }
  103. }