// set by split void CalcChrRectGrid(Font fontObj, Texture tex, Rect area, int xc, int yc, int num) { float imgw = (float)tex.width; float imgh = (float)tex.height; int fw = (int)(area.width - area.x) / xc; int fh = (int)(area.height - area.y) / yc; List <ChrRect> tblList = new List <ChrRect>(); for (int i = 0; i < num; i++) { int xi = i % xc; int yi = i / xc; ChrRect d = new ChrRect(); d.index = i; d.uvX = (float)(area.x + (fw * xi)) / imgw; d.uvY = (float)(imgh - (area.y + (fh * yi) + fh)) / imgh; d.uvW = (float)fw / imgw; d.uvH = (float)fh / imgh; d.vertX = 0; d.vertY = 0; d.vertW = fw; d.vertH = -fh; d.width = fw; tblList.Add(d); } ChrRect[] tbls = tblList.ToArray(); SetCharacterInfo(tbls, fontObj); this.ShowNotification(new GUIContent("Complete")); }
// get font table one line. ChrRect GetChrRect(string line, float imgw, float imgh) { ChrRect d = new ChrRect(); foreach (string s in line.Split(' ')) { if (s.IndexOf("id=") >= 0) { d.id = GetParamInt(s, "id="); } else if (s.IndexOf("x=") >= 0) { d.x = GetParamInt(s, "x="); } else if (s.IndexOf("y=") >= 0) { d.y = GetParamInt(s, "y="); } else if (s.IndexOf("width=") >= 0) { d.w = GetParamInt(s, "width="); } else if (s.IndexOf("height=") >= 0) { d.h = GetParamInt(s, "height="); } else if (s.IndexOf("xoffset=") >= 0) { d.xofs = GetParamInt(s, "xoffset="); } else if (s.IndexOf("yoffset=") >= 0) { d.yofs = GetParamInt(s, "yoffset="); } else if (s.IndexOf("xadvance=") >= 0) { d.width = GetParamInt(s, "xadvance="); } } d.index = d.id; d.uvX = (float)d.x / imgw; d.uvY = (float)(imgh - (d.y)) / imgh; d.uvW = (float)d.w / imgw; //d.uvH = (float)-d.h / imgh; d.uvH = (float)-d.h / imgh; d.vertX = (float)d.xofs; d.vertY = (float)d.yofs; d.vertW = d.w; d.vertH = d.h; return(d); }
void CalcChrRect(TextAsset posTbl, Texture tex) { string fileName = AssetDatabase.GetAssetPath(fontPosTbl); string texName = AssetDatabase.GetAssetPath(tex); string fontName = System.IO.Path.GetFileNameWithoutExtension(fileName); string fontPath = fileName.Replace(".fnt", ".fontsettings"); string matPath = fileName.Replace(".fnt", ".mat"); float imgw = (float)tex.width; float imgh = (float)tex.height; string txt = posTbl.text; List <ChrRect> tblList = new List <ChrRect>(); foreach (string line in txt.Split('\n')) { if (line.IndexOf("char id=") == 0) { ChrRect d = GetChrRect(line, imgw, imgh); tblList.Add(d); } } if (tblList.Count == 0) { new GUIContent("Failed"); return; } ChrRect[] tbls = tblList.ToArray(); Font font = new Font(); font.name = fontName; SetCharacterInfo(tbls, font); Material mat = new Material(Shader.Find("Unlit/Transparent")); mat.mainTexture = tex; mat.name = fontName; font.material = mat; Debug.Log(System.IO.Path.GetFileNameWithoutExtension(fileName)); Debug.Log(fileName); AssetDatabase.CreateAsset(mat, matPath); AssetDatabase.CreateAsset(font, fontPath); EditorUtility.SetDirty(mat); EditorUtility.SetDirty(font); AssetDatabase.SaveAssets(); this.ShowNotification(new GUIContent("Complete")); AssetDatabase.Refresh(); }
// set by .fnt(.txt) void CalcChrRect(Font fontObj, TextAsset posTbl, Texture tex) { float imgw = (float)tex.width; float imgh = (float)tex.height; string txt = posTbl.text; List <ChrRect> tblList = new List <ChrRect>(); int asciiStartOffset = int.MaxValue; int maxH = 0; foreach (string line in txt.Split('\n')) { if (line.IndexOf("char id=") == 0) { ChrRect d = GetChrRect(line, imgw, imgh); if (asciiStartOffset > d.id) { asciiStartOffset = d.id; } if (maxH < d.h) { maxH = d.h; } tblList.Add(d); } } ChrRect[] tbls = tblList.ToArray(); // index value for (int i = 0; i < tbls.Length; i++) { tbls[i].index = tbls[i].id - asciiStartOffset; } // make new CharacterInfo SetCharacterInfo(tbls, fontObj); // fontObj.asciiStartOffset = asciiStartOffset; this.ShowNotification(new GUIContent("Complete")); }
// get font table one line. ChrRect GetChrRect(string line, float imgw, float imgh) { ChrRect d = new ChrRect(); foreach (string s in line.Split(' ')) { if (s.IndexOf("id=") >= 0) d.id = GetParamInt(s, "id="); else if (s.IndexOf("x=") >= 0) d.x = GetParamInt(s, "x="); else if (s.IndexOf("y=") >= 0) d.y = GetParamInt(s, "y="); else if (s.IndexOf("width=") >= 0) d.w = GetParamInt(s, "width="); else if (s.IndexOf("height=") >= 0) d.h = GetParamInt(s, "height="); else if (s.IndexOf("xoffset=") >= 0) d.xofs = GetParamInt(s, "xoffset="); else if (s.IndexOf("yoffset=") >= 0) d.yofs = GetParamInt(s, "yoffset="); else if (s.IndexOf("xadvance=") >= 0) d.width = GetParamInt(s, "xadvance="); } d.uvX = (float)d.x / imgw; d.uvY = (float)(imgh - d.y - d.h) / imgh; d.uvW = (float)d.w / imgw; d.uvH = (float)d.h / imgh; d.vertX = (xoffsetEnable) ? (float)d.xofs : 0.0f; d.vertY = -(float)d.yofs; d.vertW = d.w; d.vertH = -d.h; return d; }
// over write custom font by new CharacterInfo void SetCharacterInfo(ChrRect[] tbls, Font fontObj) { CharacterInfo[] nci = new CharacterInfo[tbls.Length]; for (int i = 0; i < tbls.Length; i++) { nci[i].index = tbls[i].index; nci[i].width = tbls[i].width; nci[i].uv.x = tbls[i].uvX; nci[i].uv.y = tbls[i].uvY; nci[i].uv.width = tbls[i].uvW; nci[i].uv.height = tbls[i].uvH; nci[i].vert.x = tbls[i].vertX; nci[i].vert.y = tbls[i].vertY; nci[i].vert.width = tbls[i].vertW; nci[i].vert.height = tbls[i].vertH; } fontObj.characterInfo = nci; }
// set by split void CalcChrRectGrid(Font fontObj, Texture tex, Rect area, int xc, int yc, int num) { float imgw = (float)tex.width; float imgh = (float)tex.height; int fw = (int)(area.width - area.x) / xc; int fh = (int)(area.height - area.y) / yc; List<ChrRect> tblList = new List<ChrRect>(); for (int i = 0; i < num; i++) { int xi = i % xc; int yi = i / xc; ChrRect d = new ChrRect(); d.index = startOffset+i; d.uvX = (float)(area.x + (fw * xi)) / imgw; d.uvY = (float)(imgh - (area.y + (fh * yi) + fh)) / imgh; d.uvW = (float)fw / imgw; d.uvH = (float)fh / imgh; d.vertX = 0; d.vertY = 0; d.vertW = fw; d.vertH = -fh; d.width = fw; tblList.Add(d); } ChrRect[] tbls = tblList.ToArray(); SetCharacterInfo(tbls, fontObj); this.ShowNotification(new GUIContent("Complete")); }
// over write custom font by new CharacterInfo void SetCharacterInfo(ChrRect[] tbls, Font fontObj) { CharacterInfo[] nci = new CharacterInfo[tbls.Length]; for (int i = 0; i < tbls.Length; i++) { nci[i].index = tbls[i].index; nci[i].advance = (int)tbls[i].width; nci[i].uvBottomLeft = new Vector2(tbls[i].uvX, tbls[i].uvY); nci[i].uvTopRight = new Vector2(tbls[i].uvX + tbls[i].uvW, tbls[i].uvY + tbls[i].uvH); nci[i].minX = (int)tbls[i].vertX; nci[i].maxY = (int)tbls[i].vertY; nci[i].maxX = (int)(tbls[i].vertX + tbls[i].vertW); nci[i].minY = (int)(tbls[i].vertY + tbls[i].vertH); } fontObj.characterInfo = nci; EditorUtility.SetDirty(fontObj); }