2023-04-03 11:04:31 +08:00

119 lines
3.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace BF
{
[ExecuteInEditMode]
public class EmojiGraphic : MaskableGraphic
{
#region
//顶点缓存数据
private readonly UIVertex[] tempVerts = new UIVertex[4];
private MeshInfo meshInfo = new MeshInfo();
[SerializeField]
private Texture2D _mainTexture;
public Texture2D MainTexture {
get { return _mainTexture; }
set
{
_mainTexture = value;
SetAllDirty();
}
}
public override Texture mainTexture
{
get
{
return _mainTexture;
}
}
//inlintext
public void SetMeshInfos(List<EmojiInfo> emojiInfos)
{
meshInfo.Reset();
foreach (var emojiInfo in emojiInfos)
{
meshInfo.vertexs.AddRange(emojiInfo.Corner);
meshInfo.uvs.AddRange(emojiInfo.UVs);
}
SetAllDirty();
}
//lua调用
public void SetMeshInfoFromLua(List<Vector3> pos, List<float> uv)
{
meshInfo.Reset();
for (var i = 0; i < pos.Count; ++i)
{
pos[i] = transform.worldToLocalMatrix.MultiplyPoint3x4(pos[i]);
}
for (var i = 0; i < uv.Count; i += 2)
{
meshInfo.uvs.Add(new Vector2(uv[i], uv[i + 1]));
}
meshInfo.vertexs.AddRange(pos);
SetAllDirty();
}
#endregion
protected override void OnPopulateMesh(VertexHelper vh)
{
vh.Clear();
//在这里可以做一个数据判断,如果数据一样 就不再刷新
if (meshInfo != null)
{
for (int i = 0; i < meshInfo.vertexs.Count; i++)
{
int tempVertsIndex = i & 3;
tempVerts[tempVertsIndex].position = meshInfo.vertexs[i];// Utility.TransformWorld2Point(transform, _meshInfo.Vertices[i]);
tempVerts[tempVertsIndex].uv0 = meshInfo.uvs[i];
tempVerts[tempVertsIndex].color = color;
if (tempVertsIndex == 3)
{
vh.AddUIVertexQuad(tempVerts);
}
}
}
}
protected override void OnDestroy()
{
meshInfo.Release();
}
}
public class MeshInfo
{
public List<Vector3> vertexs = new List<Vector3>();
public List<Vector2> uvs = new List<Vector2>();
//public List<Color32> colors = new List<Color32>();
public void Reset()
{
vertexs.Clear();
uvs.Clear();
//colors.Clear();
}
public void Release()
{
vertexs.Clear();
vertexs = null;
uvs.Clear();
uvs = null;
//colors.Clear();
//colors = null;
}
}
}