UGUI艺术字制作(必成)

制作流程
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();
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值