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.上代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | 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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!