BipedIKSolversInspector.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4. namespace RootMotion.FinalIK {
  5. /*
  6. * Custom inspector and scene view tools for Biped IK Solvers.
  7. * */
  8. public class BipedIKSolversInspector: IKSolverInspector {
  9. /*
  10. * Returns all solvers SeiralizedProperties
  11. * */
  12. public static SerializedProperty[] FindProperties(SerializedProperty prop) {
  13. SerializedProperty[] props = new SerializedProperty[8] {
  14. prop.FindPropertyRelative("leftFoot"),
  15. prop.FindPropertyRelative("rightFoot"),
  16. prop.FindPropertyRelative("leftHand"),
  17. prop.FindPropertyRelative("rightHand"),
  18. prop.FindPropertyRelative("spine"),
  19. prop.FindPropertyRelative("aim"),
  20. prop.FindPropertyRelative("lookAt"),
  21. prop.FindPropertyRelative("pelvis"),
  22. };
  23. return props;
  24. }
  25. /*
  26. * Draws the custom inspector for BipedIK.Solvers
  27. * */
  28. public static void AddInspector(SerializedProperty prop, SerializedProperty[] props) {
  29. EditorGUILayout.PropertyField(prop);
  30. if (prop.isExpanded) {
  31. for (int i = 0; i < props.Length; i++) {
  32. BeginProperty(props[i]);
  33. if (props[i].isExpanded) {
  34. if (i <= 3) IKSolverLimbInspector.AddInspector(props[i], false, false);
  35. else if (i == 4) IKSolverHeuristicInspector.AddInspector(props[i], false, false);
  36. else if (i == 5) IKSolverAimInspector.AddInspector(props[i], false);
  37. else if (i == 6) IKSolverLookAtInspector.AddInspector(props[i], false, false);
  38. else if (i == 7) ConstraintsInspector.AddInspector(props[i]);
  39. }
  40. EndProperty(props[i]);
  41. }
  42. }
  43. }
  44. /*
  45. * Draws the scene view helpers for BipedIK.Solvers
  46. * */
  47. public static void AddScene(BipedIKSolvers solvers, ref int selected) {
  48. // Draw limbs
  49. for (int i = 0; i < solvers.limbs.Length; i++) {
  50. IKSolverLimbInspector.AddScene(solvers.limbs[i] as IKSolverLimb, GetSolverColor(i), selected == i);
  51. }
  52. // Draw spine
  53. IKSolverHeuristicInspector.AddScene(solvers.spine, GetSolverColor(4), selected == 4);
  54. // Draw look at
  55. IKSolverLookAtInspector.AddScene(solvers.lookAt, GetSolverColor(5), selected == 5);
  56. // Draw aim
  57. IKSolverAimInspector.AddScene(solvers.aim, GetSolverColor(6), selected == 6);
  58. // Draw constraints
  59. ConstraintsInspector.AddScene(solvers.pelvis, GetSolverColor(7), selected == 7);
  60. // Selecting solvers
  61. if (Application.isPlaying) {
  62. for (int i = 0; i < solvers.ikSolvers.Length; i++) {
  63. Handles.color = GetSolverColor(i);
  64. if (solvers.ikSolvers[i].GetIKPositionWeight() > 0 && selected != i && solvers.ikSolvers[i].initiated) {
  65. if (Inspector.DotButton(solvers.ikSolvers[i].GetIKPosition(), Quaternion.identity, GetHandleSize(solvers.ikSolvers[i].GetIKPosition()), GetHandleSize(solvers.ikSolvers[i].GetIKPosition()))) selected = i;
  66. }
  67. }
  68. if ((solvers.pelvis.positionWeight > 0 || solvers.pelvis.rotationWeight > 0) && selected != solvers.ikSolvers.Length) {
  69. Handles.color = GetSolverColor(7);
  70. if (Inspector.DotButton(solvers.pelvis.position, Quaternion.identity, GetHandleSize(solvers.pelvis.position), GetHandleSize(solvers.pelvis.position))) selected = solvers.ikSolvers.Length;
  71. }
  72. }
  73. }
  74. /*
  75. * Gets the color of the solver at index.
  76. * */
  77. private static Color GetSolverColor(int index) {
  78. if (index == 0 || index == 2) return new Color(0f, 0.8f, 1f, 1f); // Left limb
  79. if (index == 1 || index == 3) return new Color(0.3f, 1f, 0.3f, 1f); // Right limb
  80. if (index == 4) return new Color(1f, 0.5f, 0.5f, 1f); // Spine
  81. if (index == 5) return new Color(0.2f, 0.5f, 1f, 1f); // Look At
  82. if (index == 6) return new Color(1f, 0f, 0.5f, 1f); // Aim
  83. if (index == 7) return new Color(0.9f, 0.9f, 0.9f, 1f); // Pelvis
  84. return Color.white;
  85. }
  86. /*
  87. * Begin property box
  88. * */
  89. private static void BeginProperty(SerializedProperty prop) {
  90. EditorGUI.indentLevel = 1;
  91. EditorGUILayout.BeginVertical("Box");
  92. EditorGUILayout.PropertyField(prop);
  93. }
  94. /*
  95. * End Property box
  96. * */
  97. private static void EndProperty(SerializedProperty prop) {
  98. EditorGUILayout.EndVertical();
  99. if (prop.isExpanded) EditorGUILayout.Space();
  100. EditorGUI.indentLevel = 1;
  101. }
  102. }
  103. }