Unity3D中画拉选框(绘制多选框)
2024-08-29 03:25:39
问题分析:
需要根据鼠标事件,摁下鼠标开始绘制选择框,抬起鼠标结束绘制。
实现思路:
该需求是屏幕画线,Unity内置了GL类 封装了OpenGL,可以通过GL类来实现一些简单的画图操作,这里也是使用GL实现。
分析:
代码中有这样一个回调是属于屏幕渲染的,需要在API里了解一下public void OnRenderObject(),以及OnGUI都可以实现,了解下Unity的生命周期执行顺序中,屏幕渲染会在GameLogic后会执行其中有几个屏幕渲染回调API来了解一下:
注意:
GL.Push 和 GL.Pop 之间写GL代码
GL.Begin 和 GL.End 之间写画线逻辑Begin和End 之间的两个Vector3表示起点和终点
代码:
/// <summary>
/// 绘制选择框
/// </summary>
class Selectbox : MonoBehaviour
{
#region Fields & Attribute //画笔颜色
private Color brushColor=Color.white; //画线的材质
private Material drawMaterial; //开始绘制标志
private bool isStartDraw=false; //开始和结束绘制点
private Vector3 mouseStartPos, mouseEndPos; //设置选择区域的Color
private Color selectionAreaColor = Color.green; //获取绘制状态(开始绘制标志)
public bool IsStartDraw { get => isStartDraw; set => isStartDraw = value; } //绘制开始坐标
public Vector3 MouseStartPos { get => mouseStartPos; set => mouseStartPos = value; } //绘制结束坐标
public Vector3 MouseEndPos { get => mouseEndPos; set => mouseEndPos = value; } //设置画笔颜色
public Color BrushColor { get => brushColor; set => brushColor = value; } //设置选择区域的Color
public Color SelectionAreaColor { get => selectionAreaColor; set => selectionAreaColor = value; } #endregion #region Public Methods /// <summary>
/// 绘制选择框构造函数
/// </summary>
public Selectbox()
{ } /// <summary>
/// 初始化
/// </summary>
public void Awake()
{
drawMaterial = new Material(Shader.Find("UI/Default"));
this.drawMaterial.hideFlags = HideFlags.HideAndDontSave;
this.drawMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
} #endregion #region Private Methods /// <summary>
/// 绘制逻辑
/// </summary>
private void OnGUI()
{
if (IsStartDraw)
{
//材质通道,0为默认。
drawMaterial.SetPass();
GL.LoadOrtho();
//设置用屏幕坐标绘图
GL.LoadPixelMatrix();
DrawRect();
DrawRectLine();
}
} /// <summary>
/// 绘制框选区
/// </summary>
private void DrawRect()
{
GL.Begin(GL.QUADS);
//设置颜色和透明度
GL.Color(selectionAreaColor);
if ((MouseStartPos.x > MouseEndPos.x && MouseStartPos.y > MouseEndPos.y) || (MouseStartPos.x < MouseEndPos.x && MouseStartPos.y < MouseEndPos.y))
{
GL.Vertex3(MouseStartPos.x, MouseStartPos.y, );
GL.Vertex3(MouseStartPos.x, MouseEndPos.y, );
GL.Vertex3(MouseEndPos.x, MouseEndPos.y, );
GL.Vertex3(MouseEndPos.x, MouseStartPos.y, ); }
else
{
GL.Vertex3(MouseStartPos.x, MouseStartPos.y, );
GL.Vertex3(MouseEndPos.x, MouseStartPos.y, );
GL.Vertex3(MouseEndPos.x, MouseEndPos.y, );
GL.Vertex3(MouseStartPos.x, MouseEndPos.y, );
}
GL.End();
} /// <summary>
/// 绘制框选边框
/// </summary>
private void DrawRectLine()
{
GL.Begin(GL.LINES);
//设置方框的边框颜色 边框不透明
GL.Color(BrushColor);
GL.Vertex3(MouseStartPos.x, MouseStartPos.y, );
GL.Vertex3(MouseEndPos.x, MouseStartPos.y, );
GL.Vertex3(MouseEndPos.x, MouseStartPos.y, );
GL.Vertex3(MouseEndPos.x, MouseEndPos.y, );
GL.Vertex3(MouseEndPos.x, MouseEndPos.y, );
GL.Vertex3(MouseStartPos.x, MouseEndPos.y, );
GL.Vertex3(MouseStartPos.x, MouseEndPos.y, );
GL.Vertex3(MouseStartPos.x, MouseStartPos.y, );
GL.End();
} #endregion
}
这个地方,在画面时,发现只有两个象限是正常的,其他两个象限都画不出来,原来是一三和二四象限中构建面(点)的顺序不同,相反。还有shader选默认的就好,有颜色即可。
即这段。
到这完成了就,欢迎指正。
最新文章
- 深入理解Sqlserver文件存储之页和应用 (转)
- xmind的第十三天笔记
- 解决eclipse-helios中Errors running builder JavaScript Validator的问题(转)
- Using LINQ to SharePoint
- LNAMP架构中后端Apache获取用户真实IP地址的2种方法(转)
- Hadoop集群(第6期)_WordCount运行详解
- 绝对震撼 7款HTML5动画应用及源码
- Windows 下整理内存工具推荐——cleanmem
- HTML-块级元素和内联元素
- Linux 计算器
- [ios-必看] 国人当自强:两岸三地在线编程学习网站大搜罗 [转]
- 2016弱校联盟十一专场10.2——Floyd-Warshall
- kubernetes-核心资源之Ingress
- [NOI2002] 贪吃的九头龙
- 浅探网络1---tcp协议详解(三次握手和四次挥手)
- msfvenom生成各类Payload命令
- Centos7单主机部署 LAMP + phpmyadmin 服务
- Gym 101655:2013Pacific Northwest Regional Contest(寒假自训第13场)
- c++智能指针(1)
- 1.3currentThread()方法