ControllerTransitionDrawer.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. #if UNITY_EDITOR && UNITY_IMGUI
  3. using UnityEditor;
  4. using UnityEditor.Animations;
  5. using UnityEngine;
  6. namespace Animancer.Editor
  7. {
  8. /// <inheritdoc/>
  9. /// https://kybernetik.com.au/animancer/api/Animancer.Editor/ControllerTransitionDrawer
  10. [CustomPropertyDrawer(typeof(ControllerTransition<>), true)]
  11. [CustomPropertyDrawer(typeof(ControllerTransition), true)]
  12. public class ControllerTransitionDrawer : TransitionDrawer
  13. {
  14. /************************************************************************************************************************/
  15. private readonly string[] Parameters;
  16. private readonly string[] ParameterPropertySuffixes;
  17. /************************************************************************************************************************/
  18. /// <summary>Creates a new <see cref="ControllerTransitionDrawer"/> without any parameters.</summary>
  19. public ControllerTransitionDrawer()
  20. : base(ControllerTransition.ControllerFieldName)
  21. { }
  22. /// <summary>Creates a new <see cref="ControllerTransitionDrawer"/> and sets the <see cref="Parameters"/>.</summary>
  23. public ControllerTransitionDrawer(params string[] parameters)
  24. : base(ControllerTransition.ControllerFieldName)
  25. {
  26. Parameters = parameters;
  27. if (parameters == null)
  28. return;
  29. ParameterPropertySuffixes = new string[parameters.Length];
  30. for (int i = 0; i < ParameterPropertySuffixes.Length; i++)
  31. {
  32. ParameterPropertySuffixes[i] = "." + parameters[i];
  33. }
  34. }
  35. /************************************************************************************************************************/
  36. /// <inheritdoc/>
  37. protected override void DoChildPropertyGUI(
  38. ref Rect area,
  39. SerializedProperty rootProperty,
  40. SerializedProperty property,
  41. GUIContent label)
  42. {
  43. var path = property.propertyPath;
  44. if (ParameterPropertySuffixes != null)
  45. {
  46. var controllerProperty = rootProperty.FindPropertyRelative(MainPropertyName);
  47. if (controllerProperty.objectReferenceValue is AnimatorController controller)
  48. {
  49. for (int i = 0; i < ParameterPropertySuffixes.Length; i++)
  50. {
  51. if (path.EndsWith(ParameterPropertySuffixes[i]))
  52. {
  53. area.height = AnimancerGUI.LineHeight;
  54. DoParameterGUI(area, controller, property);
  55. return;
  56. }
  57. }
  58. }
  59. }
  60. EditorGUI.BeginChangeCheck();
  61. base.DoChildPropertyGUI(ref area, rootProperty, property, label);
  62. // When the controller changes, validate all parameters.
  63. if (EditorGUI.EndChangeCheck() &&
  64. Parameters != null &&
  65. path.EndsWith(MainPropertyPathSuffix))
  66. {
  67. if (property.objectReferenceValue is AnimatorController controller)
  68. {
  69. for (int i = 0; i < Parameters.Length; i++)
  70. {
  71. property = rootProperty.FindPropertyRelative(Parameters[i]);
  72. var parameterName = property.stringValue;
  73. // If a parameter is missing, assign it to the first float parameter.
  74. if (!HasFloatParameter(controller, parameterName))
  75. {
  76. parameterName = GetFirstFloatParameterName(controller);
  77. if (!string.IsNullOrEmpty(parameterName))
  78. property.stringValue = parameterName;
  79. }
  80. }
  81. }
  82. }
  83. }
  84. /************************************************************************************************************************/
  85. /// <summary>Draws a dropdown menu to select the name of a parameter in the `controller`.</summary>
  86. protected void DoParameterGUI(Rect area, AnimatorController controller, SerializedProperty property)
  87. {
  88. var parameterName = property.stringValue;
  89. var parameters = controller.parameters;
  90. using (var label = PooledGUIContent.Acquire(property))
  91. {
  92. var propertyLabel = EditorGUI.BeginProperty(area, label, property);
  93. var xMax = area.xMax;
  94. area.width = EditorGUIUtility.labelWidth;
  95. EditorGUI.PrefixLabel(area, propertyLabel);
  96. area.x += area.width;
  97. area.xMax = xMax;
  98. }
  99. var color = GUI.color;
  100. if (!HasFloatParameter(controller, parameterName))
  101. GUI.color = AnimancerGUI.ErrorFieldColor;
  102. using (var label = PooledGUIContent.Acquire(parameterName))
  103. {
  104. if (EditorGUI.DropdownButton(area, label, FocusType.Passive))
  105. {
  106. property = property.Copy();
  107. var menu = new GenericMenu();
  108. for (int i = 0; i < parameters.Length; i++)
  109. {
  110. var parameter = parameters[i];
  111. Serialization.AddPropertyModifierFunction(menu, property, parameter.name,
  112. parameter.type == AnimatorControllerParameterType.Float,
  113. (targetProperty) =>
  114. {
  115. targetProperty.stringValue = parameter.name;
  116. });
  117. }
  118. if (menu.GetItemCount() == 0)
  119. menu.AddDisabledItem(new("No Parameters"));
  120. menu.ShowAsContext();
  121. }
  122. }
  123. GUI.color = color;
  124. EditorGUI.EndProperty();
  125. }
  126. /************************************************************************************************************************/
  127. private static bool HasFloatParameter(AnimatorController controller, string name)
  128. {
  129. if (string.IsNullOrEmpty(name))
  130. return false;
  131. var parameters = controller.parameters;
  132. for (int i = 0; i < parameters.Length; i++)
  133. {
  134. var parameter = parameters[i];
  135. if (parameter.type == AnimatorControllerParameterType.Float &&
  136. parameter.name == name)
  137. {
  138. return true;
  139. }
  140. }
  141. return false;
  142. }
  143. /************************************************************************************************************************/
  144. private static string GetFirstFloatParameterName(AnimatorController controller)
  145. {
  146. var parameters = controller.parameters;
  147. for (int i = 0; i < parameters.Length; i++)
  148. {
  149. var parameter = parameters[i];
  150. if (parameter.type == AnimatorControllerParameterType.Float)
  151. {
  152. return parameter.name;
  153. }
  154. }
  155. return "";
  156. }
  157. /************************************************************************************************************************/
  158. }
  159. }
  160. #endif