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 emojiInfos) { meshInfo.Reset(); foreach (var emojiInfo in emojiInfos) { meshInfo.vertexs.AddRange(emojiInfo.Corner); meshInfo.uvs.AddRange(emojiInfo.UVs); } SetAllDirty(); } //lua调用 public void SetMeshInfoFromLua(List pos, List 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 vertexs = new List(); public List uvs = new List(); //public List colors = new List(); public void Reset() { vertexs.Clear(); uvs.Clear(); //colors.Clear(); } public void Release() { vertexs.Clear(); vertexs = null; uvs.Clear(); uvs = null; //colors.Clear(); //colors = null; } } }