Unity 里面雖然提供Linerender繪制線條,但是只能在3D空間劃線,有時候需要在UI上繪制指定的線條,柱狀圖,餅狀圖等就可以采用下面的方式了,

創建DrawLine,繼承MaskableGraphic類,重寫OnPopulateMesh(VertexHelper vh)方法,重新使用mesh繪制線條,實體代碼如下
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DrawLine : MaskableGraphic
{
public Texture texture;
public override Texture mainTexture => texture;
List<List<UIVertex>> vertexQuadList = new List<List<UIVertex>>();
List<UIVertex> vertexQuad;
public float lineWidth=2;
Vector3 lastleftPoint;
Vector3 lastrightPoint;
Vector3 lastPos;
protected override void OnPopulateMesh(VertexHelper vh)
{
vh.Clear();
for (int i = 0; i < vertexQuadList.Count; i++)
{
vh.AddUIVertexQuad(vertexQuadList[i].ToArray());
}
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
vertexQuadList.Clear();
lastPos = Input.mousePosition;
lastleftPoint = lastPos - new Vector3(Screen.width / 2, Screen.height / 2, 0) + Vector3.up * lineWidth;
lastrightPoint = lastPos - new Vector3(Screen.width / 2, Screen.height / 2, 0) - Vector3.up * lineWidth;
}
else
{
if (Input.GetMouseButton(0))
{
Vector3 newVec = Input.mousePosition - lastPos;
if (newVec.magnitude < 0.1f)
{
return;
}
vertexQuad = new List<UIVertex>();
Vector3 vec = Vector3.Cross(newVec.normalized, Vector3.forward).normalized;
Vector3 newleftPoint = Input.mousePosition - new Vector3(Screen.width / 2, Screen.height / 2, 0) + vec * lineWidth;
Vector3 newrightPoint = Input.mousePosition - new Vector3(Screen.width / 2, Screen.height / 2, 0) - vec * lineWidth;
UIVertex uIVertex = new UIVertex();
uIVertex.position = lastleftPoint;
uIVertex.color = color;
vertexQuad.Add(uIVertex);
UIVertex uIVertex1 = new UIVertex();
uIVertex1.position = lastrightPoint;
uIVertex1.color = color;
vertexQuad.Add(uIVertex1);
UIVertex uIVertex3 = new UIVertex();
uIVertex3.position = newrightPoint;
uIVertex3.color = color;
vertexQuad.Add(uIVertex3);
UIVertex uIVertex2 = new UIVertex();
uIVertex2.position = newleftPoint;
uIVertex2.color = color;
vertexQuad.Add(uIVertex2);
lastleftPoint = newleftPoint;
lastrightPoint = newrightPoint;
vertexQuadList.Add(vertexQuad);
lastPos = Input.mousePosition;
SetVerticesDirty();
}
}
}
}
工程鏈接:https://download.csdn.net/download/qq_33547099/14037991

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/246231.html
標籤:其他
上一篇:吃豆子小游戲
