RebuildIdToCharInfoTable() public method

public RebuildIdToCharInfoTable ( ) : void
return void
コード例 #1
0
ファイル: exBitmapFontUtility.cs プロジェクト: exdev/ex2d-v1
    // ------------------------------------------------------------------
    // 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();
    }
コード例 #2
0
    // ------------------------------------------------------------------
    // 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();
    }