-
-
Notifications
You must be signed in to change notification settings - Fork 650
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: separate AnimatedPropertiesEditor class
- Loading branch information
Showing
3 changed files
with
125 additions
and
102 deletions.
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
Packages/UIParticle/Scripts/Editor/AnimatedPropertiesEditor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
using UnityEditor; | ||
using UnityEngine; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using ShaderPropertyType = Coffee.UIExtensions.AnimatableProperty.ShaderPropertyType; | ||
|
||
namespace Coffee.UIExtensions | ||
{ | ||
internal class AnimatedPropertiesEditor | ||
{ | ||
static readonly List<string> s_ActiveNames = new List<string>(); | ||
static readonly System.Text.StringBuilder s_Sb = new System.Text.StringBuilder(); | ||
|
||
private string _name; | ||
private ShaderPropertyType _type; | ||
|
||
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 | ||
{ | ||
result.Aggregate(s_Sb, (a, b) => s_Sb.AppendFormat("{0}, ", b)); | ||
s_Sb.Length -= 2; | ||
} | ||
|
||
return s_Sb.ToString(); | ||
} | ||
|
||
public static void DrawAnimatableProperties(SerializedProperty sp, Material mat) | ||
{ | ||
if (!mat || !mat.shader) return; | ||
|
||
bool isClicked; | ||
using (new EditorGUILayout.HorizontalScope(GUILayout.ExpandWidth(false))) | ||
{ | ||
var r = EditorGUI.PrefixLabel(EditorGUILayout.GetControlRect(true), new GUIContent(sp.displayName, sp.tooltip)); | ||
var text = sp.hasMultipleDifferentValues ? "-" : CollectActiveNames(sp, s_ActiveNames); | ||
isClicked = GUI.Button(r, text, EditorStyles.popup); | ||
} | ||
|
||
if (!isClicked) return; | ||
|
||
var gm = new GenericMenu(); | ||
gm.AddItem(new GUIContent("Nothing"), s_ActiveNames.Count == 0, () => | ||
{ | ||
sp.ClearArray(); | ||
sp.serializedObject.ApplyModifiedProperties(); | ||
}); | ||
|
||
|
||
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; | ||
AddMenu(gm, sp, new AnimatedPropertiesEditor {_name = name, _type = type}, false); | ||
} | ||
} | ||
|
||
for (var i = 0; i < ShaderUtil.GetPropertyCount(mat.shader); i++) | ||
{ | ||
var pName = ShaderUtil.GetPropertyName(mat.shader, i); | ||
var type = (ShaderPropertyType) ShaderUtil.GetPropertyType(mat.shader, i); | ||
AddMenu(gm, sp, new AnimatedPropertiesEditor {_name = pName, _type = type}, true); | ||
|
||
if (type != ShaderPropertyType.Texture) continue; | ||
|
||
AddMenu(gm, sp, new AnimatedPropertiesEditor {_name = pName + "_ST", _type = ShaderPropertyType.Vector}, true); | ||
AddMenu(gm, sp, new AnimatedPropertiesEditor {_name = pName + "_HDR", _type = ShaderPropertyType.Vector}, true); | ||
AddMenu(gm, sp, new AnimatedPropertiesEditor {_name = pName + "_TexelSize", _type = ShaderPropertyType.Vector}, true); | ||
} | ||
|
||
gm.ShowAsContext(); | ||
} | ||
|
||
private static void AddMenu(GenericMenu menu, SerializedProperty sp, AnimatedPropertiesEditor property, bool add) | ||
{ | ||
if (add && s_ActiveNames.Contains(property._name)) return; | ||
|
||
menu.AddItem(new GUIContent(string.Format("{0} ({1})", property._name, property._type)), s_ActiveNames.Contains(property._name), () => | ||
{ | ||
var index = s_ActiveNames.IndexOf(property._name); | ||
if (0 <= index) | ||
{ | ||
sp.DeleteArrayElementAtIndex(index); | ||
} | ||
else | ||
{ | ||
sp.InsertArrayElementAtIndex(sp.arraySize); | ||
var p = sp.GetArrayElementAtIndex(sp.arraySize - 1); | ||
p.FindPropertyRelative("m_Name").stringValue = property._name; | ||
p.FindPropertyRelative("m_Type").intValue = (int) property._type; | ||
} | ||
|
||
sp.serializedObject.ApplyModifiedProperties(); | ||
}); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Packages/UIParticle/Scripts/Editor/AnimatedPropertiesEditor.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters