Unity3d 运行模式下绘制BoxCollider

最近在搞运行模式下的编辑器功能,策划提了个要求是在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);
    }
}

  

 

转载于:https://www.cnblogs.com/yixuan1980/p/6648389.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值