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"