1.bmFont工具 Edit -> Open Image Manager製作字體,
2.導出設置 Options -> Export Options bitdeth選擇32位 XML Texture選擇png
3.SavabmFont導出
4.導出文件導入unity,
新建材質球Shader選擇GUI/TextShader
右鍵Create-Custem Font,指定上述材質球
5,運行此工具
public class BMFont
{
[MenuItem("Tools/Create Font")]
static void Font()
{
//把我們創建的材質球加載進來
Material mtr = Resources.Load<Material>("Font/NumberFont");
//把我們在位圖製作工具生成的圖片加載進來
Texture2D texture = Resources.Load<Texture2D>("Font/NumberFont_0");
//把圖片賦給材質球
mtr.SetTexture(0, texture);
//把我們創建的字體加載進來
Font font = Resources.Load<Font>("Font/NumberFont");
XmlDocument xml = new XmlDocument();
XmlReaderSettings set = new XmlReaderSettings();
//這個設置是忽略xml註釋文檔的影響。有時候註釋會影響到xml的讀取
set.IgnoreComments = true;
string path = Application.dataPath + "/Resources/Font/NumberFont.fnt";
//path = path.Replace('/', '\');
//這是在BMFont裏得到的那個.fnt文件,因爲是xml文件,所以我們就用xml來解析
xml.Load(path);
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.width;
chtInfo.glyphHeight = texture.height;
chtInfo.glyphWidth = texture.width;
chtInfo.index = int.Parse(xe.GetAttribute("id"));
//這裏注意下UV座標系和從BMFont裏得到的信息的座標系是不一樣的哦,前者左下角爲(0,0),
//右上角爲(1,1)。而後者則是左上角上角爲(0,0),右下角爲(圖寬,圖高)
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;
chtInfo.maxX = width;
chtInfo.maxY = 0;
chtInfo.advance = advance;
chtInfoList.Add(chtInfo);
}
font.characterInfo = chtInfoList.ToArray();
//保存此次修改
EditorUtility.SetDirty(font);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
}