一、BmFont下载地址
1.双击下载好的软件
2.软件初始界面
二、制作位图字体
1.首先,我们来制作一个字符:
Edit–>OpenImgageManager,打开ImageManager界面
2.Image->importImage,选择图片
3.选择要导入的一张字符图片后会出现如下弹窗:
注意Id,id对应字符的ASCII码。
那么,如何知道某个字符的id或者ASCII码呢,接下来就回到上文了,还记得上文介绍BmFont界面时的介绍的右下方吗,第一个值(48)就是我们需要填写的id值。
那么这时候问题又来了,如何查看每个字符的id值呢?不要担心,其实很简单,只需要把鼠标移动到中间区域对应的字符上,即可在右下方查看,如图所示:
BMfont演示
4.导入成功后,我们会在ImageManager界面看到字符信息:
左边代表id,右边是对应的美术资源路径。
5.将需要制作位图字体的美术资源添加完成。
6.接下来就是设置位图了。选择Options->Export options
7.打开导出选项界面。
1、设置整张位图的长款。
2、设置字体的导出格式。
3、确认设置。
8.接下来就是保存位图了,选择Options-> Save bitmap font as
选择你要保存的路径,并修改命名,这里我改为lvfont并保存。保存成功后会得到两个文件,分别是.png和.fnt
三、Unity使用位图字体
1.新建Font文件夹,并将上面制作好的位图字体拖曳进去。
2.新建一个Materia设置如下:GUI–>TextShader
3.新建CustomFont,将上文创建的材质球拖到如图所示位置,设置如下:
此时你会发现新建Text,使用新字体,没有任何显示,这时候你查看会发现font.mat里面是空的。
4.下面我们就来为font.mat复制,新建C#脚本CustomFontImportor,添加如下代码
using UnityEngine;
using System.Collections;
using System.Xml;
using System;
public class CustomFontImportor : MonoBehaviour
{
public Font font;
public TextAsset textAsset;
void Awake()
{
if (font == null || textAsset == null)
{
//Debug.LogError("请设置font和textAsset.");
return;
}
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(textAsset.text);
int totalWidth = Convert.ToInt32(xmlDocument["font"]["common"].Attributes["scaleW"].InnerText);
int totalHeight = Convert.ToInt32(xmlDocument["font"]["common"].Attributes["scaleH"].InnerText);
XmlElement xml = xmlDocument["font"]["chars"];
ArrayList characterInfoList = new ArrayList();
for (int i = 0; i < xml.ChildNodes.Count; ++i)
{
XmlNode node = xml.ChildNodes[i];
if (node.Attributes == null)
{
continue;
}
int index = Convert.ToInt32(node.Attributes["id"].InnerText);
int x = Convert.ToInt32(node.Attributes["x"].InnerText);
int y = Convert.ToInt32(node.Attributes["y"].InnerText);
int width = Convert.ToInt32(node.Attributes["width"].InnerText);
int height = Convert.ToInt32(node.Attributes["height"].InnerText);
int xOffset = Convert.ToInt32(node.Attributes["xoffset"].InnerText);
int yOffset = Convert.ToInt32(node.Attributes["yoffset"].InnerText);
int xAdvance = Convert.ToInt32(node.Attributes["xadvance"].InnerText);
CharacterInfo info = new CharacterInfo();
Rect uv = new Rect();
uv.x = (float)x / totalWidth;
uv.y = (float)(totalHeight - y - height) / totalHeight;
uv.width = (float)width / totalWidth;
uv.height = (float)height / totalHeight;
info.index = index;
info.uvBottomLeft = new Vector2(uv.xMin, uv.yMin);
info.uvBottomRight = new Vector2(uv.xMax, uv.yMin);
info.uvTopLeft = new Vector2(uv.xMin, uv.yMax);
info.uvTopRight = new Vector2(uv.xMax, uv.yMax);
info.minX = xOffset;
info.maxX = xOffset + width;
info.minY = -yOffset - height;
info.maxY = -yOffset;
info.advance = xAdvance;
info.glyphWidth = width;
info.glyphHeight = height;
characterInfoList.Add(info);
}
font.characterInfo = characterInfoList.ToArray(typeof(CharacterInfo)) as CharacterInfo[];
Debug.Log("生成成功.");
}
}