Unity默认的Gizmos
Unity提供了一些默认的Gizmos,如图,选中一个GameObject,在Inspector中点击下三角按钮,即可选中对应的预置的Gizmos。
Inspector视图
Scene视图
选中Game视图的Gizmos,即可在Game视图中观察到对应的Gizmos。
Game视图
通过脚本绘制Gizmos
1. 通过Unity回调绘制Gizmos
将下面的脚本,挂载在任意GameObject上。
using UnityEngine;
//继承自MonoBehaviour
public class GizmoExample : MonoBehaviour
{
//实时绘制,并且只在Editor模式下运行
private void OnDrawGizmos()
{
var color = Gizmos.color;
Gizmos.color = Color.white;
//绘制立方体
Gizmos.DrawCube(transform.position,Vector3.one);
Gizmos.color = color;
}
//选中对应的GameObject绘制,并且只在Editor模式下运行
private void OnDrawGizmosSelected()
{
var color = Gizmos.color;
Gizmos.color = Color.red;
//绘制立方体边框
Gizmos.DrawWireCube(transform.position,Vector3.one);
Gizmos.color = color;
}
}
效果如图:
未选中状态
选中状态
我们可以在Scene视图的Gizmos下拉框中控制所有的Gizmos的显示。
Gizmos控制
2. 通过DrawGizmo属性绘制Gizmos
通过OnDrawGizmos()和OnDrawGizmosSelected()绘制Gizmos需要修改脚本本身,但一些第三方的插件或dll文件,我们无法修改它们的代码,这个时候我们就需要使用DrawGizmo属性进行绘制。
创建一个TargetExample空脚本,用于模拟第三方插件脚本,并挂在任意GameObject上。
TargetExample脚本
using UnityEngine;
public class TargetExample : MonoBehaviour
{
}
创建我们的DrawGizmoExample脚本用于对TargetExample进行绘制。
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class DrawGizmoExample
{
//添加DrawGizmo属性 实时绘制
[DrawGizmo(GizmoType.Active | GizmoType.Pickable | GizmoType.Selected
| GizmoType.NotInSelectionHierarchy | GizmoType.InSelectionHierarchy)]
private static void MyCustomOnDrawGizmos(TargetExample target, GizmoType gizmoType)
{
var color = Gizmos.color;
Gizmos.color = Color.white;
//绘制立方体
Gizmos.DrawCube( target.transform.position,Vector3.one);
Gizmos.color = color;
}
//添加DrawGizmo属性 选中绘制
[DrawGizmo(GizmoType.Active|GizmoType.InSelectionHierarchy)]
private static void MyCustomOnDrawGizmosSelected(TargetExample target, GizmoType gizmoType)
{
var color = Gizmos.color;
Gizmos.color = Color.red;
//绘制立方体边框
Gizmos.DrawWireCube(target.transform.position,Vector3.one);
Gizmos.color = color;
}
}
效果与方式一相同,效果图略。
Gizmos常用的方法
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GizmosDemo : MonoBehaviour
{
public Vector3 center = Vector3.zero;
public Vector3 size = new Vector3(2,2,2);
public float radius = 1f;
public Vector3 from =Vector3.zero;
public Vector3 direction = Vector3.up;
public Vector3 to = new Vector3(0, 10, 10);
public float fov = 60;
public float maxRange = 3;
public float minRange = 1;
public float aspect = 1.3f;
public Rect screenRect =new Rect(0, 0, 100, 100);
public Texture texture;
private void OnDrawGizmos()
{
//绘制立方体
Gizmos.DrawCube(center,size);
//绘制立方体线框
Gizmos.DrawWireCube(center,size);
//绘制球形
Gizmos.DrawSphere(center,radius);
//绘制球形线框
Gizmos.DrawWireSphere(center,radius);
//绘制射线
Gizmos.DrawRay(@from,direction);
//绘制线段
Gizmos.DrawLine(@from,to);
//绘制视锥体
Gizmos.DrawFrustum(center,fov,maxRange,minRange,aspect);
//绘制icon 需要将文件放到Gizmos文件夹下
Gizmos.DrawIcon(center,"test");
//绘制纹理 以左上角为(0,0)点
Gizmos.DrawGUITexture(screenRect,texture);
}
}
绘制icon 需要将文件放到Gizmos文件夹下
效果图
修改Gizmos变换矩阵
当我们改变GameObject的transform组件的position,rotation,scale时,我们会发现绘制的Gizmos并没有随着变化,这时我们可以通过修改Gizmos.matrix的默认变换矩阵,来解决这个问题。
有问题的
修改Gizmos矩阵
private void OnDrawGizmos()
{
//保存原来的变换矩阵
Matrix4x4 oldMatrix = Gizmos.matrix;
//设置变换矩阵
Gizmos.matrix = transform.localToWorldMatrix;
//绘制立方体
Gizmos.DrawCube(center,size);
//绘制立方体线框
Gizmos.DrawWireCube(center,size);
//绘制球形
Gizmos.DrawSphere(center,radius);
//绘制球形线框
Gizmos.DrawWireSphere(center,radius);
//绘制射线
Gizmos.DrawRay(@from,direction);
//绘制线段
Gizmos.DrawLine(@from,to);
//绘制视锥体
Gizmos.DrawFrustum(center,fov,maxRange,minRange,aspect);
//绘制icon 需要将文件放到Gizmos文件夹下
Gizmos.DrawIcon(center,"test");
//绘制纹理 以左上角为(0,0)点
Gizmos.DrawGUITexture(screenRect,texture);
//还原变换矩阵
Gizmos.matrix = oldMatrix;
}
网友评论