RotationLimitAngleInspector.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using UnityEditor;
  2. using UnityEngine;
  3. using System.Collections;
  4. namespace RootMotion.FinalIK
  5. {
  6. /*
  7. * Custom inspector for RotationLimitAngle.
  8. * */
  9. [CustomEditor(typeof(RotationLimitAngle))]
  10. [CanEditMultipleObjects]
  11. public class RotationLimitAngleInspector : RotationLimitInspector
  12. {
  13. private RotationLimitAngle script { get { return target as RotationLimitAngle; } }
  14. #region Inspector
  15. public override void OnInspectorGUI()
  16. {
  17. GUI.changed = false;
  18. // Draw the default inspector
  19. DrawDefaultInspector();
  20. script.limit = Mathf.Clamp(script.limit, 0, 180);
  21. if (GUI.changed) EditorUtility.SetDirty(script);
  22. }
  23. #endregion Inspector
  24. #region Scene
  25. void OnSceneGUI()
  26. {
  27. // Set defaultLocalRotation so that the initial local rotation will be the zero point for the rotation limit
  28. if (!Application.isPlaying && !script.defaultLocalRotationOverride) script.defaultLocalRotation = script.transform.localRotation;
  29. if (script.axis == Vector3.zero) return;
  30. DrawRotationSphere(script.transform.position);
  31. // Display the main axis
  32. DrawArrow(script.transform.position, Direction(script.axis), colorDefault, "Axis", 0.02f);
  33. Vector3 swing = script.axis.normalized;
  34. // Display limits
  35. lastPoint = script.transform.position;
  36. for (int i = 0; i < 360; i += 2)
  37. {
  38. Quaternion offset = Quaternion.AngleAxis(i, swing);
  39. Quaternion limitedRotation = Quaternion.AngleAxis(script.limit, offset * script.crossAxis);
  40. Vector3 limitedDirection = Direction(limitedRotation * swing);
  41. Handles.color = colorDefaultTransparent;
  42. Vector3 limitPoint = script.transform.position + limitedDirection;
  43. if (i == 0) zeroPoint = limitPoint;
  44. Handles.DrawLine(script.transform.position, limitPoint);
  45. if (i > 0)
  46. {
  47. Handles.color = colorDefault;
  48. Handles.DrawLine(limitPoint, lastPoint);
  49. if (i == 358) Handles.DrawLine(limitPoint, zeroPoint);
  50. }
  51. lastPoint = limitPoint;
  52. }
  53. Handles.color = Color.white;
  54. }
  55. private Vector3 lastPoint, zeroPoint;
  56. /*
  57. * Converting directions from local space to world space
  58. * */
  59. private Vector3 Direction(Vector3 v)
  60. {
  61. if (script.transform.parent == null) return script.defaultLocalRotation * v;
  62. return script.transform.parent.rotation * (script.defaultLocalRotation * v);
  63. }
  64. #endregion Scene
  65. }
  66. }