You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour
{
public Vector2 mMouseStart, mMouseEnd;
public bool mBDrawMouseRect;
private Material rectMat = null;//画线的材质不设定系统会用当前材质画线 结果不可控
void Start()
{
mBDrawMouseRect = false;
rectMat = new Material("Shader \"Lines/Colored Blended\" {" +
"SubShader { Pass { " +
" Blend SrcAlpha OneMinusSrcAlpha " +
" ZWrite Off Cull Off Fog { Mode Off } " +
" BindChannels {" +
" Bind \"vertex\", vertex Bind \"color\", color }" +
"} } }");//生成画线的材质
rectMat.hideFlags = HideFlags.HideAndDontSave;
rectMat.shader.hideFlags = HideFlags.HideAndDontSave;
} //Unity3D教程手册www.unitymanual.com
//void Update()
//{
// if (Input.GetMouseButtonDown(0))
// //按下鼠标左键
// {
// Vector3 mousePosition = Input.mousePosition;
// mMouseStart = new Vector2(mousePosition.x, mousePosition.y);
// }
// if (Input.GetMouseButton(0))
// //持续按下鼠标左键
// {
// mBDrawMouseRect = true;
// Vector3 mousePosition = Input.mousePosition;
// mMouseEnd = new Vector2(mousePosition.x, mousePosition.y);
// }
// if (Input.GetMouseButtonUp(0))
// {
// mBDrawMouseRect = false;
// }
//}
//Unity3D教程手册www.unitymanual.com
void OnGUI()
{
if (mBDrawMouseRect)
Draw(mMouseStart, mMouseEnd);
}
//渲染2D框
public void Draw(Vector2 start, Vector2 end)
{
rectMat.SetPass(0);
GL.PushMatrix();//保存摄像机变换矩阵
Color clr = Color.green;
clr.a = 0.1f;
GL.LoadPixelMatrix();//设置用屏幕坐标绘图
//透明框
GL.Begin(GL.QUADS);
GL.Color(clr);
GL.Vertex3(start.x, start.y, 0);
GL.Vertex3(end.x, start.y, 0);
GL.Vertex3(end.x, end.y, 0);
GL.Vertex3(start.x, end.y, 0);
GL.End();
//线
//上
GL.Begin(GL.LINES);
GL.Color(Color.green);
GL.Vertex3(start.x, start.y, 0);
GL.Vertex3(end.x, start.y, 0);
GL.End();
//下
GL.Begin(GL.LINES);
GL.Color(Color.green);
GL.Vertex3(start.x, end.y, 0);
GL.Vertex3(end.x, end.y, 0);
GL.End();
//左
GL.Begin(GL.LINES);
GL.Color(Color.green);
GL.Vertex3(start.x, start.y, 0);
GL.Vertex3(start.x, end.y, 0);
GL.End();
//右
GL.Begin(GL.LINES);
GL.Color(Color.green);
GL.Vertex3(end.x, start.y, 0);
GL.Vertex3(end.x, end.y, 0);
GL.End();
GL.PopMatrix();//还原
}
}