Skip to content

[Unity] 绘制 Capsule Gizmos  #23

Open
@yangruihan

Description

@yangruihan
public static void DrawWireCapsule(Vector3 pos, Quaternion rot, float radius, float height,
    Color color = default(Color))
{
    if (color != default(Color))
        Handles.color = color;

    Matrix4x4 angleMatrix = Matrix4x4.TRS(pos, rot, Handles.matrix.lossyScale);
    using (new Handles.DrawingScope(angleMatrix))
    {
        var pointOffset = (height - (radius * 2)) / 2;

        //draw sideways
        Handles.DrawWireArc(Vector3.up * pointOffset, Vector3.left, Vector3.back, -180, radius);
        Handles.DrawLine(new Vector3(0, pointOffset, -radius), new Vector3(0, -pointOffset, -radius));
        Handles.DrawLine(new Vector3(0, pointOffset, radius), new Vector3(0, -pointOffset, radius));
        Handles.DrawWireArc(Vector3.down * pointOffset, Vector3.left, Vector3.back, 180, radius);
        //draw frontways
        Handles.DrawWireArc(Vector3.up * pointOffset, Vector3.back, Vector3.left, 180, radius);
        Handles.DrawLine(new Vector3(-radius, pointOffset, 0), new Vector3(-radius, -pointOffset, 0));
        Handles.DrawLine(new Vector3(radius, pointOffset, 0), new Vector3(radius, -pointOffset, 0));
        Handles.DrawWireArc(Vector3.down * pointOffset, Vector3.back, Vector3.left, -180, radius);
        //draw center
        Handles.DrawWireDisc(Vector3.up * pointOffset, Vector3.up, radius);
        Handles.DrawWireDisc(Vector3.down * pointOffset, Vector3.up, radius);
    }
}

参考资料:https://forum.unity.com/threads/drawing-capsule-gizmo.354634/

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions