unity制作位图字体
第一步: ps 制作好 艺术字体,每个一样宽一样高。
第二步: 使用 bmfont 软件,将前面做好的小图转成fnt和png。(下载地址:https://www.angelcode.com/products/bmfont/)
<?xml version="1.0"?>
<font>
<info face="Arial" size="32" bold="0" italic="0" charset="" unicode="1" stretchH="100" smooth="1" aa="1" padding="0,0,0,0" spacing="1,1" outline="0"/>
<common lineHeight="32" base="26" scaleW="256" scaleH="256" pages="1" packed="0" alphaChnl="1" redChnl="0" greenChnl="0" blueChnl="0"/>
<pages>
<page id="0" file="number0_0.png" />
</pages>
<chars count="10">
<char id="48" x="0" y="0" width="28" height="35" xoffset="0" yoffset="0" xadvance="28" page="0" chnl="15" />
<char id="49" x="29" y="0" width="28" height="35" xoffset="0" yoffset="0" xadvance="28" page="0" chnl="15" />
<char id="50" x="58" y="0" width="28" height="35" xoffset="0" yoffset="0" xadvance="28" page="0" chnl="15" />
<char id="51" x="87" y="0" width="28" height="35" xoffset="0" yoffset="0" xadvance="28" page="0" chnl="15" />
<char id="52" x="116" y="0" width="28" height="35" xoffset="0" yoffset="0" xadvance="28" page="0" chnl="15" />
<char id="53" x="145" y="0" width="28" height="35" xoffset="0" yoffset="0" xadvance="28" page="0" chnl="15" />
<char id="54" x="174" y="0" width="28" height="35" xoffset="0" yoffset="0" xadvance="28" page="0" chnl="15" />
<char id="55" x="203" y="0" width="28" height="35" xoffset="0" yoffset="0" xadvance="28" page="0" chnl="15" />
<char id="56" x="0" y="36" width="28" height="35" xoffset="0" yoffset="0" xadvance="28" page="0" chnl="15" />
<char id="57" x="29" y="36" width="28" height="35" xoffset="0" yoffset="0" xadvance="28" page="0" chnl="15" />
</chars>
</font>
第三步: 导入 fnt,png 文件到 unity工程里边。
第四步: 增加 菜单脚本:
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Xml;
using System;
public class BitmapFontExporter : ScriptableWizard
{
[MenuItem("BitmapFontExporter/Create")]
private static void CreateFont()
{
ScriptableWizard.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);
}
}
第五步: 上边的菜单脚本会生成一个菜单,点击unity 菜单 BitmapFontExporter->Create 选中刚才的ftn文件和png文件,然后点击生成.
然后就可以保存 字体文件了,这个字体文件只能使用到txt中,txt 创建位置在 ui->legacy->txt
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2020-07-31 查看暂用端口号