You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.6 KiB

5 years ago
  1. using UnityEditor;
  2. using UnityEngine;
  3. [CustomEditor(typeof(BezierCurve))]
  4. public class BezierCurveInspector : Editor {
  5. private const int lineSteps = 10;
  6. private const float directionScale = 0.5f;
  7. private BezierCurve curve;
  8. private Transform handleTransform;
  9. private Quaternion handleRotation;
  10. private void OnSceneGUI () {
  11. curve = target as BezierCurve;
  12. handleTransform = curve.transform;
  13. handleRotation = Tools.pivotRotation == PivotRotation.Local ?
  14. handleTransform.rotation : Quaternion.identity;
  15. Vector3 p0 = ShowPoint(0);
  16. Vector3 p1 = ShowPoint(1);
  17. Vector3 p2 = ShowPoint(2);
  18. Vector3 p3 = ShowPoint(3);
  19. Handles.color = Color.gray;
  20. Handles.DrawLine(p0, p1);
  21. Handles.DrawLine(p2, p3);
  22. ShowDirections();
  23. Handles.DrawBezier(p0, p3, p1, p2, Color.white, null, 2f);
  24. }
  25. private void ShowDirections () {
  26. Handles.color = Color.green;
  27. Vector3 point = curve.GetPoint(0f);
  28. Handles.DrawLine(point, point + curve.GetDirection(0f) * directionScale);
  29. for (int i = 1; i <= lineSteps; i++) {
  30. point = curve.GetPoint(i / (float)lineSteps);
  31. Handles.DrawLine(point, point + curve.GetDirection(i / (float)lineSteps) * directionScale);
  32. }
  33. }
  34. private Vector3 ShowPoint (int index) {
  35. Vector3 point = handleTransform.TransformPoint(curve.points[index]);
  36. EditorGUI.BeginChangeCheck();
  37. point = Handles.DoPositionHandle(point, handleRotation);
  38. if (EditorGUI.EndChangeCheck()) {
  39. Undo.RecordObject(curve, "Move Point");
  40. EditorUtility.SetDirty(curve);
  41. curve.points[index] = handleTransform.InverseTransformPoint(point);
  42. }
  43. return point;
  44. }
  45. }