FabrikSolver2DEditor.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using UnityEngine;
  2. using UnityEngine.U2D.IK;
  3. namespace UnityEditor.U2D.IK
  4. {
  5. /// <summary>
  6. /// Custom Inspector for FabrikSolver2D.
  7. /// </summary>
  8. [CustomEditor(typeof(FabrikSolver2D))]
  9. [CanEditMultipleObjects]
  10. public class FabrikSolver2DEditor : Solver2DEditor
  11. {
  12. private static class Contents
  13. {
  14. public static readonly GUIContent effectorLabel = new GUIContent("Effector", "The last Transform of a hierarchy constrained by the target");
  15. public static readonly GUIContent targetLabel = new GUIContent("Target", "Transfrom which the effector will follow");
  16. public static readonly GUIContent chainLengthLabel = new GUIContent("Chain Length", "Number of Transforms handled by the IK");
  17. public static readonly GUIContent iterationsLabel = new GUIContent("Iterations", "Number of iterations the IK solver is run per frame");
  18. public static readonly GUIContent toleranceLabel = new GUIContent("Tolerance", "How close the target is to the goal to be considered as successful");
  19. }
  20. private SerializedProperty m_TargetProperty;
  21. private SerializedProperty m_EffectorProperty;
  22. private SerializedProperty m_TransformCountProperty;
  23. private SerializedProperty m_IterationsProperty;
  24. private SerializedProperty m_ToleranceProperty;
  25. private FabrikSolver2D m_Solver;
  26. private void OnEnable()
  27. {
  28. m_Solver = target as FabrikSolver2D;
  29. var chainProperty = serializedObject.FindProperty("m_Chain");
  30. m_TargetProperty = chainProperty.FindPropertyRelative("m_TargetTransform");
  31. m_EffectorProperty = chainProperty.FindPropertyRelative("m_EffectorTransform");
  32. m_TransformCountProperty = chainProperty.FindPropertyRelative("m_TransformCount");
  33. m_IterationsProperty = serializedObject.FindProperty("m_Iterations");
  34. m_ToleranceProperty = serializedObject.FindProperty("m_Tolerance");
  35. }
  36. /// <summary>
  37. /// Custom Inspector OnInspectorGUI override.
  38. /// </summary>
  39. public override void OnInspectorGUI()
  40. {
  41. IKChain2D chain = m_Solver.GetChain(0);
  42. serializedObject.Update();
  43. EditorGUILayout.PropertyField(m_EffectorProperty, Contents.effectorLabel);
  44. EditorGUILayout.PropertyField(m_TargetProperty, Contents.targetLabel);
  45. EditorGUILayout.IntSlider(m_TransformCountProperty, 0, IKUtility.GetMaxChainCount(chain), Contents.chainLengthLabel);
  46. EditorGUILayout.PropertyField(m_IterationsProperty, Contents.iterationsLabel);
  47. EditorGUILayout.PropertyField(m_ToleranceProperty, Contents.toleranceLabel);
  48. DrawCommonSolverInspector();
  49. serializedObject.ApplyModifiedProperties();
  50. }
  51. }
  52. }