所有gizmo绘制需要在脚本的OnDrawGizmos或OnDrawGizmosSelected里函数完成。

OnDrawGizmos在每帧调用。所有在OnDrawGizmos中渲染的gizmos都是可见的。

OnDrawGizmosSelected仅在脚本附加的物体被选择时被调用。

  1. Gizmos.DrawLine

从obj1到obj2之间画一条绿色的线。

 using UnityEngine;
using System.Collections;
public class DrawLineText : MonoBehaviour { public GameObject obj1;
public GameObject obj2; void OnDrawGizmos()
{
Gizmos.color = Color.green;
Gizmos.DrawLine( obj1.transform.position , obj2.transform.position );
}
}

2.Gizmos.DrawRay

从obj向上画一条长度为10的射线

 using UnityEngine;
using System.Collections;
public class DrawRayText : MonoBehaviour { public GameObject obj;
void OnDrawGizmos()
{
Gizmos.color = Color.gray;
Gizmos.DrawRay(obj.transform.position, Vector3.up * ); //10 是长度
}
}

3.Gizmos.DrawCube

在(0,1,0)处画一个(1,1,1)大小的立方体

 using UnityEngine;
using System.Collections;
public class DrawCubeText : MonoBehaviour { void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawCube(Vector3.up , Vector3.one);
}
}

4.Gizmos.DrawIcon

在(0,0,0)处生成一个名字为002IMgZLzy6Mro7r94Ka2&690.jpg的Icon ,此图片要放到Assets下的  Gizmos文件夹里才行。

 using UnityEngine;
using System.Collections;
public class DrawIconText : MonoBehaviour { void OnDrawGizmos()
{
Gizmos.DrawIcon(Vector3.zero , "002IMgZLzy6Mro7r94Ka2&690.jpg");
}
}
04-28 08:08