# Unity3D教你制作Bezier和Spine曲线编辑器一

CSDN博客 (源链)

1.创建自定义的编辑器

2.在Scene View窗口中绘制

3.可以在Scene View窗口中编辑

4.利用数学公式创建Bezier曲线

5.可以绘制曲线的移动方向

6.通过组合曲线构建Bezier和Spine曲线

7.支持镜像控制点

8.支持循环样条曲线

9.可以在曲线放置和移动物体

## 线的绘制

```using UnityEngine;

public class Line : MonoBehaviour {

public Vector3 p0, p1;
}```

```using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(Line))]
public class LineInspector : Editor {
}```

OnSceneGUI，这个函数是Unity自带的回调函数，定义如下所示：

```private void OnSceneGUI () {
Line line = target as Line;

Handles.color = Color.white;
Handles.DrawLine(line.p0, line.p1);
}```

```private void OnSceneGUI () {
Line line = target as Line;
Transform handleTransform = line.transform;
Vector3 p0 = handleTransform.TransformPoint(line.p0);
Vector3 p1 = handleTransform.TransformPoint(line.p1);

Handles.color = Color.white;
Handles.DrawLine(p0, p1);
}```

```private void OnSceneGUI () {
Line line = target as Line;
Transform handleTransform = line.transform;
Quaternion handleRotation = handleTransform.rotation;
Vector3 p0 = handleTransform.TransformPoint(line.p0);
Vector3 p1 = handleTransform.TransformPoint(line.p1);

Handles.color = Color.white;
Handles.DrawLine(p0, p1);
Handles.DoPositionHandle(p0, handleRotation);
Handles.DoPositionHandle(p1, handleRotation);
}```

```Quaternion handleRotation = Tools.pivotRotation == PivotRotation.Local ?
handleTransform.rotation : Quaternion.identity;```

```EditorGUI.BeginChangeCheck();
p0 = Handles.DoPositionHandle(p0, handleRotation);
if (EditorGUI.EndChangeCheck()) {
line.p0 = handleTransform.InverseTransformPoint(p0);
}
EditorGUI.BeginChangeCheck();
p1 = Handles.DoPositionHandle(p1, handleRotation);
if (EditorGUI.EndChangeCheck()) {
line.p1 = handleTransform.InverseTransformPoint(p1);
}```

```EditorGUI.BeginChangeCheck();
p0 = Handles.DoPositionHandle(p0, handleRotation);
if (EditorGUI.EndChangeCheck()) {
Undo.RecordObject(line, "Move Point");
EditorUtility.SetDirty(line);
line.p0 = handleTransform.InverseTransformPoint(p0);
}
EditorGUI.BeginChangeCheck();
p1 = Handles.DoPositionHandle(p1, handleRotation);
if (EditorGUI.EndChangeCheck()) {
Undo.RecordObject(line, "Move Point");
EditorUtility.SetDirty(line);
line.p1 = handleTransform.InverseTransformPoint(p1);
}```

|