canto/Assets/Scripts/Editor/Drawers/AnimatorControllerParameterNamePropertyDrawer.cs
2025-10-02 15:28:03 -04:00

53 lines
1.9 KiB
C#

using KitsuneCafe.Sys.Attributes;
using UnityEditor;
using UnityEditor.Animations;
using UnityEngine;
using UnityForge.PropertyDrawers.Editor;
namespace KitsuneCafe.Editor.PropertyDrawers
{
[CustomPropertyDrawer(typeof(AnimatorControllerParameterNameAttribute))]
public class AnimatorControllerParameterNameDrawer : AnimatorControllerPropertyDrawer<AnimatorControllerParameterNameAttribute>
{
public AnimatorControllerParameterNameDrawer() : base(SerializedPropertyType.String)
{
}
protected override string GetPropertyPath(AnimatorControllerParameterNameAttribute attribute)
{
return attribute.AnimatorControllerField;
}
protected override void DrawAnimatorControllerProperty(Rect position, SerializedProperty property, AnimatorController animatorController)
{
var propertyStringValue = property.hasMultipleDifferentValues ? "-" : property.stringValue;
var content = string.IsNullOrEmpty(propertyStringValue) ? new GUIContent("<None>") : new GUIContent(propertyStringValue);
if (GUI.Button(position, content, EditorStyles.popup))
{
LayerSelector(property, animatorController);
}
}
private void LayerSelector(SerializedProperty property, AnimatorController animatorController)
{
var menu = new GenericMenu();
var parameterAttribute = (AnimatorControllerParameterNameAttribute)attribute;
var parameterType = parameterAttribute.ParameterType;
foreach (var parameter in animatorController.parameters)
{
if (parameter.type == parameterType)
{
var parameterName = parameter.name;
menu.AddItem(new GUIContent(parameterName),
parameterName == property.stringValue,
StringPropertyPair.HandlePairObjectSelect,
new StringPropertyPair(parameterName, property));
}
}
menu.ShowAsContext();
}
}
}