コード例 #1
0
        private void ExportFont(BitmapFontConfig config)
        {
            if (!config.IsValid())
            {
                Debug.LogError("FontConfig is unvalid");
                return;
            }

            int charIndex             = charIDStart;
            List <Texture2D> textures = new List <Texture2D>();

            for (int i = 0; i < config.fontChars.Count; ++i)
            {
                BitmapFontChar fontChar = config.fontChars[i];
                fontChar.charIndexes = new int[fontChar.chars.Count];
                for (int j = 0; j < fontChar.chars.Count; ++j)
                {
                    fontChar.charIndexes[j] = (charIndex++);

                    textures.Add(fontChar.textures[j]);

                    SetCharTextureSetting(fontChar.textures[j]);
                }
            }

            Texture2D atlas = PackCharTexture(config.GetFontTexturePath(), textures.ToArray(), config.padding, config.maxSize, out Rect[] rects);

            if (atlas == null)
            {
                Debug.LogError("PackCharTexture failed");
                return;
            }
            int rectIndex = 0;

            for (int i = 0; i < config.fontChars.Count; ++i)
            {
                BitmapFontChar fontChar = config.fontChars[i];
                fontChar.charRects = new Rect[fontChar.chars.Count];
                for (int j = 0; j < fontChar.chars.Count; ++j)
                {
                    fontChar.charRects[j] = rects[rectIndex];
                    ++rectIndex;
                }
            }

            Font font = CreateFont(config, atlas, out BitmapFontCharMap[] charMaps);

            string     fontDataPath = config.GetFontDataPath();
            BitmapFont fontData     = AssetDatabase.LoadAssetAtPath <BitmapFont>(fontDataPath);

            if (fontData == null)
            {
                fontData = ScriptableObject.CreateInstance <BitmapFont>();
                AssetDatabase.CreateAsset(fontData, fontDataPath);
                AssetDatabase.ImportAsset(fontDataPath);
            }

            fontData.bmFont   = font;
            fontData.charMaps = charMaps;

            EditorUtility.SetDirty(fontData);
        }
コード例 #2
0
        private Font CreateFont(BitmapFontConfig config, Texture2D atlas, out BitmapFontCharMap[] charMap)
        {
            string fontAssetPath = config.GetFontPath();

            charMap = new BitmapFontCharMap[config.fontChars.Count];

            Font font = AssetDatabase.LoadMainAssetAtPath(fontAssetPath) as Font;

            if (font == null)
            {
                font = new Font();
                AssetDatabase.CreateAsset(font, fontAssetPath);
                AssetDatabase.ImportAsset(fontAssetPath);
            }
            Shader matShader = Shader.Find(MATERIAL_SHADER);

            Material fontMat = AssetDatabase.LoadAssetAtPath <Material>(fontAssetPath);

            if (fontMat == null)
            {
                fontMat      = new Material(matShader);
                fontMat.name = "Font Material";
                AssetDatabase.AddObjectToAsset(fontMat, fontAssetPath);

                font.material = fontMat;
            }

            fontMat.mainTexture = atlas;

            List <CharacterInfo> charInfos = new List <CharacterInfo>();

            for (int i = 0; i < config.fontChars.Count; ++i)
            {
                BitmapFontChar fontChar = config.fontChars[i];
                charMap[i] = new BitmapFontCharMap()
                {
                    name     = fontChar.fontName,
                    orgChars = fontChar.chars.ToArray(),
                    mapChars = (from index in fontChar.charIndexes select(char) index).ToArray(),
                };

                for (int j = 0; j < fontChar.chars.Count; ++j)
                {
                    CharacterInfo cInfo = new CharacterInfo();
                    cInfo.index = fontChar.charIndexes[j];
                    Rect rect = fontChar.charRects[j];

                    cInfo.uvBottomLeft  = new Vector2(rect.x, rect.y);
                    cInfo.uvTopRight    = new Vector2(rect.x + rect.width, rect.y + rect.height);
                    cInfo.uvBottomRight = new Vector2(rect.x + rect.width, rect.y);
                    cInfo.uvTopLeft     = new Vector2(rect.x, rect.y + rect.height);

                    Rect vert = new Rect(0, -rect.height * atlas.height, rect.width * atlas.width, rect.height * atlas.height);
                    cInfo.minX = (int)vert.xMin;
                    cInfo.maxX = (int)vert.xMax;
                    cInfo.minY = (int)vert.yMin;
                    cInfo.maxY = -(int)vert.yMax;

                    cInfo.glyphWidth  = Mathf.RoundToInt(atlas.width * rect.width);
                    cInfo.glyphHeight = Mathf.RoundToInt(atlas.height * rect.height);

                    cInfo.advance = cInfo.glyphWidth + fontChar.charSpace;
                    cInfo.bearing = 0;

                    cInfo.style = FontStyle.Normal;

                    charInfos.Add(cInfo);
                }
            }

            font.characterInfo = charInfos.ToArray();

            EditorUtility.SetDirty(font);
            return(font);
        }
