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

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 {
}

	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;

注意，由于句柄值在世界空间中，因此我们需要使用InverseTransformPoint方法将其转换回本地空间。 另外，当点改变时，我们只需要这样做， 我们可以使用EditorGUI.BeginChangeCheck和EditorGUI.EndChangeCheck。

		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);
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120