untiy使用GL简单画个线框

由于最近做个功能,想要在屏幕上画个框框突出下 我要显示的东西,所以简单使用GL写了个画线框的方法,只要传入两个点就可以了

using UnityEngine;
using System.Collections;

public class DrawLine : MonoBehaviour {

public Material mat;
private Vector2 screenStartPos, screenEndPos;

void OnPostRender(Vector2 pos1, Vector2 pos2)
    {
        GL.PushMatrix();
        mat.SetPass(0);
        GL.LoadOrtho();
        GL.Begin(GL.LINES);
        GL.Color(Color.red);
        GL.Vertex3(pos1.x / Screen.width, pos1.y / Screen.height, 0);
        GL.Vertex3(pos2.x / Screen.width, pos1.y / Screen.height, 0);
        GL.Vertex3(pos2.x / Screen.width, pos1.y / Screen.height, 0);
        GL.Vertex3(pos2.x / Screen.width, pos2.y / Screen.height, 0);
        GL.Vertex3(pos2.x / Screen.width, pos2.y / Screen.height, 0);
        GL.Vertex3(pos1.x / Screen.width, pos2.y / Screen.height, 0);
        GL.Vertex3(pos1.x / Screen.width, pos2.y / Screen.height, 0);
        GL.Vertex3(pos1.x / Screen.width, pos1.y / Screen.height, 0);
        GL.End();
        GL.PopMatrix();
    }
}

你可能感兴趣的:(untiy使用GL简单画个线框)