Unity3d 使用Gizmos画一个圆圈
作者:mfmdaoyou 时间:2022-05-22 05:10:52
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视图窗体里显示一个圆了。
通过调整Transform的Position。Rotation。Scale,来调整圆的位置,旋转,缩放。
补充:基于Unity3D使用LineRender组件绘制圆线
在此记录一下使用Unity3D 的LineRender绘制线的过程,经过测试LineRender与OpenGL的GL_LINE_STRIP绘制方式一样,因此计算完点之后需要把起始点即为终点,多算一个点才算闭合。
代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DrawLines: MonoBehaviour
{
public float m_radius = 1.0f;
public Material m_material;
public float m_lineWidth = 1.0f;
private List<Vector3> vPath = new List<Vector3>();
// Start is called before the first frame update
void Start()
{
int count = 60;
for (int i=1; i<= (count+1); i++)
{
if(i == (count+1))
{
float x = Mathf.Cos(2 * Mathf.PI / count) * m_radius;
float y = transform.localPosition.y;
float z = Mathf.Sin(2 * Mathf.PI / count) * m_radius;
vPath.Add(new Vector3(x, y, z));
}
else
{
float x = Mathf.Cos(2 * Mathf.PI / count * i) * m_radius;
float y = transform.localPosition.y;
float z = Mathf.Sin(2 * Mathf.PI / count * i) * m_radius;
vPath.Add(new Vector3(x, y, z));
}
}
GameObject lineGroup = new GameObject("LineGroup");
GameObject lineObject = new GameObject("RadarLine");
LineRenderer line = lineObject.AddComponent<LineRenderer>();
line.material = m_material;
line.useWorldSpace = false;
line.positionCount = vPath.Count;
line.startWidth = m_lineWidth;
line.endWidth = m_lineWidth;
line.SetPositions(vPath.ToArray());
}
// Update is called once per frame
void Update()
{
}
}
运行一下看一下效果:
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://www.cnblogs.com/mfmdaoyou/p/7235990.html
标签:Unity3d,Gizmos,画圆
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#子线程更新UI控件的方法实例总结
2022-08-31 15:40:23
![](https://img.aspxhome.com/file/2023/9/81089_0s.jpg)
C#6.0中你可能不知道的新特性总结
2023-08-17 04:25:54
C#读写操作app.config中的数据应用介绍
2021-07-25 21:33:19
Spring注解@Scope原理及用法解析
2023-12-06 14:08:17
mybatis 逆向生成后遵循java驼峰法则的解决
2023-03-03 03:22:02
C#利用TreeView控件实现目录跳转
2021-07-26 02:57:50
![](https://img.aspxhome.com/file/2023/6/89866_0s.png)
C#中静态方法和实例化方法的区别、使用
2023-08-25 20:49:33
Java String 拼接字符串原理详解
2023-05-14 10:10:33
Android WorkManager浅谈
2023-03-24 11:26:46
![](https://img.aspxhome.com/file/2023/4/100654_0s.jpg)
SpringBoot-JPA删除不成功,只执行了查询语句问题
2022-09-10 04:13:55
详解Java线程-守护线程与用户线程
2023-11-25 00:17:29
你所不知道的Spring的@Autowired实现细节分析
2023-11-25 05:40:47
![](https://img.aspxhome.com/file/2023/0/60120_0s.jpg)
二叉搜索树实例练习
2022-09-20 22:03:06
详解spring-boot actuator(监控)配置和使用
2022-07-12 17:20:37
Android 实现单线程轮循机制批量下载图片
2022-11-05 11:03:45
Cocos2d-x 3.0多线程异步加载资源实例
2022-02-08 03:26:51
全网最全SpringBoot集成swagger的详细教程
2021-10-25 16:53:44
![](https://img.aspxhome.com/file/2023/7/103857_0s.jpg)
Java ThreadLocal的使用详解
2023-11-29 04:48:43
MyBatis插入数据返回主键的介绍
2023-10-26 03:15:21
Java 中的抽象介绍
2023-03-27 06:10:20