【Unity】Unity Gizmos扩展(圆弧、扇形、圆形)

本文介绍了一组用于Unity编辑器的Gizmos扩展方法,包括画圆弧线、扇形、空心圆和实心圆的功能。这些扩展通过UnityEditor.Handles类实现,可用于在3D空间中直观展示各种形状,适用于游戏开发和场景编辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【Unity】Unity Gizmos扩展(圆弧、扇形、圆形)

使用 UnityEditor.Handles 为Unity Gizmos 实现的扩展:

  • GizmosX.DrawWireArc() :画圆弧线
  • GizmosX.DrawSolidArc() :画扇形
  • GizmosX.DrawWireDisc() :画空心圆
  • GizmosX.DrawSolidDisc() :画实心圆
// GizmosExtension.cs

namespace UnityEngine
{
    /// <summary>
    /// Gizmos Extensions.
    /// </summary>
    public static class GizmosX
    {
        /// <summary>
        /// Draw a circular arc in 3D space.
        /// </summary>
        /// <param name="center">The center of the circle.</param>
        /// <param name="normal">The normal of the circle.</param>
        /// <param name="from">The direction of the point on the circle circumference, relative to the center, where the arc begins.</param>
        /// <param name="angle"> The angle of the arc, in degrees.</param>
        /// <param name="radius">The radius of the circle Note: Use HandleUtility.GetHandleSize where you might want to have constant screen-sized handles.</param>
        [System.Diagnostics.Conditional("UNITY_EDITOR")]
        public static void DrawWireArc(Vector3 center, Vector3 normal, Vector3 from, float angle, float radius)
        {
#if UNITY_EDITOR
            UnityEditor.Handles.color = Gizmos.color;
            UnityEditor.Handles.DrawWireArc(center, normal, from, angle, radius);
#endif
        }

        /// <summary>
        /// Draw a circular sector (pie piece) in 3D space.
        /// </summary>
        /// <param name="center">The center of the circle.</param>
        /// <param name="normal">The normal of the circle.</param>
        /// <param name="from">The direction of the point on the circle circumference, relative to the center, where the arc begins.</param>
        /// <param name="angle"> The angle of the arc, in degrees.</param>
        /// <param name="radius">The radius of the circle Note: Use HandleUtility.GetHandleSize where you might want to have constant screen-sized handles.</param>
        [System.Diagnostics.Conditional("UNITY_EDITOR")]
        public static void DrawSolidArc(Vector3 center, Vector3 normal, Vector3 from, float angle, float radius)
        {
#if UNITY_EDITOR
            UnityEditor.Handles.color = Gizmos.color;
            UnityEditor.Handles.DrawSolidArc(center, normal, from, angle, radius);
#endif
        }

        /// <summary>
        /// Draw the outline of a flat disc in 3D space.
        /// </summary>
        /// <param name="center">The center of the disc.</param>
        /// <param name="normal">The normal of the disc.</param>
        /// <param name="radius">The radius of the disc.</param>
        [System.Diagnostics.Conditional("UNITY_EDITOR")]
        public static void DrawWireDisc(Vector3 center, Vector3 normal, float radius)
        {
#if UNITY_EDITOR
            UnityEditor.Handles.color = Gizmos.color;
            UnityEditor.Handles.DrawWireDisc(center, normal, radius);
#endif
        }

        /// <summary>
        /// Draw a solid flat disc in 3D space.
        /// Note: Use HandleUtility.GetHandleSize where you might want to have constant screen-sized handles.
        /// </summary>
        /// <param name="center">The center of the disc.</param>
        /// <param name="normal">The normal of the disc.</param>
        /// <param name="radius">The radius of the disc.</param>
        [System.Diagnostics.Conditional("UNITY_EDITOR")]
        public static void DrawSolidDisc(Vector3 center, Vector3 normal, float radius)
        {
#if UNITY_EDITOR
            UnityEditor.Handles.color = Gizmos.color;
            UnityEditor.Handles.DrawSolidDisc(center, normal, radius);
#endif
        }

        /// <summary>
        /// Make a text label positioned in 3D space.
        /// </summary>
        /// <param name="position">Position in 3D space as seen from the current handle camera.</param>
        /// <param name="text">Text to display on the label.</param>
        [System.Diagnostics.Conditional("UNITY_EDITOR")]
        public static void Label(Vector3 position, string text)
        {
#if UNITY_EDITOR
            UnityEditor.Handles.color = Gizmos.color;
            UnityEditor.Handles.Label(position, text);
#endif
        }

        /// <summary>
        /// Make a text label positioned in 3D space.
        ///  Note: Use HandleUtility.GetHandleSize where you might want to have constant screen-sized handles.
        /// </summary>
        /// <param name="position">Position in 3D space as seen from the current handle camera.</param>
        /// <param name="content">Text, image and tooltip for this label.</param>
        /// <param name="style">The style to use. If left out, the label style from the current GUISkin is used.</param>
        [System.Diagnostics.Conditional("UNITY_EDITOR")]
        public static void Label(Vector3 position, GUIContent content, GUIStyle style)
        {
#if UNITY_EDITOR
            UnityEditor.Handles.color = Gizmos.color;
            UnityEditor.Handles.Label(position, content, style);
#endif
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值