using System; using System.Collections.Generic; using UnityEngine; using UnityEditor; using UnityEngine.UIElements; using YKMoon; namespace YKMoonEditor { [CustomPropertyDrawer(typeof(YKAnimatorPresetStyleSelectorAttribute))] public class YKAnimatorPresetStyleSelectorAttributeDrawer : PropertyDrawer { public Texture2D icon { get { if(m_icon == null) { m_icon = PackageUtility.LoadIcon("AnimatorControllerPresetIcon.png"); } return m_icon; } } private Texture2D m_icon; public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { EditorGUI.BeginProperty(position, label, property); if(property.propertyType != SerializedPropertyType.String) { EditorGUI.LabelField(position, "ERROR:", "May only apply to type string"); EditorGUI.EndProperty(); return; } var styleNames = GetStyleNames(); int index = styleNames.FindIndex(0, styleNames.Count, style => style.text.Equals(property.stringValue)); index = EditorGUI.Popup(position, label, index, styleNames.ToArray()); if(index >= 0) { property.stringValue = styleNames[index].text; } else { property.stringValue = styleNames[0].text; } EditorGUI.EndProperty(); } private static YKAnimatorPresetSetting GetSettings() { return YKAnimatorPresetSettingManager.GetSettings(); } private List GetStyleNames() { List result = new List(); var settings = GetSettings(); foreach(var style in settings.styles) { result.Add(new GUIContent(style.name, icon)); } return result; } } }