unity使用BMFont制作位图字体

一、BmFont下载地址

BMFont - AngelCode.com

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("生成成功.");
    }
}

5.将CustomFontImportor脚本拖到场景中的物体身上,并运行场景,我们会可看到有一个“生成成功”的日志输出,这是选中test.mat查看发现,已经被成功赋值了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值