private void CalUV() { Rect outer = new Rect(m_sprite.x, m_sprite.y, m_sprite.width, m_sprite.height); Texture tex = atlas.spriteMaterial.mainTexture; m_outerUV = MikuSpriteData.ConvertToTexCoords(outer, tex.width, tex.height); }
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); }
public MikuSpriteData GetSpriteDataByName(string name) { MikuSpriteData result = null; if (!m_inited) { SlotDictData(); } m_spritesDict.TryGetValue(name, out result); return(result); }
public void SlotDictData() { if (m_inited) { return; } for (int i = 0, imax = m_sprites.Count; i < imax; i++) { MikuSpriteData data = m_sprites[i]; m_spritesDict[data.name] = data; } m_inited = true; }
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++; } } }
/// <summary> /// Parse the specified JSon file, loading sprite information for the specified atlas. /// </summary> static void LoadSpriteData(MikuAtlas atlas, Hashtable decodedHash) { if (decodedHash == null || atlas == null) { return; } List <MikuSpriteData> oldSprites = atlas.spriteList; atlas.spriteList = new List <MikuSpriteData>(); Hashtable frames = (Hashtable)decodedHash["frames"]; foreach (DictionaryEntry item in frames) { MikuSpriteData newSprite = new MikuSpriteData(); newSprite.name = item.Key.ToString(); bool exists = false; // Check to see if this sprite exists foreach (MikuSpriteData oldSprite in oldSprites) { if (oldSprite.name.Equals(newSprite.name, StringComparison.OrdinalIgnoreCase)) { exists = true; break; } } // Get rid of the extension if the sprite doesn't exist // The extension is kept for backwards compatibility so it's still possible to update older atlases. if (!exists) { newSprite.name = newSprite.name.Replace(".png", ""); newSprite.name = newSprite.name.Replace(".tga", ""); } // Extract the info we need from the TexturePacker json file, mainly uvRect and size Hashtable table = (Hashtable)item.Value; Hashtable frame = (Hashtable)table["frame"]; int frameX = int.Parse(frame["x"].ToString()); int frameY = int.Parse(frame["y"].ToString()); int frameW = int.Parse(frame["w"].ToString()); int frameH = int.Parse(frame["h"].ToString()); // [Modify] by maosongliang, begin // Read the rotation value newSprite.rotated = (bool)table["rotated"]; // [Modify] by maosongliang, end newSprite.x = frameX; newSprite.y = frameY; newSprite.width = frameW; newSprite.height = frameH; // Support for trimmed sprites Hashtable sourceSize = (Hashtable)table["sourceSize"]; Hashtable spriteSize = (Hashtable)table["spriteSourceSize"]; if (spriteSize != null && sourceSize != null) { // TODO: Account for rotated sprites if (frameW > 0) { int spriteX = int.Parse(spriteSize["x"].ToString()); int spriteW = int.Parse(spriteSize["w"].ToString()); int sourceW = int.Parse(sourceSize["w"].ToString()); newSprite.paddingLeft = spriteX; newSprite.paddingRight = sourceW - (spriteX + spriteW); } if (frameH > 0) { int spriteY = int.Parse(spriteSize["y"].ToString()); int spriteH = int.Parse(spriteSize["h"].ToString()); int sourceH = int.Parse(sourceSize["h"].ToString()); newSprite.paddingTop = spriteY; newSprite.paddingBottom = sourceH - (spriteY + spriteH); } } // [Modify] by maosongliang, begin if (newSprite.rotated) { int temp = newSprite.width; newSprite.width = newSprite.height; newSprite.height = temp; temp = newSprite.paddingLeft; newSprite.paddingLeft = newSprite.paddingTop; newSprite.paddingTop = temp; temp = newSprite.paddingRight; newSprite.paddingRight = newSprite.paddingBottom; newSprite.paddingBottom = temp; } // [Modify] by maosongliang, end // If the sprite was present before, see if we can copy its inner rect foreach (MikuSpriteData oldSprite in oldSprites) { if (oldSprite.name.Equals(newSprite.name, StringComparison.OrdinalIgnoreCase)) { if (oldSprite.rotated != newSprite.rotated) { // ---modify=> by zengyi // left top changed if (oldSprite.rotated) { newSprite.borderLeft = oldSprite.borderTop; newSprite.borderTop = oldSprite.borderRight; newSprite.borderBottom = oldSprite.borderLeft; newSprite.borderRight = oldSprite.borderBottom; } else { newSprite.borderLeft = oldSprite.borderTop; newSprite.borderTop = oldSprite.borderLeft; // right bottom changed newSprite.borderRight = oldSprite.borderBottom; newSprite.borderBottom = oldSprite.borderRight; } //----------------------- } else { newSprite.borderLeft = oldSprite.borderLeft; newSprite.borderRight = oldSprite.borderRight; newSprite.borderBottom = oldSprite.borderBottom; newSprite.borderTop = oldSprite.borderTop; } break; } } // Add this new sprite atlas.spriteList.Add(newSprite); } // Sort imported sprites alphabetically atlas.spriteList.Sort(CompareSprites); Debug.Log("Imported " + atlas.spriteList.Count + " sprites"); }
/// <summary> /// Sprite comparison function for sorting. /// </summary> static int CompareSprites(MikuSpriteData a, MikuSpriteData b) { return(a.name.CompareTo(b.name)); }