// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public static bool Parse(exBitmapFont _bitmapFont, Object _fontInfo) { _bitmapFont.Reset(); string fontInfoPath = AssetDatabase.GetAssetPath(_fontInfo); string dirname = Path.GetDirectoryName(fontInfoPath); string line; FileInfo fileInfo = new FileInfo(fontInfoPath); StreamReader reader = fileInfo.OpenText(); int textureHeight = -1; while ((line = reader.ReadLine()) != null) { string[] words = line.Split(' '); if (words[0] == "info") { _bitmapFont.size = int.Parse(ParseValue(words, "size")); } else if (words[0] == "common") { _bitmapFont.lineHeight = int.Parse(ParseValue(words, "lineHeight")); _bitmapFont.baseLine = int.Parse(ParseValue(words, "base")); // _bitmapFont.width = int.Parse ( ParseValue( words, "scaleW" ) ); // _bitmapFont.height = int.Parse ( ParseValue( words, "scaleH" ) ); int pages = int.Parse(ParseValue(words, "pages")); if (pages != 1) { Debug.LogError("Parse Error: only support one page"); return(false); } } else if (words[0] == "page") { // load texture from file string filename = ParseValue(words, "file"); filename = filename.Substring(1, filename.Length - 2); // remove the "" in "foobar.png" string texturePath = Path.Combine(dirname, filename); Texture2D texture = (Texture2D)AssetDatabase.LoadAssetAtPath(texturePath, typeof(Texture2D)); if (texture == null) { Debug.LogError("Parse Failed: The texture " + filename + " not found."); return(false); } if (exEditorUtility.IsValidForBitmapFont(texture) == false) { exEditorUtility.ImportTextureForBitmapFont(texture); } textureHeight = texture.height; // add page info _bitmapFont.texture = texture; } else if (words[0] == "char") { exBitmapFont.CharInfo charInfo = new exBitmapFont.CharInfo(); charInfo.id = int.Parse(ParseValue(words, "id")); charInfo.width = int.Parse(ParseValue(words, "width")); charInfo.height = int.Parse(ParseValue(words, "height")); charInfo.trim_x = int.Parse(ParseValue(words, "x")); charInfo.trim_y = int.Parse(ParseValue(words, "y")); charInfo.xoffset = int.Parse(ParseValue(words, "xoffset")); charInfo.yoffset = int.Parse(ParseValue(words, "yoffset")); charInfo.xadvance = int.Parse(ParseValue(words, "xadvance")); charInfo.rotated = false; // charInfo.page = int.Parse ( ParseValue( words, "page" ) ); // add char info _bitmapFont.charInfos.Add(charInfo); } else if (words[0] == "kerning") { exBitmapFont.KerningInfo kerningInfo = new exBitmapFont.KerningInfo(); kerningInfo.first = int.Parse(ParseValue(words, "first")); kerningInfo.second = int.Parse(ParseValue(words, "second")); kerningInfo.amount = int.Parse(ParseValue(words, "amount")); _bitmapFont.kernings.Add(kerningInfo); } } reader.Close(); _bitmapFont.rawFontGUID = exEditorUtility.AssetToGUID(_fontInfo); _bitmapFont.rawTextureGUID = exEditorUtility.AssetToGUID(_bitmapFont.texture); // revert charInfo uv-y to fit the Unity's uv-coordination. foreach (exBitmapFont.CharInfo charInfo in _bitmapFont.charInfos) { charInfo.trim_y = textureHeight - (charInfo.trim_y + charInfo.height); charInfo.x = charInfo.trim_x; charInfo.y = charInfo.trim_y; } EditorUtility.SetDirty(_bitmapFont); return(true); }
/////////////////////////////////////////////////////////////////////////////// // mesh building functions /////////////////////////////////////////////////////////////////////////////// // ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public void CalculateSize(out float[] _lineWidths, out float[] _kernings, out float _halfWidthScaled, out float _halfHeightScaled, out float _offsetX, out float _offsetY) { if (useMultiline_) { long lines = exStringHelper.CountLinesInString(text_); _lineWidths = new float[lines]; } else { _lineWidths = new float[0]; } _kernings = new float[Mathf.Max(text_.Length - 1, 0)]; float maxWidth = 0.0f; float curWidth = 0.0f; float height = fontInfo_.lineHeight; int curLine = 0; for (int i = 0; i < text_.Length; ++i) { char c = text_[i]; if (c == '\n') { if (useMultiline_) { if (curWidth > maxWidth) { maxWidth = curWidth; } _lineWidths[curLine] = curWidth; curWidth = 0.0f; height = height + fontInfo_.lineHeight + lineSpacing_; ++curLine; } continue; } // if we don't have the character, it will become space. exBitmapFont.CharInfo charInfo = fontInfo_.GetCharInfo(c); if (charInfo != null) { curWidth = curWidth + charInfo.xadvance + tracking_; if (useKerning_) { if (i < text_.Length - 1) { for (int idx = 0; idx < fontInfo_.kernings.Count; ++idx) { exBitmapFont.KerningInfo k = fontInfo_.kernings[idx]; if (k.first == c && k.second == text_[i + 1]) { curWidth += k.amount; _kernings[i] = k.amount; break; } } } } } } if (curWidth > maxWidth) { maxWidth = curWidth; } if (useMultiline_) { _lineWidths[curLine] = curWidth; } Vector2 finalScale = new Vector2(scale_.x * ppfScale_.x, scale_.y * ppfScale_.y); _halfWidthScaled = maxWidth * finalScale.x * 0.5f; _halfHeightScaled = height * finalScale.y * 0.5f; _offsetX = 0.0f; _offsetY = 0.0f; // calculate anchor offset switch (anchor_) { case Anchor.TopLeft: _offsetX = -_halfWidthScaled; _offsetY = -_halfHeightScaled; break; case Anchor.TopCenter: _offsetX = 0.0f; _offsetY = -_halfHeightScaled; break; case Anchor.TopRight: _offsetX = _halfWidthScaled; _offsetY = -_halfHeightScaled; break; case Anchor.MidLeft: _offsetX = -_halfWidthScaled; _offsetY = 0.0f; break; case Anchor.MidCenter: _offsetX = 0.0f; _offsetY = 0.0f; break; case Anchor.MidRight: _offsetX = _halfWidthScaled; _offsetY = 0.0f; break; case Anchor.BotLeft: _offsetX = -_halfWidthScaled; _offsetY = _halfHeightScaled; break; case Anchor.BotCenter: _offsetX = 0.0f; _offsetY = _halfHeightScaled; break; case Anchor.BotRight: _offsetX = _halfWidthScaled; _offsetY = _halfHeightScaled; break; default: _offsetX = 0.0f; _offsetY = 0.0f; break; } _offsetX -= offset_.x; _offsetY += offset_.y; }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ static void ParseFontInfo(exBitmapFont _bitmapFont, Object _fontInfo) { EditorUtility.DisplayProgressBar("Building BitmapFont...", "Parsing font info...", 0.1f); string fontInfoPath = AssetDatabase.GetAssetPath(_fontInfo); string dirname = Path.GetDirectoryName(fontInfoPath); // TODO { // _bitmapFont.fontInfoGUIDs.Add(exEditorHelper.AssetToGUID(_fontInfo)); // } TODO end // DELME { // string[] lines = _textAsset.text.Split ('\n'); // foreach ( string line in lines ) { // } DELME end string line; FileInfo fileInfo = new FileInfo(fontInfoPath); StreamReader reader = fileInfo.OpenText(); while ((line = reader.ReadLine()) != null) { // DISABLE: it is too slow { // EditorUtility.DisplayProgressBar( "Building BitmapFont...", // "Parsing line " + i, // (float)i/(float)lines.Length ); // } DISABLE end string[] words = line.Split(' '); if (words[0] == "info") { _bitmapFont.size = int.Parse(ParseValue(words, "size")); } else if (words[0] == "common") { _bitmapFont.lineHeight = int.Parse(ParseValue(words, "lineHeight")); // _bitmapFont.width = int.Parse ( ParseValue( words, "scaleW" ) ); // _bitmapFont.height = int.Parse ( ParseValue( words, "scaleH" ) ); int pages = int.Parse(ParseValue(words, "pages")); _bitmapFont.pageInfos = new List <exBitmapFont.PageInfo>(pages); for (int i = 0; i < pages; ++i) { _bitmapFont.pageInfos.Add(new exBitmapFont.PageInfo()); } // DISABLE { // if ( pages != 1 ) { // Debug.LogError ( "Parse Error: only support one page" ); // return; // } // } DISABLE end } else if (words[0] == "page") { // check if id is valid int id = int.Parse(ParseValue(words, "id")); if (id >= _bitmapFont.pageInfos.Count) { Debug.LogError("Parse Failed: The page id is exceed the page number"); return; } // load texture from file string filename = ParseValue(words, "file"); filename = filename.Substring(1, filename.Length - 2); // remove the "" in "foobar.png" string texturePath = Path.Combine(dirname, filename); Texture2D texture = (Texture2D)AssetDatabase.LoadAssetAtPath(texturePath, typeof(Texture2D)); if (texture == null) { Debug.LogError("Parse Failed: The texture " + filename + " not found."); return; } // load material, if not exists, create a new one. string filenameNoExt = Path.GetFileNameWithoutExtension(texturePath); string materialPath = Path.Combine(dirname, filenameNoExt) + ".mat"; Material material = (Material)AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material)); if (material == null) { material = new Material(Shader.Find("ex2D/Alpha Blended")); material.mainTexture = texture; AssetDatabase.CreateAsset(material, materialPath); } // add page info _bitmapFont.pageInfos[id].texture = texture; _bitmapFont.pageInfos[id].material = material; } else if (words[0] == "char") { exBitmapFont.CharInfo charInfo = new exBitmapFont.CharInfo(); charInfo.id = int.Parse(ParseValue(words, "id")); charInfo.x = int.Parse(ParseValue(words, "x")); charInfo.y = int.Parse(ParseValue(words, "y")); charInfo.width = int.Parse(ParseValue(words, "width")); charInfo.height = int.Parse(ParseValue(words, "height")); charInfo.xoffset = int.Parse(ParseValue(words, "xoffset")); charInfo.yoffset = int.Parse(ParseValue(words, "yoffset")); charInfo.xadvance = int.Parse(ParseValue(words, "xadvance")); charInfo.page = int.Parse(ParseValue(words, "page")); exBitmapFont.PageInfo pageInfo = _bitmapFont.pageInfos[charInfo.page]; charInfo.uv0 = new Vector2((float)charInfo.x / pageInfo.texture.width, (pageInfo.texture.height - (float)charInfo.y - charInfo.height) / pageInfo.texture.height); _bitmapFont.charInfos.Add(charInfo); } else if (words[0] == "kerning") { exBitmapFont.KerningInfo kerningInfo = new exBitmapFont.KerningInfo(); kerningInfo.first = int.Parse(ParseValue(words, "first")); kerningInfo.second = int.Parse(ParseValue(words, "second")); kerningInfo.amount = int.Parse(ParseValue(words, "amount")); _bitmapFont.kernings.Add(kerningInfo); } } _bitmapFont.RebuildIdToCharInfoTable(); }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ static void ParseFontInfo( exBitmapFont _bitmapFont, Object _fontInfo ) { EditorUtility.DisplayProgressBar( "Building BitmapFont...", "Parsing font info...", 0.1f ); string fontInfoPath = AssetDatabase.GetAssetPath(_fontInfo); string dirname = Path.GetDirectoryName(fontInfoPath); // TODO { // _bitmapFont.fontInfoGUIDs.Add(exEditorHelper.AssetToGUID(_fontInfo)); // } TODO end // DELME { // string[] lines = _textAsset.text.Split ('\n'); // foreach ( string line in lines ) { // } DELME end string line; FileInfo fileInfo = new FileInfo(fontInfoPath); StreamReader reader = fileInfo.OpenText(); while ( (line = reader.ReadLine()) != null ) { // DISABLE: it is too slow { // EditorUtility.DisplayProgressBar( "Building BitmapFont...", // "Parsing line " + i, // (float)i/(float)lines.Length ); // } DISABLE end string[] words = line.Split(' '); if ( words[0] == "info" ) { _bitmapFont.size = int.Parse ( ParseValue( words, "size" ) ); } else if ( words[0] == "common" ) { _bitmapFont.lineHeight = int.Parse ( ParseValue( words, "lineHeight" ) ); // _bitmapFont.width = int.Parse ( ParseValue( words, "scaleW" ) ); // _bitmapFont.height = int.Parse ( ParseValue( words, "scaleH" ) ); int pages = int.Parse( ParseValue( words, "pages" ) ); _bitmapFont.pageInfos = new List<exBitmapFont.PageInfo>(pages); for ( int i = 0; i < pages; ++i ) { _bitmapFont.pageInfos.Add(new exBitmapFont.PageInfo()); } // DISABLE { // if ( pages != 1 ) { // Debug.LogError ( "Parse Error: only support one page" ); // return; // } // } DISABLE end } else if ( words[0] == "page" ) { // check if id is valid int id = int.Parse ( ParseValue( words, "id" ) ); if ( id >= _bitmapFont.pageInfos.Count ) { Debug.LogError("Parse Failed: The page id is exceed the page number"); return; } // load texture from file string filename = ParseValue( words, "file" ); filename = filename.Substring( 1, filename.Length-2 ); // remove the "" in "foobar.png" string texturePath = Path.Combine( dirname, filename ); Texture2D texture = (Texture2D)AssetDatabase.LoadAssetAtPath( texturePath, typeof(Texture2D) ); if ( texture == null ) { Debug.LogError("Parse Failed: The texture " + filename + " not found."); return; } // load material, if not exists, create a new one. string filenameNoExt = Path.GetFileNameWithoutExtension(texturePath); string materialPath = Path.Combine( dirname, filenameNoExt ) + ".mat"; Material material = (Material)AssetDatabase.LoadAssetAtPath( materialPath, typeof(Material) ); if ( material == null ) { material = new Material( Shader.Find("ex2D/Alpha Blended") ); material.mainTexture = texture; AssetDatabase.CreateAsset( material, materialPath ); } // add page info _bitmapFont.pageInfos[id].texture = texture; _bitmapFont.pageInfos[id].material = material; } else if ( words[0] == "char" ) { exBitmapFont.CharInfo charInfo = new exBitmapFont.CharInfo(); charInfo.id = int.Parse ( ParseValue( words, "id" ) ); charInfo.x = int.Parse ( ParseValue( words, "x" ) ); charInfo.y = int.Parse ( ParseValue( words, "y" ) ); charInfo.width = int.Parse ( ParseValue( words, "width" ) ); charInfo.height = int.Parse ( ParseValue( words, "height" ) ); charInfo.xoffset = int.Parse ( ParseValue( words, "xoffset" ) ); charInfo.yoffset = int.Parse ( ParseValue( words, "yoffset" ) ); charInfo.xadvance = int.Parse ( ParseValue( words, "xadvance" ) ); charInfo.page = int.Parse ( ParseValue( words, "page" ) ); exBitmapFont.PageInfo pageInfo = _bitmapFont.pageInfos[charInfo.page]; charInfo.uv0 = new Vector2 ( (float)charInfo.x / pageInfo.texture.width, (pageInfo.texture.height - (float)charInfo.y - charInfo.height) / pageInfo.texture.height ); _bitmapFont.charInfos.Add(charInfo); } else if ( words[0] == "kerning" ) { exBitmapFont.KerningInfo kerningInfo = new exBitmapFont.KerningInfo(); kerningInfo.first = int.Parse ( ParseValue( words, "first" ) ); kerningInfo.second = int.Parse ( ParseValue( words, "second" ) ); kerningInfo.amount = int.Parse ( ParseValue( words, "amount" ) ); _bitmapFont.kernings.Add(kerningInfo); } } _bitmapFont.RebuildIdToCharInfoTable(); }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public static bool Parse( exBitmapFont _bitmapFont, Object _fontInfo ) { _bitmapFont.Reset(); string fontInfoPath = AssetDatabase.GetAssetPath(_fontInfo); string dirname = Path.GetDirectoryName(fontInfoPath); string line; FileInfo fileInfo = new FileInfo(fontInfoPath); StreamReader reader = fileInfo.OpenText(); int textureHeight = -1; while ( (line = reader.ReadLine()) != null ) { string[] words = line.Split(' '); if ( words[0] == "info" ) { _bitmapFont.size = int.Parse ( ParseValue( words, "size" ) ); } else if ( words[0] == "common" ) { _bitmapFont.lineHeight = int.Parse ( ParseValue( words, "lineHeight" ) ); _bitmapFont.baseLine = int.Parse ( ParseValue( words, "base" ) ); // _bitmapFont.width = int.Parse ( ParseValue( words, "scaleW" ) ); // _bitmapFont.height = int.Parse ( ParseValue( words, "scaleH" ) ); int pages = int.Parse( ParseValue( words, "pages" ) ); if ( pages != 1 ) { Debug.LogError ( "Parse Error: only support one page" ); return false; } } else if ( words[0] == "page" ) { // load texture from file string filename = ParseValue( words, "file" ); filename = filename.Substring( 1, filename.Length-2 ); // remove the "" in "foobar.png" string texturePath = Path.Combine( dirname, filename ); Texture2D texture = (Texture2D)AssetDatabase.LoadAssetAtPath( texturePath, typeof(Texture2D) ); if ( texture == null ) { Debug.LogError("Parse Failed: The texture " + filename + " not found."); return false; } if ( exEditorUtility.IsValidForBitmapFont(texture) == false ) { exEditorUtility.ImportTextureForBitmapFont(texture); } textureHeight = texture.height; // add page info _bitmapFont.texture = texture; } else if ( words[0] == "char" ) { exBitmapFont.CharInfo charInfo = new exBitmapFont.CharInfo(); charInfo.id = int.Parse ( ParseValue( words, "id" ) ); charInfo.width = int.Parse ( ParseValue( words, "width" ) ); charInfo.height = int.Parse ( ParseValue( words, "height" ) ); charInfo.trim_x = int.Parse ( ParseValue( words, "x" ) ); charInfo.trim_y = int.Parse ( ParseValue( words, "y" ) ); charInfo.xoffset = int.Parse ( ParseValue( words, "xoffset" ) ); charInfo.yoffset = int.Parse ( ParseValue( words, "yoffset" ) ); charInfo.xadvance = int.Parse ( ParseValue( words, "xadvance" ) ); charInfo.rotated = false; // charInfo.page = int.Parse ( ParseValue( words, "page" ) ); // add char info _bitmapFont.charInfos.Add(charInfo); } else if ( words[0] == "kerning" ) { exBitmapFont.KerningInfo kerningInfo = new exBitmapFont.KerningInfo(); kerningInfo.first = int.Parse ( ParseValue( words, "first" ) ); kerningInfo.second = int.Parse ( ParseValue( words, "second" ) ); kerningInfo.amount = int.Parse ( ParseValue( words, "amount" ) ); _bitmapFont.kernings.Add(kerningInfo); } } reader.Close(); _bitmapFont.rawFontGUID = exEditorUtility.AssetToGUID(_fontInfo); _bitmapFont.rawTextureGUID = exEditorUtility.AssetToGUID(_bitmapFont.texture); // revert charInfo uv-y to fit the Unity's uv-coordination. foreach ( exBitmapFont.CharInfo charInfo in _bitmapFont.charInfos ) { charInfo.trim_y = textureHeight - (charInfo.trim_y + charInfo.height); charInfo.x = charInfo.trim_x; charInfo.y = charInfo.trim_y; } EditorUtility.SetDirty(_bitmapFont); return true; }