コード例 #3
0
        private void DrawToolbar(Rect rect)
        {
            EditorGUI.LabelField(rect, GUIContent.none, EditorStyles.toolbar);

            Rect createRect = rect;

            createRect.width = Styles.toolbarWidth;
            if (GUI.Button(createRect, Contents.creatContent, EditorStyles.toolbarButton))
            {
                string filePath = EditorUtility.SaveFilePanel("Create Font Config", Application.dataPath, "bitmap_font_config", "asset");
                if (!string.IsNullOrEmpty(filePath))
                {
                    BitmapFontConfig config = ScriptableObject.CreateInstance <BitmapFontConfig>();
                    config.name = Path.GetFileNameWithoutExtension(filePath);

                    string fileAssetPath = PathUtility.GetAssetPath(filePath);
                    AssetDatabase.CreateAsset(config, fileAssetPath);
                    AssetDatabase.ImportAsset(fileAssetPath);

                    fontConfigs.Add(config);
                    fontConfigListView.AddItem(config);
                    SelectedChanged(fontConfigs.Count - 1);
                }
            }

            Rect deleteRect = createRect;

            deleteRect.x += createRect.width;
            EditorGUI.BeginDisabledGroup(selectedIndex < 0);
            {
                if (GUI.Button(deleteRect, Contents.deleteContent, EditorStyles.toolbarButton))
                {
                    if (selectedIndex > 0)
                    {
                        BitmapFontConfig font = fontConfigs[selectedIndex];
                        AssetDatabase.DeleteAsset(font.GetFontDataPath());
                        AssetDatabase.DeleteAsset(font.GetFontPath());
                        AssetDatabase.DeleteAsset(font.GetFontTexturePath());

                        fontConfigs.RemoveAt(selectedIndex);
                        fontConfigListView.RemoveAt(selectedIndex);

                        AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(font));

                        SelectedChanged(-1);
                    }
                }
            }
            EditorGUI.EndDisabledGroup();

            Rect exportRect = deleteRect;

            exportRect.x += deleteRect.width;
            EditorGUI.BeginDisabledGroup(selectedIndex < 0);
            {
                if (GUI.Button(exportRect, Contents.exportContent, EditorStyles.toolbarButton))
                {
                    ExportFont(fontConfigs[selectedIndex]);
                    AssetDatabase.SaveAssets();
                }
            }
            EditorGUI.EndDisabledGroup();

            Rect exportAllRect = rect;

            exportAllRect.x    += rect.width - Styles.toolbarWidth;
            exportAllRect.width = Styles.toolbarWidth;
            if (GUI.Button(exportAllRect, Contents.exportAllContent, EditorStyles.toolbarButton))
            {
                foreach (var config in fontConfigs)
                {
                    ExportFont(config);
                }
                AssetDatabase.SaveAssets();
            }
        }