void Fill() { if (atlas == null) { return; } if (string.IsNullOrEmpty(spriteName)) { return; } m_sprite = atlas.GetSpriteDataByName(spriteName); CalUV(); Vector4 u = drawingUVs; Vector4 v = drawingDimensions; //得到对应的网格对象// Mesh mesh = new Mesh(); meshFilter.mesh = mesh; //三角形顶点的坐标数组// vertices.Clear(); //uv贴图坐标// uv.Clear(); //三角形顶点数组// int[] triangles = GenerateCachedIndexBuffer(4, 6); //颜色数组// Color[] colors = new Color[4]; vertices.Add(new Vector3(v.x, v.y, 0)); vertices.Add(new Vector3(v.x, v.w, 0)); vertices.Add(new Vector3(v.z, v.w, 0)); vertices.Add(new Vector3(v.z, v.y, 0)); //设置顶点颜色// colors[0] = Color.white; colors[1] = Color.white; colors[2] = Color.white; colors[3] = Color.white; //绑定贴图UV// uv.Add(new Vector2(u.x, u.y)); uv.Add(new Vector2(u.x, u.w)); uv.Add(new Vector2(u.z, u.w)); uv.Add(new Vector2(u.z, u.y)); //给mesh赋值// mesh.SetVertices(vertices); mesh.triangles = triangles; mesh.colors = colors; mesh.SetUVs(0, uv); }
void DrawText(List <Vector3> fontVertices, List <int> fontTriangles, List <Vector2> fontUV, List <Color> fontColorList, Color fontColor, List <Vector3> picVertices, List <Vector2> picUV, List <int> picTriangles) { Vector3 pos = Vector3.zero; int index = 0; int picIndex = 0; for (int i = 0; i < str.Length;) { char c = str[i]; if (c == '#' && Char.IsNumber(str[i + 1]) && Char.IsNumber(str[i + 2]) && Char.IsNumber(str[i + 3]) ) { string name = str.Substring(i + 1, 3); if (atlas != null) { MikuSpriteData spriteInfo = atlas.GetSpriteDataByName(name); Rect outer = new Rect(spriteInfo.x, spriteInfo.y, spriteInfo.width, spriteInfo.height); Texture tex = atlas.spriteMaterial.mainTexture; Rect outerUV = MikuSpriteData.ConvertToTexCoords(outer, tex.width, tex.height); picVertices.Add(pos + new Vector3(0, spriteInfo.height, 0)); picVertices.Add(pos + new Vector3(spriteInfo.width, spriteInfo.height, 0)); picVertices.Add(pos + new Vector3(spriteInfo.width, 0, 0)); picVertices.Add(pos + new Vector3(0, 0, 0)); picUV.Add(new Vector2(outerUV.xMin, outerUV.yMax)); picUV.Add(new Vector2(outerUV.xMax, outerUV.yMax)); picUV.Add(new Vector2(outerUV.xMax, outerUV.yMin)); // ch.uvBottomRight); picUV.Add(new Vector2(outerUV.xMin, outerUV.yMin)); //ch.uvBottomLeft); picTriangles.Add(4 * picIndex + 0); picTriangles.Add(4 * picIndex + 1); picTriangles.Add(4 * picIndex + 2); picTriangles.Add(4 * picIndex + 0); picTriangles.Add(4 * picIndex + 2); picTriangles.Add(4 * picIndex + 3); // Advance character position pos += new Vector3(spriteInfo.width, 0, 0); } i = i + 4; picIndex++; } else { // Get character rendering information from the font CharacterInfo ch; font.GetCharacterInfo(c, out ch, fontSize); fontVertices.Add(pos + new Vector3(ch.minX, ch.maxY, 0)); fontVertices.Add(pos + new Vector3(ch.maxX, ch.maxY, 0)); fontVertices.Add(pos + new Vector3(ch.maxX, 0, 0)); fontVertices.Add(pos + new Vector3(ch.minX, 0, 0)); fontUV.Add(ch.uvTopLeft); fontUV.Add(ch.uvTopRight); fontUV.Add(ch.uvBottomRight); fontUV.Add(ch.uvBottomLeft); fontColorList.Add(fontColor); fontColorList.Add(fontColor); fontColorList.Add(fontColor); fontColorList.Add(fontColor); fontTriangles.Add(4 * index + 0); fontTriangles.Add(4 * index + 1); fontTriangles.Add(4 * index + 2); fontTriangles.Add(4 * index + 0); fontTriangles.Add(4 * index + 2); fontTriangles.Add(4 * index + 3); // Advance character position pos += new Vector3(ch.advance, 0, 0); index++; i++; } } }