自己写的BMFont导入工具,快速把图片转换为美术字体
配合BMFont工具使用可以快速把圖片轉換為美術字,在項目中使用美術字體,需要準備一個材質球 一個空白字體,然后自己改一下路徑,這三個腳本是分開的 ,先點art。
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
public class ArtFontTool
{
? ? static Dictionary<Image, string> imageDic = null;
? ? [MenuItem("Font/Make ArtFont")]
? ? static public void GenAllFont()
? ? {
? ? ? ? imageDic = new Dictionary<Image, string>();
? ? ? ? string prefabs = Application.dataPath + "/BundleResources/UI/Prefabs/";
? ? ? ? foreach (var dir in Directory.GetFiles(prefabs,"*.prefab"))
? ? ? ? {
? ? ? ? ? ? ProcPrefab(dir.Substring(prefabs.Length));
? ? ? ? }
? ? ? ? AssetDatabase.SaveAssets();
? ? }
? ? static void ProcPrefab(string prefab)
? ? {
? ? ? ? Debug.Log("------------------------------------");
? ? ? ? Debug.Log(prefab);
? ? ? ? var prefabName = "Assets/BundleResources/UI/Prefabs/" + prefab;
? ? ? ? GameObject go = AssetDatabase.LoadAssetAtPath<GameObject>(prefabName);
? ? ? ? if(go == null)
? ? ? ? {
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? imageDic.Clear();
? ? ? ? Image[] imgs = go.GetComponentsInChildren<Image>(true);
? ? ? ? for(int i = 0; i < imgs.Length; ++i)
? ? ? ? {
? ? ? ? ? ? Image img = imgs[i];
? ? ? ? ? ? //var hash = img.mainTexture.imageContentsHash;
? ? ? ? ? ? //var sobj = new SerializedObject(img.sprite);
? ? ? ? ? ? Sprite s = img.sprite;
? ? ? ? ? ? string path = AssetDatabase.GetAssetPath(s.GetInstanceID());
? ? ? ? ? ? string relativeUrl = path.Replace("Assets/BundleResources/", "");
? ? ? ? ? ? if(relativeUrl.StartsWith("UI/ArtFont/Atlas"))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Debug.Log(path + "------------->" + img.name);
? ? ? ? ? ? ? ? imageDic.Add(img,relativeUrl);
? ? ? ? ? ? ? ? img.sprite = null;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? if(imageDic.Count > 0)
? ? ? ? {
? ? ? ? ? ? ArtFontParser parser = go.GetComponent<ArtFontParser>();
? ? ? ? ? ? if(parser == null)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? parser = go.AddComponent<ArtFontParser>();
? ? ? ? ? ? }
? ? ? ? ? ? var it = imageDic.GetEnumerator();
? ? ? ? ? ? while(it.MoveNext())
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if(!parser.ImageDic.ContainsKey(it.Current.Key))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? parser.ImageDic.Add(it.Current.Key, it.Current.Value);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? AssetDatabase.Refresh(); ? ? ?
? ? }
}
?
?
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ArtFontParser : MonoBehaviour , ISerializationCallbackReceiver
{
? ? public List<string> UrlList = new List<string>();
? ? public List<Image> ImageList = new List<Image>();
? ? //Unity doesn't know how to serialize a Dictionary
? ? public Dictionary<Image, string> ImageDic = new Dictionary<Image, string>();
? ? private List<KEngine.SpriteLoader> loaders = new List<KEngine.SpriteLoader>();
? ? public void OnBeforeSerialize()
? ? {
? ? ? ? UrlList.Clear();
? ? ? ? ImageList.Clear();
? ? ? ? foreach (var kvp in ImageDic)
? ? ? ? {
? ? ? ? ? ? UrlList.Add(kvp.Value);
? ? ? ? ? ? ImageList.Add(kvp.Key);
? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? }
? ? public void OnAfterDeserialize()
? ? {
? ? ? ? ImageDic = new Dictionary<Image, string>();
? ? ? ? for (int i = 0; i != Math.Min(UrlList.Count, ImageList.Count); i++)
? ? ? ? {
? ? ? ? ? ? ImageDic.Add(ImageList[i], UrlList[i]);
? ? ? ? }
? ? ? ? ? ??
? ? }
? ? void Awake()
? ? {
? ? ? ? string lan = KEngine.KTool.GetLanguageSimplify();
? ? ? ? lan = string.Format("/{0}/", lan);
? ? ? ? var it = ImageDic.GetEnumerator();
? ? ? ? while (it.MoveNext())
? ? ? ? {
? ? ? ? ? ? var key = it.Current.Value;
? ? ? ? ? ? key = key.Replace("/CH/", lan);
? ? ? ? ? ? var loader = KEngine.SpriteLoader.Load(key, (bool isOk, Sprite tex) =>
? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ?if (isOk)
? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ?it.Current.Key.sprite = tex;
? ? ? ? ? ? ? ? ? ? ?it.Current.Key.SetNativeSize();
? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ?});
? ? ? ? ? ? loaders.Add(loader);
? ? ? ? }
? ? }
? ? void OnDestroy()
? ? {
? ? ? ? for (int i = 0; i < loaders.Count; ++i)
? ? ? ? {
? ? ? ? ? ? var loader = loaders[i];
? ? ? ? ? ? if (loader != null)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? loader.Release();
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? loaders = null;
? ? }
}
?
?
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using UnityEditor;
using UnityEngine;
public class FontTool
{
? ? [MenuItem("Font/Make BmFont")]
? ? static void GenAllFont()
? ? {
? ? ? ? string fontPath = Application.dataPath + "/BundleResources/BMFont/";
? ? ? ? foreach (var dir in Directory.GetDirectories(fontPath))
? ? ? ? {
? ? ? ? ? ? GetFont(dir.Substring(fontPath.Length));
? ? ? ? }
? ? }
? ? static void GetFont(string fontName)
? ? {
? ? ? ? var root = string.Format("Assets/BundleResources/BMFont/{0}/", fontName);
? ? ? ? Material mtr = AssetDatabase.LoadAssetAtPath<Material>(string.Format("{0}{1}.mat",root,fontName));
? ? ? ? Texture2D texture = AssetDatabase.LoadAssetAtPath<Texture2D>(string.Format("{0}{1}_0.png",root,fontName));
? ? ? ? mtr.mainTexture = texture;
? ? ? ? Font font = AssetDatabase.LoadAssetAtPath<Font>(string.Format("{0}{1}.fontsettings",root,fontName));
? ? ? ? XmlDocument xml = new XmlDocument();
? ? ? ? xml.Load(string.Format("{0}/BundleResources/BMFont/{1}/{2}.fnt", Application.dataPath,fontName,fontName));
? ? ? ? List<CharacterInfo> chtInfoList = new List<CharacterInfo>();
? ? ? ? XmlNode node = xml.SelectSingleNode("font/chars");
? ? ? ? foreach(XmlNode nd in node.ChildNodes)
? ? ? ? {
? ? ? ? ? ? XmlElement xe = (XmlElement)nd;
? ? ? ? ? ? int x = int.Parse(xe.GetAttribute("x"));
? ? ? ? ? ? int y = int.Parse(xe.GetAttribute("y"));
? ? ? ? ? ? int width = int.Parse(xe.GetAttribute("width"));
? ? ? ? ? ? int height = int.Parse(xe.GetAttribute("height"));
? ? ? ? ? ? int advance = int.Parse(xe.GetAttribute("xadvance"));
? ? ? ? ? ? CharacterInfo chtInfo = new CharacterInfo();
? ? ? ? ? ? float texWidth = texture.width;
? ? ? ? ? ? float texHeight = texture.height;//width
? ? ? ? ? ? chtInfo.glyphHeight = texture.height;
? ? ? ? ? ? chtInfo.glyphWidth = texture.width;
? ? ? ? ? ? chtInfo.index = int.Parse(xe.GetAttribute("id"));
? ? ? ? ? ? chtInfo.uvTopLeft = new Vector2((float)x /texture.width, 1 - (float)y/ texture.height);
? ? ? ? ? ? chtInfo.uvTopRight = new Vector2((float)(x + width) / texture.width, 1 - (float)y / texture.height);
? ? ? ? ? ? chtInfo.uvBottomLeft = new Vector2((float)x / texture.width, 1 - (float)(y + height) / texture.height);
? ? ? ? ? ? chtInfo.uvBottomRight = new Vector2((float)(x + width) / texture.width, 1 - (float)(y + height) / texture.height);
? ? ? ? ? ? chtInfo.minX = 0;
? ? ? ? ? ? chtInfo.minY = -(height / 2);
? ? ? ? ? ? chtInfo.maxX = width;
? ? ? ? ? ? chtInfo.maxY = height / 2;
? ? ? ? ? ? chtInfo.advance = width;
? ? ? ? ? ? chtInfoList.Add(chtInfo);
? ? ? ? }
? ? ? ? font.characterInfo = chtInfoList.ToArray();
? ? ? ? EditorUtility.SetDirty(font);
? ? ? ? AssetDatabase.SaveAssets();
? ? ? ? AssetDatabase.Refresh();
? ? ? ? Debug.Log("BMFont gen Done ->" + root);
? ? }
}
總結
以上是生活随笔為你收集整理的自己写的BMFont导入工具,快速把图片转换为美术字体的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 学习BMFONT的感想
- 下一篇: bmfont的设置