Solver2DEditor.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using UnityEngine;
  2. using UnityEngine.U2D.IK;
  3. namespace UnityEditor.U2D.IK
  4. {
  5. /// <summary>
  6. /// Custom Inspector for Solver2D.
  7. /// </summary>
  8. [CustomEditor(typeof(Solver2D))]
  9. [CanEditMultipleObjects]
  10. public class Solver2DEditor : Editor
  11. {
  12. private static class Contents
  13. {
  14. public static readonly GUIContent constrainRotationLabel = new GUIContent("Constrain Rotation", "Set Effector's rotation to Target");
  15. public static readonly GUIContent solveFromDefaultPoseLabel = new GUIContent("Solve from Default Pose", "Restore transform's rotation to default value before solving the IK");
  16. public static readonly GUIContent weightLabel = new GUIContent("Weight", "Blend between Forward and Inverse Kinematics");
  17. public static readonly string restoreDefaultPoseString = "Restore Default Pose";
  18. public static readonly string createTargetString = "Create Target";
  19. }
  20. private SerializedProperty m_ConstrainRotationProperty;
  21. private SerializedProperty m_SolveFromDefaultPoseProperty;
  22. private SerializedProperty m_WeightProperty;
  23. private SerializedProperty m_SolverColorProperty;
  24. private void SetupProperties()
  25. {
  26. if(m_ConstrainRotationProperty == null || m_SolveFromDefaultPoseProperty == null || m_WeightProperty == null)
  27. {
  28. m_ConstrainRotationProperty = serializedObject.FindProperty("m_ConstrainRotation");
  29. m_SolveFromDefaultPoseProperty = serializedObject.FindProperty("m_SolveFromDefaultPose");
  30. m_WeightProperty = serializedObject.FindProperty("m_Weight");
  31. }
  32. }
  33. /// <summary>
  34. /// Custom Inspector GUI for Solver2D.
  35. /// </summary>
  36. protected void DrawCommonSolverInspector()
  37. {
  38. SetupProperties();
  39. EditorGUILayout.PropertyField(m_ConstrainRotationProperty, Contents.constrainRotationLabel);
  40. EditorGUILayout.PropertyField(m_SolveFromDefaultPoseProperty, Contents.solveFromDefaultPoseLabel);
  41. EditorGUILayout.PropertyField(m_WeightProperty, Contents.weightLabel);
  42. EditorGUILayout.Space();
  43. EditorGUILayout.BeginHorizontal();
  44. GUILayout.FlexibleSpace();
  45. EditorGUI.BeginDisabledGroup(!EnableCreateTarget());
  46. DoCreateTargetButton();
  47. EditorGUI.EndDisabledGroup();
  48. EditorGUI.BeginDisabledGroup(!EnableRestoreDefaultPose());
  49. DoRestoreDefaultPoseButton();
  50. EditorGUI.EndDisabledGroup();
  51. GUILayout.FlexibleSpace();
  52. EditorGUILayout.EndHorizontal();
  53. EditorGUILayout.Space();
  54. }
  55. private bool EnableRestoreDefaultPose()
  56. {
  57. foreach (var l_target in targets)
  58. {
  59. var solver = l_target as Solver2D;
  60. if (!solver.isValid || IKEditorManager.instance.FindManager(solver) == null)
  61. continue;
  62. return true;
  63. }
  64. return false;
  65. }
  66. private bool EnableCreateTarget()
  67. {
  68. foreach (var l_target in targets)
  69. {
  70. var solver = l_target as Solver2D;
  71. if (!solver.isValid)
  72. continue;
  73. for(int i = 0; i < solver.chainCount; ++i)
  74. {
  75. var chain = solver.GetChain(i);
  76. if(chain.target == null)
  77. return true;
  78. }
  79. }
  80. return false;
  81. }
  82. private void DoRestoreDefaultPoseButton()
  83. {
  84. if (GUILayout.Button(Contents.restoreDefaultPoseString, GUILayout.MaxWidth(150f)))
  85. {
  86. foreach (var l_target in targets)
  87. {
  88. var solver = l_target as Solver2D;
  89. if (!solver.isValid)
  90. continue;
  91. IKEditorManager.instance.Record(solver, Contents.restoreDefaultPoseString);
  92. for(int i = 0; i < solver.chainCount; ++i)
  93. {
  94. var chain = solver.GetChain(i);
  95. chain.RestoreDefaultPose(solver.constrainRotation);
  96. if(chain.target)
  97. {
  98. chain.target.position = chain.effector.position;
  99. chain.target.rotation = chain.effector.rotation;
  100. }
  101. }
  102. IKEditorManager.instance.UpdateSolverImmediate(solver, true);
  103. }
  104. }
  105. }
  106. private void DoCreateTargetButton()
  107. {
  108. if (GUILayout.Button(Contents.createTargetString, GUILayout.MaxWidth(125f)))
  109. {
  110. foreach (var l_target in targets)
  111. {
  112. var solver = l_target as Solver2D;
  113. if (!solver.isValid)
  114. continue;
  115. for(int i = 0; i < solver.chainCount; ++i)
  116. {
  117. var chain = solver.GetChain(i);
  118. if(chain.target == null)
  119. {
  120. Undo.RegisterCompleteObjectUndo(solver, Contents.createTargetString);
  121. chain.target = new GameObject(GameObjectUtility.GetUniqueNameForSibling(solver.transform, solver.name + "_Target")).transform;
  122. chain.target.SetParent(solver.transform);
  123. chain.target.position = chain.effector.position;
  124. chain.target.rotation = chain.effector.rotation;
  125. Undo.RegisterCreatedObjectUndo(chain.target.gameObject, Contents.createTargetString);
  126. }
  127. }
  128. }
  129. }
  130. }
  131. }
  132. }