我创建了使用手柄绘制的贝塞尔曲线。当我旋转容器游戏对象时,曲线会旋转,但是锚点和控制点手柄不会旋转。我怀疑问题来自我的PathEditor类中的以下代码行。如何解决此轮换问题?

如果需要,请检查here链接到PathEditor的其他类,我决定不将它们放在此处以保持问题整洁。

PathEditor Draw()方法中的可疑行:

for (int i = 0; i < path.NumPoints; i++)
    {
        Vector2 newPos = Handles.FreeMoveHandle(path[i], Quaternion.identity, .1f, Vector2.zero, Handles.CylinderHandleCap);
        if (path[i] != newPos)
        {
            Undo.RecordObject(creator, "Move point");
            path.MovePoint(i, newPos);
        }
    }


路径编辑器:

[CustomEditor(typeof(PathCreator))]
public class PathEditor : Editor {

PathCreator creator;
Path path;

public override void OnInspectorGUI()
{
    base.OnInspectorGUI();
    EditorGUI.BeginChangeCheck();

    bool continuousControlPoints = GUILayout.Toggle(path.isContinuous, "Set Continuous Control Points");
    if (continuousControlPoints != path.isContinuous)
    {
        Undo.RecordObject(creator, "Toggle set continuous controls");
        path.isContinuous = continuousControlPoints;
    }

    if (EditorGUI.EndChangeCheck())
    {
        SceneView.RepaintAll();
    }
}

void OnSceneGUI()
{
    Input();
    Draw();
}

void Input()
 {
    Event guiEvent = Event.current;
    Vector2 mousePos = HandleUtility.GUIPointToWorldRay(guiEvent.mousePosition).origin;

    if (guiEvent.type == EventType.MouseDown && guiEvent.button == 0 && guiEvent.shift)
    {
        Undo.RecordObject(creator, "Add segment");
        path.AddSegment(mousePos);
    }
}

void Draw()
{

     Transform handleTransform = creator.transform;
    Quaternion handleRotation = Tools.pivotRotation == PivotRotation.Local ?
        handleTransform.rotation : Quaternion.identity;

    for (int i = 0; i < path.NumSegments; i++)
    {
        Vector2[] points = path.GetPointsInSegment(i);
        Handles.color = Color.black;

        Vector2 p0 = handleTransform.TransformPoint(points[0]);
        Vector2 p1 = handleTransform.TransformPoint(points[1]);
        Vector2 p2 = handleTransform.TransformPoint(points[2]);
        Vector2 p3 = handleTransform.TransformPoint(points[3]);

        Handles.DrawLine(p1, p0);
        Handles.DrawLine(p2, p3);
        Handles.DrawBezier(p0, p3, p1, p2, Color.green, null, 2);

    }


    Handles.color = Color.red;
    for (int i = 0; i < path.NumPoints; i++)
    {
        Vector2 newPos = Handles.FreeMoveHandle(path[i], Quaternion.identity, .1f, Vector2.zero, Handles.CylinderHandleCap);
        if (path[i] != newPos)
        {
            Undo.RecordObject(creator, "Move point");
            path.MovePoint(i, newPos);
        }
    }
}

void OnEnable()
{
    creator = (PathCreator)target;
    if (creator.path == null)
    {
        creator.CreatePath();
    }
    path = creator.path;
}
}

最佳答案

好像您将点值存储在世界坐标中。变换对象时,这些点并没有意识到这一点,而是继续在同一位置上绘制。

而是将点存储在局部坐标中,并在每次绘制时应用矩阵变换。

编辑执行以下步骤以使其正常工作:


Path中的所有点都视为局部坐标(即相对于gameObject本身)
PathEditor中,无论何时要设置从路径到场景的值,都应使用目标的transform组件应用变换。例如,在Input方法中,您应该像这样校正鼠标位置:

mousePos = creator.transform.InverseTransformPoint(mousePos);


在您的Draw方法中,实际上是在计算正确的handleTrasform矩阵和四元数handleRotation,但是您没有应用它们:

Vector2 newPos = Handles.FreeMoveHandle(path[i], Quaternion.identity, .1f, Vector2.zero, Handles.CylinderHandleCap);


应该:

var pos = handleTransform.InverseTransformPoint(path[i]);
Vector2 newPos = Handles.FreeMoveHandle(pos, handleRotation, .1f, Vector2.zero, Handles.CylinderHandleCap);


您也可以改为在方法开始时设置Handles.matrix,并且刻度将自动处理。

Handles.matrix = creator.transform.localToWorldMatrix;

09-05 21:50