首页 > 其他分享 >位图字体导入

位图字体导入

时间:2024-10-17 17:21:30浏览次数:1  
标签:Convert string int uv 导入 字体 charInfo ToInt32

/// <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

相关文章

  • IEEE全文导入飞书/Word
    IEEE网站右击切换到PlainSource然后公式显示为latex代码加上$$格式F12打开控制台,输入神秘代码(见附件)最终IEEE网站显示效果直接复制到飞书效果:使用沉浸式翻译将IEEE网站翻译成中文:复制到飞书:生成word生成word效果从IEEE复制MathMLCode替换word中无法......
  • 新建next.js项目,customize the default import alias是否要自定义默认导入别名(@/*)的选
    使用命令npxcreate-next-app@latest新建项目时,会自定义一些选项,如下图:其中自定义导入别名的选项,选择Yes或No有何区别?Wouldyouliketocustomizethedefaultimportalias(@/*)?...No/Yes一、选择"Yes"jsconfig.js文件的内容是:{"compilerOptions":{......
  • Docker 安装 Oracle创建表空间并导入数据库
    一、Docker 安装Oracle1、检查docker版本2、docker下载镜像3、docker启动容器4、 进入容器内部5、安装成功,使用客户端工具连接二、创建数据库用户与表空间1、查询数据库文件位置2、创建表空间3、创建用户4、设置权限三、导出数据库DMP1、上传文件到指定目录......
  • MySQL数据的导入
    我们在帖子MySQL数据的导出-brucexia-博客园(cnblogs.com)中讲了MySQL数据的导出,本文讲讲解MySQL数据的导入。MySQL数据的导入包括使用LOADDATAINFILE命令导入和使用mysqlimport命令导入。使用LOADDATAINFILE方式导入文本文件MySQL允许将数据导出到外部文件,也可以从外......
  • Redis 数据导入导出
    方式一:redis-dump前置安装redis-dumphttps://blog.csdn.net/zhanaolu4821/article/details/103684237数据导出##-uuri##-a密码##-ddatabase##导出所有库/usr/local/bin/redis-dump-u127.0.0.1:63790-a123456>/data/dba/yanhao/test/testredis.json##导......
  • 如何修改网站后台字体?公司网站修改网页内容?
    要进行网站栏目模板修改或公司地址修改,通常涉及前端HTML/CSS以及可能的后端逻辑调整。下面分别介绍这两种情况的处理方法:网站栏目模板修改定位模板文件找到存放网站栏目的模板文件,这通常位于网站的前端目录下,如templates文件夹内。备份原有文件修改前,请先备份原有的模......
  • 织梦网站头部模板修改?导入网站模板后怎样修改?
    网站头部模板修改步骤登录后台管理打开浏览器,输入你的织梦网站后台地址,通常是 http://你的域名/admin。输入用户名和密码登录。进入模板管理登录后台后,找到并点击“模板管理”或“Templets”。选择头部模板文件在模板管理页面,找到你当前使用的模板文件夹。通......
  • qt5报错无法枚举xxx字体:qt.qpa.fonts: Unable to enumerate family ' "WenYue XinQing
    问题描述:使用qt5时,出现错误提示:qt.qpa.fonts:Unabletoenumeratefamily'"WenYueXinQingNianTi(Non-CommercialUse)"'虽然不影响正常使用,但是还是希望解决。猜测:可能是qt5在自动枚举字体时,系统中安装的字体名称过长或其他参数不合规导致qt5无法枚举。如果此字体不是项......
  • 界面组件DevExpress WPF v24.1亮点 - 支持全新的字体图标图像
    DevExpressWPF拥有120+个控件和库,将帮助您交付满足甚至超出企业需求的高性能业务应用程序。通过DevExpressWPF能创建有着强大互动功能的XAML基础应用程序,这些应用程序专注于当代客户的需求和构建未来新一代支持触摸的解决方案。DevExpressWPF控件日前正式发布了今年一个重大版......
  • 图片字体制作
    图片字体制作工具参考https://blog.csdn.net/lizhong2008/article/details/132781942......