- 16
- 0
- 约1.36万字
- 约 3页
- 2017-06-08 发布于河南
- 举报
Unity3D之Gizmos画圆
Unity3D之Gizmos画圆
Gizmos是场景视图里的一个可视化调试工具。在做项目过程中,我们经常会用到它,例如:绘制一条射线等。
Unity3D 4.2版本截至,目前只提供了绘制射线,线段,网格球体,实体球体,网格立方体,实体立方体,图标,GUI纹理,以及摄像机线框。
如果需要绘制一个圆环还需要自己写代码。
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视图窗口里显示一个
您可能关注的文档
最近下载
- 新解读《DL_T 408—2023电力安全工作规程 发电厂和变电站电气部分》最新解读.docx VIP
- 1.7 有多少名观众 教案 2025-2026学年北师大版数学三年级下册.docx VIP
- 第5章 比亚迪精诚钣喷质量管理体系(A0版).pdf VIP
- 学堂在线《大学生心理健康》课后作业单元考核答案.docx VIP
- 脑出血钻孔引流术后护理要点.pptx VIP
- 抖音美妆类短视频营销策略.pdf VIP
- 热敏罐灸疗法可复制.pdf VIP
- 《过敏性紫癜预防与处理指南(2025)解读》.docx VIP
- SL706-2015水库调度编制导则.pdf VIP
- 《美妆短视频的发展问题研究》文献综述1700字.docx VIP
原创力文档

文档评论(0)