Пример #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 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();
            }
        }