All gizmo drawing needs to be completed in the OnDrawGizmos or OnDrawGizmosSelected function of the script.
OnDrawGizmos is called every frame. All gizmos rendered in OnDrawGizmos are visible.
OnDrawGizmosSelected is only called when the object attached by the script is selected.
-
Gizmos.DrawLine
Draw a green line from obj1 to 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
Draw a ray of length 10 upward from obj
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); //10 是长度
}
}
3.Gizmos.DrawCube
Draw a cube of size (1,1,1) at (0,1,0)
using UnityEngine;
using System.Collections;
public class DrawCubeText : MonoBehaviour {
void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawCube(Vector3.up , Vector3.one);
}
}
4.Gizmos.DrawIcon
Create an Icon named 002IMgZLzy6Mro7r94Ka2&690.jpg at (0,0,0). This picture must be placed in the Gizmos folder under Assets.
using UnityEngine;
using System.Collections;
public class DrawIconText : MonoBehaviour {
void OnDrawGizmos()
{
Gizmos.DrawIcon(Vector3.zero , "002IMgZLzy6Mro7r94Ka2&690.jpg");
}
}