/// <summary>
/// 位图字体生成工具
/// </summary>
public class BMFontTool
{
/// <summary>
/// 字体生成
/// </summary>
[MenuItem("Assets/BMFont/CreateFont")]
static void CreateFont()
{
string directorPath;
//选中的asset路径
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);
}
//找到.fnt文件和.fontsetting文件
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;
}
//根据选中物体判断是资源还是文件夹,然后获取文件夹下的.fnt和.fontsetting
TextAsset fntFile = AssetDatabase.LoadAssetAtPath<TextAsset>(fntPath);
Font fontFile = AssetDatabase.LoadAssetAtPath<Font>(fontsettingPath);
//读取进入字体文件
ReadFntToSetting(fontFile, fntFile);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
/// <summary>
/// 读取fnt文件进入fontsetting文件
/// </summary>
/// <param name="font">fontsetting文件</param>
/// <param name="textAsset">fnt文件</param>
static void ReadFntToSetting(Font font,TextAsset textAsset)
{
if (font == null || textAsset == null)
{
//Debug.LogError("请设置font和textAsset.");
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];
// Define the regular expression pattern
string pattern = @"\d+";
// Create a Regex object
Regex regex = new Regex(pattern);
// Find all matches
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("生成成功.");
}
/// <summary>
/// 获取匹配的路径
/// </summary>
/// <param name="folderPath">文件夹路径</param>
/// <param name="filter">匹配字符串</param>
/// <returns>路径列表</returns>
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;
}
}
标签:Convert,string,int,uv,导入,字体,charInfo,ToInt32
From: https://www.cnblogs.com/comradexiao/p/18472739