コード例 #1
0
ファイル: exAtlasInfo.cs プロジェクト: exdev/ex2d-v1
    // ------------------------------------------------------------------
    /// \param _objects 
    /// get the Texture2D and exBitmapFont from a list of objects, import them into atlas
    // ------------------------------------------------------------------
    public void ImportObjects( Object[] _objects )
    {
        bool dirty = false;
        foreach ( Object o in _objects ) {
            if ( o is Texture2D ) {
                Texture2D t = o as Texture2D;
                exAtlasDB.ElementInfo elInfo = exAtlasDB.GetElementInfo(t);
                if ( elInfo == null ) {
                    AddElement( t, trimElements );
                    dirty = true;
                }
                else {
                    Debug.LogError( "The texture [" + t.name + "]" +
                                    " has already been added in atlas: " +
                                    AssetDatabase.GUIDToAssetPath(elInfo.guidAtlasInfo) );
                }
            }
            else if ( o is exBitmapFont ) {
                exBitmapFont f = o as exBitmapFont;
                if ( f.inAtlas ) {
                    // NOTE: it is still possible we have atlas font in the obj list since we use Selection.GetFiltered().
                    continue;
                }

                // multi-page atlas font is forbit
                if ( f.pageInfos.Count > 1 ) {
                    Debug.LogError("Can't not create atlas font from " + f.name + ", it has multiple page info.");
                    continue;
                }

                // check if we have resource in the project
                string assetPath = AssetDatabase.GetAssetPath(texture);
                string dirname = Path.GetDirectoryName(assetPath);
                string filename = Path.GetFileNameWithoutExtension(assetPath);
                string bitmapFontPath = Path.Combine( dirname, filename + " - " + f.name + ".asset" );
                exBitmapFont f2 = (exBitmapFont)AssetDatabase.LoadAssetAtPath( bitmapFontPath,
                                                                               typeof(exBitmapFont) );
                if ( f2 == null ) {
                    f2 = (exBitmapFont)ScriptableObject.CreateInstance(typeof(exBitmapFont));
                    f2.inAtlas = true;
                    f2.name = f.name;
                    f2.lineHeight = f.lineHeight;

                    // add page info
                    exBitmapFont.PageInfo pageInfo = new exBitmapFont.PageInfo();
                    pageInfo.texture = texture;
                    pageInfo.material = material;
                    f2.pageInfos.Add(pageInfo);

                    // add char info
                    foreach ( exBitmapFont.CharInfo c in f.charInfos ) {
                        exBitmapFont.CharInfo c2 = new exBitmapFont.CharInfo(c);
                        f2.charInfos.Add(c2);
                    }

                    // add kerning info
                    foreach ( exBitmapFont.KerningInfo k in f.kernings ) {
                        f2.kernings.Add(k);
                    }

                    AssetDatabase.CreateAsset ( f2, bitmapFontPath );

                    //
                    foreach ( exBitmapFont.CharInfo c in f2.charInfos ) {
                        if ( c.id == -1 )
                            continue;
                        AddFontElement( f, f2, c );
                    }
                }
                else {
                    Debug.LogError("You already add the BitmapFont in this Atlas");
                }

                //
                if ( bitmapFonts.IndexOf(f2) == -1 ) {
                    bitmapFonts.Add(f2);
                }

                dirty = true;
            }
            if ( dirty ) {
                EditorUtility.SetDirty(this);
            }
        }
    }
コード例 #2
0
    // ------------------------------------------------------------------
    /// \param _objects
    /// get the Texture2D and exBitmapFont from a list of objects, import them into atlas
    // ------------------------------------------------------------------

    public void ImportObjects(Object[] _objects)
    {
        bool dirty = false;

        foreach (Object o in _objects)
        {
            if (o is Texture2D)
            {
                Texture2D             t      = o as Texture2D;
                exAtlasDB.ElementInfo elInfo = exAtlasDB.GetElementInfo(t);
                if (elInfo == null)
                {
                    AddElement(t, trimElements);
                    dirty = true;
                }
                else
                {
                    Debug.LogError("The texture [" + t.name + "]" +
                                   " has already been added in atlas: " +
                                   AssetDatabase.GUIDToAssetPath(elInfo.guidAtlasInfo));
                }
            }
            else if (o is exBitmapFont)
            {
                exBitmapFont f = o as exBitmapFont;
                if (f.inAtlas)
                {
                    // NOTE: it is still possible we have atlas font in the obj list since we use Selection.GetFiltered().
                    continue;
                }

                // multi-page atlas font is forbit
                if (f.pageInfos.Count > 1)
                {
                    Debug.LogError("Can't not create atlas font from " + f.name + ", it has multiple page info.");
                    continue;
                }

                // check if we have resource in the project
                string       assetPath      = AssetDatabase.GetAssetPath(texture);
                string       dirname        = Path.GetDirectoryName(assetPath);
                string       filename       = Path.GetFileNameWithoutExtension(assetPath);
                string       bitmapFontPath = Path.Combine(dirname, filename + " - " + f.name + ".asset");
                exBitmapFont f2             = (exBitmapFont)AssetDatabase.LoadAssetAtPath(bitmapFontPath,
                                                                                          typeof(exBitmapFont));
                if (f2 == null)
                {
                    f2            = (exBitmapFont)ScriptableObject.CreateInstance(typeof(exBitmapFont));
                    f2.inAtlas    = true;
                    f2.name       = f.name;
                    f2.lineHeight = f.lineHeight;

                    // add page info
                    exBitmapFont.PageInfo pageInfo = new exBitmapFont.PageInfo();
                    pageInfo.texture  = texture;
                    pageInfo.material = material;
                    f2.pageInfos.Add(pageInfo);

                    // add char info
                    foreach (exBitmapFont.CharInfo c in f.charInfos)
                    {
                        exBitmapFont.CharInfo c2 = new exBitmapFont.CharInfo(c);
                        f2.charInfos.Add(c2);
                    }

                    // add kerning info
                    foreach (exBitmapFont.KerningInfo k in f.kernings)
                    {
                        f2.kernings.Add(k);
                    }

                    AssetDatabase.CreateAsset(f2, bitmapFontPath);

                    //
                    foreach (exBitmapFont.CharInfo c in f2.charInfos)
                    {
                        if (c.id == -1)
                        {
                            continue;
                        }
                        AddFontElement(f, f2, c);
                    }
                }
                else
                {
                    Debug.LogError("You already add the BitmapFont in this Atlas");
                }

                //
                if (bitmapFonts.IndexOf(f2) == -1)
                {
                    bitmapFonts.Add(f2);
                }

                dirty = true;
            }
            if (dirty)
            {
                EditorUtility.SetDirty(this);
            }
        }
    }
コード例 #3
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();
    }