mhhk88
1
My client gave me 2 bitmap font files (png format) and a fnt file.
He wants me to use the font.
The fnt file has lots of these lines …
info face=“³ª´®°íµñ” size=32 bold=0 italic=0 charset=“” unicode=1 stretchH=100 smooth=1 aa=1 padding=0,0,0,0 spacing=1,1 outline=0
common lineHeight=32 base=26 scaleW=1024 scaleH=1024 pages=2 packed=0 alphaChnl=1 redChnl=0 greenChnl=0 blueChnl=0
page id=0 file=“nanumgothin_bitmapfont_0.png”
page id=1 file=“nanumgothin_bitmapfont_1.png”
chars count=2538
char id=0 x=329 y=1022 width=3 height=1 xoffset=-1 yoffset=31 xadvance=8 page=0 chnl=15
char id=1 x=285 y=1022 width=3 height=1 xoffset=-1 yoffset=31 xadvance=8 page=0 chnl=15
char id=2 x=341 y=1022 width=3 height=1 xoffset=-1 yoffset=31 xadvance=8 page=0 chnl=15
char id=3 x=357 y=1022 width=3 height=1 xoffset=-1 yoffset=31 xadvance=8 page=0 chnl=15
char id=4 x=373 y=1021 width=3 height=1 xoffset=-1 yoffset=31 xadvance=8 page=0 chnl=15
char id=5 x=397 y=1021 width=3 height=1 xoffset=-1 yoffset=31 xadvance=8 page=0 chnl=15
…
…
…
kerning first=89 second=117 amount=-2
kerning first=89 second=118 amount=-1
kerning first=89 second=119 amount=-1
And the png files look like this…
The fnt file seems to show some info about each characters in PGN files.
Can anyone help me how to use it ? Thanks in advance for any help.
By the way, Happy New Year! everyone~~^^
Yword
2
Maybe you can reference the following method which will create a custom font from the bitmap font.
1) Import the .fnt and image files into your project.
2) Import the following script to a Editor folder. Use the following script to generate a custom font. (You can right click the .fnt text asset and select “Create Custom BMFont”)
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.IO;
public static class CustomBMFontGenerator
{
[MenuItem("Assets/Create Custom BMFont")]
private static void CreateCustomFont()
{
TextAsset fontDataText = Selection.activeObject as TextAsset;
if (fontDataText == null)
{
return;
}
List<CharacterInfo> characterInfoList = new List<CharacterInfo>();
int scaleW = 0;
int scaleH = 0;
int lineHeight = 0;
string[] fontDataLines = fontDataText.text.Trim().Split('
');
for (int i = 0; i < fontDataLines.Length; i++)
{
string line = fontDataLines*;*
-
if (line.StartsWith("common "))*
-
{*
-
var commonData = ParseDataLine(line);*
-
scaleW = int.Parse(commonData["scaleW"]);*
-
scaleH = int.Parse(commonData["scaleH"]);*
-
lineHeight = int.Parse(commonData["lineHeight"]);*
-
}*
-
else if (line.StartsWith("char "))*
-
{*
-
var charData = ParseDataLine(line);*
-
int id = int.Parse(charData["id"]);*
-
int x = int.Parse(charData["x"]);*
-
int y = int.Parse(charData["y"]);*
-
int width = int.Parse(charData["width"]);*
-
int height = int.Parse(charData["height"]);*
-
int xoffset = int.Parse(charData["xoffset"]);*
-
int yoffset = int.Parse(charData["yoffset"]);*
-
int xadvance = int.Parse(charData["xadvance"]);*
-
CharacterInfo ci = new CharacterInfo();*
-
ci.index = id;*
-
ci.advance = xadvance;*
-
Rect uv = new Rect();*
-
uv.x = (float)x / scaleW;*
-
uv.y = (float)(scaleH - y - height) / scaleH;*
-
uv.width = (float)width / scaleW;*
-
uv.height = (float)height / scaleH;*
-
ci.uvBottomLeft = new Vector2(uv.x, uv.y);*
-
ci.uvTopRight = ci.uvBottomLeft + new Vector2(uv.width, uv.height);*
-
Rect vert = new Rect();*
-
vert.x = xoffset;*
-
vert.y = -yoffset;*
-
vert.width = width;*
-
vert.height = -height;*
-
ci.minX = (int)(vert.x);*
-
ci.minY = (int)(vert.y + vert.height);*
-
ci.maxX = (int)(vert.x + vert.width);*
-
ci.maxY = (int)(vert.y);*
-
characterInfoList.Add(ci);*
-
}*
-
}*
-
string directory = Path.GetDirectoryName(AssetDatabase.GetAssetPath(fontDataText));*
-
string path = directory + "/" + fontDataText.name + ".fontsettings";*
-
Font font = AssetDatabase.LoadAssetAtPath<Font>(path);*
-
bool isNewFont = false;*
-
if (font == null)*
-
{*
-
font = new Font();*
-
isNewFont = true;*
-
}*
-
font.characterInfo = characterInfoList.ToArray();*
-
SetLineSpacing(font, lineHeight);*
-
if (isNewFont)*
-
{*
-
AssetDatabase.CreateAsset(font, path);*
-
}*
-
else*
-
{*
-
AssetDatabase.SaveAssets();*
-
}*
-
}*
-
private static Dictionary<string, string> ParseDataLine(string line)*
-
{*
-
Dictionary<string, string> data = new Dictionary<string, string>();*
-
string[] properties = line.Split(' ');*
-
foreach (string property in properties)*
-
{*
-
string[] keyValue = property.Split('=');*
-
if (keyValue.Length == 2)*
-
{*
-
data.Add(keyValue[0], keyValue[1]);*
-
}*
-
}*
-
return data;*
-
}*
-
private static void SetLineSpacing(Font font, int lineSpacing)*
-
{*
-
SerializedObject fontObject = new SerializedObject(font);*
-
SerializedProperty property = fontObject.FindProperty("m_LineSpacing");*
-
property.floatValue = lineSpacing;*
-
fontObject.ApplyModifiedProperties();*
-
}*
}
3) Create a new material. Set the shader to something with transparency (e.g. “UI/Unlit/Transparent” or “Particles/Alpha Blended”). Assign the bitmap font texture to the material.
4) Set the font and material for your text component(UI Text, TextMesh …).