在Unity中使用fnt字體

參考:https://blog.csdn.net/langresser_king/article/details/49964021 部分字體使用博主的腳本生成后會(huì)無(wú)法對(duì)齊也祠,修改了其中的部分代碼

美術(shù)生成藝術(shù)字體后涎跨,一般會(huì)有一個(gè)fnt文件以及一個(gè)png文件棱烂,導(dǎo)入到Unity中,需要轉(zhuǎn)換為CustomFont才能使用扼雏。

在Editor文件夾下創(chuàng)建以下腳本

using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;

public class CreateBMFont : MonoBehaviour
{
    [MenuItem("Tool/CreateBMFont")]
    static void CreateFont()
    {
        Object obj = Selection.activeObject;
        string fntPath = AssetDatabase.GetAssetPath(obj);
        string fontName = Path.GetFileNameWithoutExtension(fntPath);
        if (fntPath.IndexOf(".fnt") == -1)
        {
            // 不是字體文件 
            Debug.LogError("The Selected Object Is Not A .fnt file!");
            return;
        }

        string customFontPath = fntPath.Replace(".fnt", ".fontsettings");
        Font customFont;
        if (!File.Exists(customFontPath))
        {
            customFont = new Font();
            AssetDatabase.CreateAsset(customFont, customFontPath);
        }
        else
        {
            customFont = AssetDatabase.LoadAssetAtPath<Font>(customFontPath);
        }

        string pngPath = fntPath.Replace(".fnt", ".png");
        if (!File.Exists(pngPath))
        {
            Debug.LogError("未找到同名圖片");
            return;
        }
        Texture texture = AssetDatabase.LoadAssetAtPath<Texture>(pngPath);

        string matPath = fntPath.Replace(".fnt", ".mat");
        Material mat;
        if (!File.Exists(matPath))
        {
            mat = new Material(Shader.Find("GUI/Text Shader"));
            mat.mainTexture = texture;
            mat.name = fontName;
            AssetDatabase.CreateAsset(mat, matPath);
        }
        else
        {
            mat = AssetDatabase.LoadAssetAtPath<Material>(matPath);
            mat.mainTexture = texture;
        }

        customFont.material = mat;

        StreamReader reader = new StreamReader(new FileStream(fntPath, FileMode.Open));

        List<CharacterInfo> charList = new List<CharacterInfo>();

        Regex reg = new Regex(@"char id=(?<id>\d+)\s+x=(?<x>\d+)\s+y=(?<y>\d+)\s+width=(?<width>\d+)\s+height=(?<height>\d+)\s+xoffset=(?<xoffset>(-|\d)+)\s+yoffset=(?<yoffset>(-|\d)+)\s+xadvance=(?<xadvance>\d+)\s+");
        string line = reader.ReadLine();
        int lineHeight = 65;
        int texWidth = 512;
        int texHeight = 512;

        while (line != null)
        {
            if (line.IndexOf("char id=") != -1)
            {
                System.Text.RegularExpressions.Match match = reg.Match(line);
                if (match != System.Text.RegularExpressions.Match.Empty)
                {
                    var id = System.Convert.ToInt32(match.Groups["id"].Value);
                    var x = System.Convert.ToInt32(match.Groups["x"].Value);
                    var y = System.Convert.ToInt32(match.Groups["y"].Value);
                    var width = System.Convert.ToInt32(match.Groups["width"].Value);
                    var height = System.Convert.ToInt32(match.Groups["height"].Value);
                    var xoffset = System.Convert.ToInt32(match.Groups["xoffset"].Value);
                    var yoffset = System.Convert.ToInt32(match.Groups["yoffset"].Value);
                    var xadvance = System.Convert.ToInt32(match.Groups["xadvance"].Value);
                    Debug.Log("ID" + id);


                    CharacterInfo info = new CharacterInfo();
                    info.index = id;
                    float uvx = 1f * x / texWidth;
                    float uvy = 1 - (1f * y / texHeight);
                    float uvw = 1f * width / texWidth;
                    float uvh = -1f * height / texHeight;

                    info.uvBottomLeft = new Vector2(uvx, uvy);
                    info.uvBottomRight = new Vector2(uvx + uvw, uvy);
                    info.uvTopLeft = new Vector2(uvx, uvy + uvh);
                    info.uvTopRight = new Vector2(uvx + uvw, uvy + uvh);

                    info.minX = -xoffset;
                    info.minY = lineHeight / 2 - yoffset; // 縱向的偏移取統(tǒng)一的高度
                    info.glyphWidth = width;
                    info.glyphHeight = -height;
                    info.advance = xadvance;

                    charList.Add(info);
                }
            }
            else if (line.IndexOf("scaleW=") != -1)
            {
                Regex reg2 = new Regex(@"common lineHeight=(?<lineHeight>\d+)\s+.*scaleW=(?<scaleW>\d+)\s+scaleH=(?<scaleH>\d+)");
                System.Text.RegularExpressions.Match match = reg2.Match(line);
                if (match != System.Text.RegularExpressions.Match.Empty)
                {
                    lineHeight = System.Convert.ToInt32(match.Groups["lineHeight"].Value);
                    texWidth = System.Convert.ToInt32(match.Groups["scaleW"].Value);
                    texHeight = System.Convert.ToInt32(match.Groups["scaleH"].Value);
                }
            }
            line = reader.ReadLine();
        }

        customFont.characterInfo = charList.ToArray();
        EditorUtility.SetDirty(customFont);
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
        Debug.Log(customFont);
    }
}

