最近在搞运行模式下的编辑器功能,策划提了个要求是在Game窗口中能看到BoxCollider,所以就找了找资料,实现了一下:
using System;
using System.Collections.Generic;
using UnityEngine;
public class TriggerRectView : MonoBehaviour {
public int width;
public int height;
//画线用的材质球
Material lineMat;
private Vector3 v3FrontTopLeft;
private Vector3 v3FrontTopRight;
private Vector3 v3FrontBottomLeft;
private Vector3 v3FrontBottomRight;
private Vector3 v3BackTopLeft;
private Vector3 v3BackTopRight;
private Vector3 v3BackBottomLeft;
private Vector3 v3BackBottomRight;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnEnable()
{
if (lineMat == null)
{
lineMat = Resources.Load<Material>("M/LineMat");
}
CalcBoxColliderPositons();
}
void OnRenderObject()
{
CalcBoxColliderPositons();
lineMat.SetPass(0);
GL.Begin(GL.LINES);
GL.Color(Color.red);
GL.Vertex(v3FrontTopLeft);
GL.Vertex(v3FrontTopRight);
GL.Vertex(v3FrontTopRight);
GL.Vertex(v3FrontBottomRight);
GL.Vertex(v3FrontBottomRight);
GL.Vertex(v3FrontBottomLeft);
GL.Vertex(v3FrontBottomLeft);
GL.Vertex(v3FrontTopLeft);
GL.Vertex(v3BackTopLeft);
GL.Vertex(v3BackTopRight);
GL.Vertex(v3BackTopRight);
GL.Vertex(v3BackBottomRight);
GL.Vertex(v3BackBottomRight);
GL.Vertex(v3BackBottomLeft);
GL.Vertex(v3BackBottomLeft);
GL.Vertex(v3BackTopLeft);
GL.Vertex(v3FrontTopLeft);
GL.Vertex(v3BackTopLeft);
GL.Vertex(v3FrontTopRight);
GL.Vertex(v3BackTopRight);
GL.Vertex(v3FrontBottomRight);
GL.Vertex(v3BackBottomRight);
GL.Vertex(v3FrontBottomLeft);
GL.Vertex(v3BackBottomLeft);
GL.End();
}
void CalcBoxColliderPositons()
{
Bounds bounds = GetComponent<BoxCollider>().bounds;
Vector3 v3Center = bounds.center - transform.position;//此处减去GameObject本身坐标是为了在物体移动时,绘制的Box也跟着一起移动
Vector3 v3Extents = bounds.extents;
v3FrontTopLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z); // Front top left corner
v3FrontTopRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z); // Front top right corner
v3FrontBottomLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z); // Front bottom left corner
v3FrontBottomRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z); // Front bottom right corner
v3BackTopLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z); // Back top left corner
v3BackTopRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z); // Back top right corner
v3BackBottomLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z); // Back bottom left corner
v3BackBottomRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z); // Back bottom right corner
v3FrontTopLeft = transform.TransformPoint(v3FrontTopLeft);
v3FrontTopRight = transform.TransformPoint(v3FrontTopRight);
v3FrontBottomLeft = transform.TransformPoint(v3FrontBottomLeft);
v3FrontBottomRight = transform.TransformPoint(v3FrontBottomRight);
v3BackTopLeft = transform.TransformPoint(v3BackTopLeft);
v3BackTopRight = transform.TransformPoint(v3BackTopRight);
v3BackBottomLeft = transform.TransformPoint(v3BackBottomLeft);
v3BackBottomRight = transform.TransformPoint(v3BackBottomRight);
}
}