CCDSolver2DEditor.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using UnityEngine;
  2. using UnityEngine.U2D.IK;
  3. namespace UnityEditor.U2D.IK
  4. {
  5. /// <summary>
  6. /// Custom Inspector for CCDSolver2D.
  7. /// </summary>
  8. [CustomEditor(typeof(CCDSolver2D))]
  9. [CanEditMultipleObjects]
  10. public class CCDSolver2DEditor : 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. public static readonly GUIContent velocityLabel = new GUIContent("Velocity", "How fast the chain elements rotate to the effector per iteration");
  20. }
  21. private SerializedProperty m_TargetProperty;
  22. private SerializedProperty m_EffectorProperty;
  23. private SerializedProperty m_TransformCountProperty;
  24. private SerializedProperty m_IterationsProperty;
  25. private SerializedProperty m_ToleranceProperty;
  26. private SerializedProperty m_VelocityProperty;
  27. private CCDSolver2D m_Solver;
  28. private void OnEnable()
  29. {
  30. m_Solver = target as CCDSolver2D;
  31. var chainProperty = serializedObject.FindProperty("m_Chain");
  32. m_TargetProperty = chainProperty.FindPropertyRelative("m_TargetTransform");
  33. m_EffectorProperty = chainProperty.FindPropertyRelative("m_EffectorTransform");
  34. m_TransformCountProperty = chainProperty.FindPropertyRelative("m_TransformCount");
  35. m_IterationsProperty = serializedObject.FindProperty("m_Iterations");
  36. m_ToleranceProperty = serializedObject.FindProperty("m_Tolerance");
  37. m_VelocityProperty = serializedObject.FindProperty("m_Velocity");
  38. }
  39. /// <summary>
  40. /// Custom Inspector OnInspectorGUI override.
  41. /// </summary>
  42. public override void OnInspectorGUI()
  43. {
  44. IKChain2D chain = m_Solver.GetChain(0);
  45. serializedObject.Update();
  46. EditorGUILayout.PropertyField(m_EffectorProperty, Contents.effectorLabel);
  47. EditorGUILayout.PropertyField(m_TargetProperty, Contents.targetLabel);
  48. EditorGUILayout.IntSlider(m_TransformCountProperty, 0, IKUtility.GetMaxChainCount(chain), Contents.chainLengthLabel);
  49. EditorGUILayout.PropertyField(m_IterationsProperty, Contents.iterationsLabel);
  50. EditorGUILayout.PropertyField(m_ToleranceProperty, Contents.toleranceLabel);
  51. EditorGUILayout.PropertyField(m_VelocityProperty, Contents.velocityLabel);
  52. DrawCommonSolverInspector();
  53. serializedObject.ApplyModifiedProperties();
  54. }
  55. }
  56. }