Unity UGUI 散图转字体
废话不多说,直接上步骤
1.首先下载BMFont工具,下载地址: http://www.angelcode.com/products/bmfont/
2.解压后运行bmfont32.exe或者bmfont64.exe,运行跟自己系统对应的!
3.

4.设置导出的尺寸(根据自己的图片大小去调整),选32,生成的信息文件Xml格式,导出的图为png后点击OK

5.

6.开始导入散图,选择散图(每次只能选一个)

7.id对应的是ASCII码(ASCII码对照:http://c.biancheng.net/c/ascii/),

8.ASCII码还可以在BitMap查看

9.所有要导入的完成后,导出
10.导出的文件有2个(*.fnt和*.png)
11.接下来就是要在unity操作了
12.上代码:
using UnityEngine;
using UnityEditor;
using System.Xml;
using System;
public class BitmapFontExporter : ScriptableWizard
{
[MenuItem("BitmapFontExporter/Create")]
private static void CreateFont()
{
DisplayWizard<BitmapFontExporter>("Create Font");
}
public TextAsset fontFile;
public Texture2D textureFile;
private void OnWizardCreate()
{
if (fontFile == null || textureFile == null)
{
return;
}
string path = EditorUtility.SaveFilePanelInProject("Save Font", fontFile.name, "", "");
if (!string.IsNullOrEmpty(path))
{
ResolveFont(path);
}
}
private void ResolveFont(string exportPath)
{
if (!fontFile) throw new UnityException(fontFile.name + "is not a valid font-xml file");
Font font = new Font();
XmlDocument xml = new XmlDocument();
xml.LoadXml(fontFile.text);
XmlNode info = xml.GetElementsByTagName("info")[0];
XmlNodeList chars = xml.GetElementsByTagName("chars")[0].ChildNodes;
CharacterInfo[] charInfos = new CharacterInfo[chars.Count];
for (int cnt = 0; cnt < chars.Count; cnt++)
{
XmlNode node = chars[cnt];
CharacterInfo charInfo = new CharacterInfo();
charInfo.index = ToInt(node, "id");
charInfo.width = ToInt(node, "xadvance");
charInfo.uv = GetUV(node);
charInfo.vert = GetVert(node);
charInfos[cnt] = charInfo;
}
Shader shader = Shader.Find("Unlit/Transparent");
Material material = new Material(shader);
material.mainTexture = textureFile;
AssetDatabase.CreateAsset(material, exportPath + ".mat");
font.material = material;
font.name = info.Attributes.GetNamedItem("face").InnerText;
font.characterInfo = charInfos;
AssetDatabase.CreateAsset(font, exportPath + ".fontsettings");
}
private Rect GetUV(XmlNode node)
{
Rect uv = new Rect();
uv.x = ToFloat(node, "x") / textureFile.width;
uv.y = ToFloat(node, "y") / textureFile.height;
uv.width = ToFloat(node, "width") / textureFile.width;
uv.height = ToFloat(node, "height") / textureFile.height;
uv.y = 1f - uv.y - uv.height;
return uv;
}
private Rect GetVert(XmlNode node)
{
Rect uv = new Rect();
uv.x = ToFloat(node, "xoffset");
uv.y = ToFloat(node, "yoffset");
uv.width = ToFloat(node, "width");
uv.height = ToFloat(node, "height");
uv.y = -uv.y;
uv.height = -uv.height;
return uv;
}
private int ToInt(XmlNode node, string name)
{
return Convert.ToInt32(node.Attributes.GetNamedItem(name).InnerText);
}
private float ToFloat(XmlNode node, string name)
{
return (float) ToInt(node, name);
}
}
13.unity的菜单中选择

14.把刚才导入的2个文件分别导入,点击Create,

15大功告成

注:不能通过调整Font Size来调整显示大小(我目前了解到是不行,如果各位有什么好的处理方式,请告知我)
参考连接:https://zhuanlan.zhihu.com/p/93201650

浙公网安备 33010602011771号