经常在scene中看到这些 线框,图标 形状 ,我好像从来没有注意过。。。。
这都是 Gizmos 的功劳
Gizmos在这里
你可以调节选项以及值,来查看效果。
更改图标大小,是否显示。
然后去看下API,以及F12
这是两个函数,系统自动调用.
private void OnDrawGizmos()
{
Debug.Log("drawGizmos,一直调用");
}
private void OnDrawGizmosSelected()
{
Debug.Log("selected,只有物体选中后调用");
}
[ExecuteInEditMode]
public class TimorGizmosDrawRay : MonoBehaviour
{
public GameObject cube = null;
public float distance = 5;
private void Start()
{
cube = GameObject.Find("Sphere");
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.cyan;
Gizmos.DrawRay(transform.localPosition, cube.transform.forward * distance);
Gizmos.DrawRay(transform.localPosition, cube.transform.position);
}
}
这里坐标处理的有点问题。后期修正
public class TimorGizmosDrawCube : MonoBehaviour
{
public Vector3 center = Vector3.zero;
public Vector3 size = Vector3.zero;
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.cyan;
Gizmos.DrawCube(center, size);
Gizmos.DrawWireCube(center, size + new Vector3(3, 3, 3));
}
}
既然可以绘制实心立方体,也有空心,同理也有球体
Gizmos.DrawSphere()
一些参数挺简单的,我就不写了
public class TimorGizmosDrawSphere : MonoBehaviour
{
public Vector3 center = Vector3.zero;
public float radius = 1.5f;
void Start()
{
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.cyan;
Gizmos.DrawSphere(center,radius);
Gizmos.DrawWireSphere(center, radius * 1.5f);
}
}
这下需要注意,图片需要放到Gizmos文件下。
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.cyan;
Gizmos.DrawIcon(center, "timor", true);
}
public Vector3 from = Vector3.zero;
public Vector3 to = Vector3.zero;
public Color color = Color.cyan;
public int x = 3;
public int y = 3;
public int z = 3;
void Start()
{
}
private void OnDrawGizmosSelected()
{
Gizmos.color = color;
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
for (int k = 0; k < z; k++)
{
to = new Vector3(i, j, k);
Gizmos.DrawLine(Vector3.zero, to);
}
}
}
}
既然可以算出来每个点坐标,从 0,0,0点画线过去,那可不可以画出一个空心立方体。回头研究
从前有个人看我的文章,他点了赞,第二天面试就通过了
Unity - Manual: Gizmos menu https://docs.unity3d.com/Manual/GizmosMenu.html
Unity - Scripting API: Gizmos https://docs.unity3d.com/ScriptReference/Gizmos.html