選中fnt字體肛著,再點(diǎn)擊菜單欄上的 "Tool/CreateBMFont" 即可生成對(duì)應(yīng)的CustomFont策泣。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末统抬,一起剝皮案震驚了整個(gè)濱河市聪建,隨后出現(xiàn)的幾起案子金麸,更是在濱河造成了極大的恐慌挥下,老刑警劉巖棚瘟,帶你破解...
    沈念sama閱讀 216,470評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件庄蹋,死亡現(xiàn)場(chǎng)離奇詭異限书,居然都是意外死亡倦西,警方通過(guò)查閱死者的電腦和手機(jī)调限,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)裆装,“玉大人哨免,你說(shuō)我怎么就攤上這事琢唾〔商遥” “怎么了工扎?”我有些...
    開(kāi)封第一講書(shū)人閱讀 162,577評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)舆驶。 經(jīng)常有香客問(wèn)我畴博,道長(zhǎng),這世上最難降的妖魔是什么袱结? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,176評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮果元,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘倡怎。我一直安慰自己监署,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,189評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布簇捍。 她就那樣靜靜地躺著垦写,像睡著了一般彰触。 火紅的嫁衣襯著肌膚如雪梯投。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,155評(píng)論 1 299
  • 那天况毅,我揣著相機(jī)與錄音尔许,去河邊找鬼么鹤。 笑死蒸甜,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的恨憎。 我是一名探鬼主播净蚤,決...
    沈念sama閱讀 40,041評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼放椰!你這毒婦竟也來(lái)了作烟?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 38,903評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤如蚜,失蹤者是張志新(化名)和其女友劉穎压恒,沒(méi)想到半個(gè)月后影暴,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,319評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡探赫,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,539評(píng)論 2 332
  • 正文 我和宋清朗相戀三年型宙,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片伦吠。...
    茶點(diǎn)故事閱讀 39,703評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡妆兑,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出毛仪,到底是詐尸還是另有隱情搁嗓,我是刑警寧澤,帶...
    沈念sama閱讀 35,417評(píng)論 5 343
  • 正文 年R本政府宣布箱靴,位于F島的核電站腺逛,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏衡怀。R本人自食惡果不足惜棍矛,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,013評(píng)論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望抛杨。 院中可真熱鬧够委,春花似錦、人聲如沸蝶桶。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,664評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)真竖。三九已至,卻和暖如春厌小,著一層夾襖步出監(jiān)牢的瞬間恢共,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,818評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工璧亚, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留讨韭,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,711評(píng)論 2 368
  • 正文 我出身青樓癣蟋,卻偏偏與公主長(zhǎng)得像透硝,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子疯搅,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,601評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容