unity3d Gizmos 画圆

<pre class="csharp" name="code">using UnityEngine;
using System;

public class HeGizmosCircle : MonoBehaviour
{
       public Transform m_Transform;
       public float m_Radius = 1; // 圆环的半径
       public float m_Theta = 0.1f; // 值越低圆环越平滑
       public Color m_Color = Color.green; // 线框颜色
       
       void Start()
       {
              if (m_Transform == null)
              {
                     throw new Exception("Transform is NULL.");
              }
       }

       void OnDrawGizmos()
       {
              if (m_Transform == null) return;
              if (m_Theta < 0.0001f) m_Theta = 0.0001f;

              // 设置矩阵
              Matrix4x4 defaultMatrix = Gizmos.matrix;
              Gizmos.matrix = m_Transform.localToWorldMatrix;

              // 设置颜色
              Color defaultColor = Gizmos.color;
              Gizmos.color = m_Color;

              // 绘制圆环
              Vector3 beginPoint = Vector3.zero;
              Vector3 firstPoint = Vector3.zero;
              for (float theta = 0; theta < 2 * Mathf.PI; theta += m_Theta)
              {
                     float x = m_Radius * Mathf.Cos(theta);
                     float z = m_Radius * Mathf.Sin(theta);
                     Vector3 endPoint = new Vector3(x, 0, z);
                     if (theta == 0)
                     {
                            firstPoint = endPoint;
                     }
                     else
                     {
                            Gizmos.DrawLine(beginPoint, endPoint);
                     }
                     beginPoint = endPoint;
              }

              // 绘制最后一条线段
              Gizmos.DrawLine(firstPoint, beginPoint);

              // 恢复默认颜色
              Gizmos.color = defaultColor;

              // 恢复默认矩阵
              Gizmos.matrix = defaultMatrix;
       }
}

 
 


把代码拖到一个GameObject上,关联该GameObject的Transform,然后就可以在Scene视图窗口里显示一个圆了。

通过调整Transform的Position,Rotation,Scale,来调整圆的位置,旋转,缩放。




  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值