参考链接:
https://blog.csdn.net/jk823394954/article/details/53870779
说明:unity版本5.3,新建一个Image,挂上以下的脚本
一.图形绘制
1.绘制三角形
using UnityEngine;
using UnityEngine.UI; public class TestMesh : BaseMeshEffect { public override void ModifyMesh(VertexHelper vh)
{
vh.Clear();
vh.AddVert(new Vector3(, ), Color.red, Vector2.zero);
vh.AddVert(new Vector3(, ), Color.green, Vector2.zero);
vh.AddVert(new Vector3(, ), Color.black, Vector2.zero);
vh.AddTriangle(, , );
}
}
2.绘制正方形(绘制两个三角形)
using UnityEngine;
using UnityEngine.UI; public class TestMesh : BaseMeshEffect { public override void ModifyMesh(VertexHelper vh)
{
vh.Clear();
vh.AddVert(new Vector3(, ), Color.red, Vector2.zero);
vh.AddVert(new Vector3(, ), Color.green, Vector2.zero);
vh.AddVert(new Vector3(, ), Color.black, Vector2.zero);
vh.AddVert(new Vector3(, ), Color.blue, Vector2.zero);
vh.AddTriangle(, , );
vh.AddTriangle(, , );
}
}
3.绘制正方形(绘制一个正方形)
using UnityEngine;
using UnityEngine.UI; public class TestMesh : BaseMeshEffect { public override void ModifyMesh(VertexHelper vh)
{
vh.Clear();
UIVertex[] verts = new UIVertex[]; verts[].position = new Vector3(, );
verts[].color = Color.red;
verts[].uv0 = Vector2.zero; verts[].position = new Vector3(, );
verts[].color = Color.green;
verts[].uv0 = Vector2.zero; verts[].position = new Vector3(, );
verts[].color = Color.black;
verts[].uv0 = Vector2.zero; verts[].position = new Vector3(, );
verts[].color = Color.blue;
verts[].uv0 = Vector2.zero; vh.AddUIVertexQuad(verts);
}
}
二.相关api
using UnityEngine;
using UnityEngine.UI; public class TestMesh : BaseMeshEffect { public override void ModifyMesh(VertexHelper vh)
{
vh.Clear();
vh.AddVert(new Vector3(, ), Color.red, Vector2.zero);
vh.AddVert(new Vector3(, ), Color.green, Vector2.zero);
vh.AddVert(new Vector3(, ), Color.black, Vector2.zero);
vh.AddVert(new Vector3(, ), Color.blue, Vector2.zero);
vh.AddTriangle(, , );
vh.AddTriangle(, , ); //顶点有4个,索引有6个
Debug.Log(vh.currentIndexCount);
Debug.Log(vh.currentVertCount); //PopulateUIVertex,返回顶点数据。当前取值范围:0-3
UIVertex vertex = new UIVertex();
vh.PopulateUIVertex(ref vertex, );
Debug.Log(vertex.color);
vh.PopulateUIVertex(ref vertex, );
Debug.Log(vertex.color); //SetUIVertex,设置顶点数据
vertex.color = Color.yellow;
vh.SetUIVertex(vertex, );
}
}
输出:
Stream流批量操作:
1.AddUIVertexStream,批量添加顶点
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; public class TestMesh : BaseMeshEffect { public override void ModifyMesh(VertexHelper vh)
{
vh.Clear();
List<UIVertex> verts = new List<UIVertex>(); UIVertex vert0 = new UIVertex();
vert0.position = new Vector3(, );
vert0.color = Color.red;
vert0.uv0 = Vector2.zero;
verts.Add(vert0); UIVertex vert1 = new UIVertex();
vert1.position = new Vector3(, );
vert1.color = Color.green;
vert1.uv0 = Vector2.zero;
verts.Add(vert1); UIVertex vert2 = new UIVertex();
vert2.position = new Vector3(, );
vert2.color = Color.black;
vert2.uv0 = Vector2.zero;
verts.Add(vert2); UIVertex vert3 = new UIVertex();
vert3.position = new Vector3(, );
vert3.color = Color.blue;
vert3.uv0 = Vector2.zero;
verts.Add(vert3); List<int> indices = new List<int>() { , , , , , };
vh.AddUIVertexStream(verts, indices);
}
}
2.AddUIVertexTriangleStream,批量添加三角形顶点,长度必须是3的倍数
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; public class TestMesh : BaseMeshEffect { public override void ModifyMesh(VertexHelper vh)
{
vh.Clear();
List<UIVertex> verts = new List<UIVertex>(); UIVertex vert0 = new UIVertex();
vert0.position = new Vector3(, );
vert0.color = Color.red;
vert0.uv0 = Vector2.zero;
verts.Add(vert0); UIVertex vert1 = new UIVertex();
vert1.position = new Vector3(, );
vert1.color = Color.green;
vert1.uv0 = Vector2.zero;
verts.Add(vert1); UIVertex vert2 = new UIVertex();
vert2.position = new Vector3(, );
vert2.color = Color.black;
vert2.uv0 = Vector2.zero;
verts.Add(vert2); vh.AddUIVertexTriangleStream(verts);
}
}
3.GetUIVertexStream,获取所有顶点数据
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; public class TestMesh : BaseMeshEffect { public override void ModifyMesh(VertexHelper vh)
{
vh.Clear(); vh.AddVert(new Vector3(, ), Color.red, Vector2.zero);
vh.AddVert(new Vector3(, ), Color.green, Vector2.zero);
vh.AddVert(new Vector3(, ), Color.black, Vector2.zero);
vh.AddTriangle(, , ); List<UIVertex> stream = new List<UIVertex>();
vh.GetUIVertexStream(stream);
foreach (UIVertex v in stream)
{
Debug.Log(v.color);
}
}
}