Unity 编辑器扩展四 Gizmos辅助调试工具
2021-12-09 本文已影响0人
合肥黑
参考
Unity Editor 基础篇(五):Gizmos
【Unity 编辑器】扩展总结五:Gizmos辅助调试工具
Gizmos是用于在Scene场景视图中,可视化调试或辅助设置用的。所有Gizmo绘制需要在以下两个函数里完成:
- OnDrawGizmos 每帧调用
- OnDrawGizmosSelected 仅在脚本附加的物体被选择时调用
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyGizmos : MonoBehaviour
{
private void OnDrawGizmos()
{
Debug.Log("invocation in every frame");
}
private void OnDrawGizmosSelected()
{
Debug.Log("you click on me!");
}
}
将以上脚本挂到物体上,把鼠标放在Scene场景里,就能看到打印信息了。
一、Draw系列API
- Gizmos.DrawCube() 绘制实体立方体
- Gizmos.DrawWireCube() 绘制立方体边框
- Gizmos.DrawRay() 绘制射线
- Gizmos.DrawLine() 绘制直线
- Gizmos.DrawIcon() 绘制Icon,Icon素材需要放在Gizmos文件夹中
- Gizmos.DrawFrustum() 绘制摄像机视椎体的视野范围
1.DrawWireSphere
public float areaRadius;
private void OnDrawGizmos()
{
//绘制线框球体
Gizmos.DrawWireSphere(transform.position,areaRadius);
Debug.Log("invocation in every frame");
}
image.png
2.DrawLine
public float areaRadius;
public float size;
private void OnDrawGizmos()
{
Gizmos.DrawWireSphere(transform.position,areaRadius);
Gizmos.DrawLine(transform.position,transform.position+transform.forward*size);
Debug.Log("invocation in every frame");
}
3.Mathf.Repeat
参考unity笔记3 - Mathf.Repeat and Mathf.PingPong
public static float Repeat(float t,float length);
循环值t,使输出不会大于等于length,也不会小于0。
//输入 012345678
//输出 012012012
for (int i = 0; i < 9; i++)
{
Debug.Log(Mathf.Repeat(i,3));
}
其实跟取模(%)的结果一样,但是取模的结果会有负数,Repeat的结果只会是整数
4.DrawSphere
public float areaRadius;
public float size;
public Vector3[] nodePoints;
private void OnDrawGizmos()
{
Gizmos.DrawWireSphere(transform.position,areaRadius);
Gizmos.color = Color.red;
Gizmos.DrawLine(transform.position,transform.position+transform.forward*size);
Gizmos.color = Color.blue;
for (int i=0;i < nodePoints.Length;i ++)
{
Gizmos.DrawSphere(nodePoints[i], 0.5f);
}
for (int i = 0; i < nodePoints.Length;i++)
{
Gizmos.DrawLine(nodePoints[i],
nodePoints[(int)Mathf.Repeat(i+1,nodePoints.Length)]);
}
}
image.png
Gizmos.color作为全局的静态变量,为了防止这里的color修改会对其他地方的绘制造成影响,所以在绘制完Gizmos的时候,将Gizmos.color修改为原先的值。
private void OnDrawGizmosSelected()
{
var color = Gizmos.color;
Gizmos.color = Color.white;
Gizmos.DrawWireCube(transform.position, Vector3.one);
Gizmos.color = color;
}
5.DrawIcon
Gizmos.DrawIcon(transform.position, "icon.png");
在Assets/Gizmos路径下放一个icon.png图片,就能在场景中看到
image.png
6.Gizmos.DrawFrustum() 绘制摄像机视椎体的视野范围
实现场景中一直显示主摄像机的视野范围
private Camera mainCamera;
private void OnDrawGizmos()
{
if(mainCamera == null)
mainCamera = Camera.main;
Gizmos.color = Color.green;
//设置gizmos的矩阵
Gizmos.matrix = Matrix4x4.TRS(mainCamera.transform.position,
mainCamera.transform.rotation, Vector3.one);
Gizmos.DrawFrustum(Vector3.zero, mainCamera.fieldOfView,
mainCamera.farClipPlane, mainCamera.nearClipPlane, mainCamera.aspect);
}
二、DrawGIzmos标签
在Scripts文件夹下新建一个表示业务逻辑的类TargetExample,将其挂载在场景中的对象身上。
在Editor文件夹内新建MyCustomOnDrawGizmos
using UnityEngine;
using UnityEditor;
public class GizmosTest
{
//表示物体显示并且被选择的时候,绘制Gizmos
[DrawGizmo(GizmoType.Active | GizmoType.Selected)]
//第一个参数需要指定目标类,目标类需要挂载在场景对象中
private static void MyCustomOnDrawGizmos(TargetExample target, GizmoType gizmoType)
{
var color = Gizmos.color;
Gizmos.color = Color.white;
//target为挂载该组件的对象
Gizmos.DrawCube(target.transform.position, Vector3.one);
Gizmos.color = color;
}
}
image.png
private static void MyCustomOnDrawGizmos(TargetExample target, GizmoType gizmoType)
这里指定了TargetExample 类型对象,由于其针对的是编辑器组件的方法,需要设置为Static方法。
GizmosType 描述
- Active 如果激活,则绘制
- SelectedOrChild 如果被选择或者选择子物体时,则绘制
- NotSelected 如果全没选择,则绘制
- Selected 如果选择,则绘制
- Pickable 在编辑器中gizmo可以被点选