public class BMFontTool
{
[MenuItem("Assets/BMFont/CreateFont")]
static void CreateFont()
{
string directorPath;
string assetPath = AssetDatabase.GetAssetPath(Selection.activeObject);
if (string.IsNullOrEmpty(assetPath)) return;
if (AssetDatabase.IsValidFolder(assetPath))
{
directorPath = assetPath;
}
else
{
int endIndex = assetPath.LastIndexOf('/');
directorPath = assetPath.Substring(0, endIndex + 1);
}
string fntPath;
var fntList = GetAllAssetPathsInFolder(directorPath, ".fnt");
if (fntList.Count > 0)
{
fntPath = fntList[0];
if (string.IsNullOrEmpty(fntPath))
{
Debug.LogError("没有找到.fnt文件,请检查生成");
return;
}
}
else
{
Debug.LogError("没有找到.fnt文件,请检查生成");
return;
}
string fontsettingPath;
var settingList = GetAllAssetPathsInFolder(directorPath, ".fontsetting");
if (settingList.Count > 0)
{
fontsettingPath = settingList[0];
if (string.IsNullOrEmpty(fontsettingPath))
{
Debug.LogError("没有找到.fontsetting文件,请检查是否创建");
return;
}
}
else
{
Debug.LogError("没有找到.fontsetting文件,请检查是否创建");
return;
}
TextAsset fntFile = AssetDatabase.LoadAssetAtPath<TextAsset>(fntPath);
Font fontFile = AssetDatabase.LoadAssetAtPath<Font>(fontsettingPath);
ReadFntToSetting(fontFile, fntFile);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
static void ReadFntToSetting(Font font,TextAsset textAsset)
{
if (font == null || textAsset == null)
{
return;
}
ArrayList characterInfoList = new ArrayList();
string[] allLine = textAsset.text.Split('\n');
string[] cInfo = allLine[1].Split(' ');
int totalWidth = Convert.ToInt32(cInfo[3].Split('=')[1]);
int totalHeight = Convert.ToInt32(cInfo[4].Split('=')[1]);
string[] lInfo = allLine[3].Split(' ');
int count = Convert.ToInt32(lInfo[1].Split('=')[1]);
for (int i = 4; i < 4 + count; i++)
{
string line = allLine[i];
string pattern = @"\d+";
Regex regex = new Regex(pattern);
MatchCollection matches = regex.Matches(line);
int index = Convert.ToInt32(matches[0].Value);
int x = Convert.ToInt32(matches[1].Value);
int y = Convert.ToInt32(matches[2].Value);
int width = Convert.ToInt32(matches[3].Value);
int height = Convert.ToInt32(matches[4].Value);
int xOffset = Convert.ToInt32(matches[5].Value);
int yOffset = Convert.ToInt32(matches[6].Value);
int xAdvance = Convert.ToInt32(matches[7].Value);
CharacterInfo charInfo = 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;
charInfo.index = index;
charInfo.uvBottomLeft = new Vector2(uv.xMin, uv.yMin);
charInfo.uvBottomRight = new Vector2(uv.xMax, uv.yMin);
charInfo.uvTopLeft = new Vector2(uv.xMin, uv.yMax);
charInfo.uvTopRight = new Vector2(uv.xMax, uv.yMax);
charInfo.minX = xOffset;
charInfo.maxX = xOffset + width;
charInfo.minY = -yOffset - height;
charInfo.maxY = -yOffset;
charInfo.advance = xAdvance;
charInfo.glyphWidth = width;
charInfo.glyphHeight = height;
characterInfoList.Add(charInfo);
}
font.characterInfo = characterInfoList.ToArray(typeof(CharacterInfo)) as CharacterInfo[];
Debug.Log("生成成功.");
}
public static List<string> GetAllAssetPathsInFolder(string folderPath,string filter)
{
List<string> filepaths = new List<string>();
string[] subfoldersAndFiles = AssetDatabase.FindAssets("", new string[] { folderPath });
foreach (string guid in subfoldersAndFiles)
{
string assetPath = AssetDatabase.GUIDToAssetPath(guid);
if (!AssetDatabase.IsValidFolder(assetPath))
{
string filename = Path.GetFileName(assetPath);
if (filename.Contains(filter))
{
filepaths.Add(assetPath);
}
}
}
return filepaths;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了