若愚

  • 首页
  • 书生意气
  • 随心杂谈
  • 技不压身
  • 游戏人生
  • 吾辈简介
    • 吾辈简介
    • 慷概解囊
惊起
半截薄衣轻推窗,灯火阑珊夜微凉; 独坐残床不思量,一盏昏灯照孤郎。
  1. 首页
  2. 技不压身
  3. 正文

UGUI自定义字体

2021年3月31日 1459点热度 0人点赞 0条评论

如何使用字体

将字体文件拖入UGUI的Text中的Font中,即可使用字体

新建字体

寻找一个fnt字体

在Project下右键Create->CustomFont创建一个字体

垂直旋转图片,按照要求填入字体数据

使用BMFont生成自定义字体

打开BMFont软件

打开ImageManager导入字体资源

保存字体就会生成自定义的Fnt字体文件

UGUI字体自动生成

代码如下

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

// 创建bmfont
public class CreateFontEditor : Editor
{
    [MenuItem("Assets/CreateBMFont")]  
    static void CreateFont()
    {  
        Object obj = Selection.activeObject;  
        string fntPath = AssetDatabase.GetAssetPath(obj); 
        if (fntPath.IndexOf(".fnt") == -1)
        {  
            // 不是字体文件 
            Debug.LogError("The Selected Object Is Not A .fnt file!");
            return;  
        }  

        string customFontPath = fntPath.Replace(".fnt", ".fontsettings");  
        if (!File.Exists(customFontPath))
        {  
            Debug.LogError("The .fontsettings file not exists");
            return;  
        }  

        Debug.Log(fntPath);  
        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)
            {  
                Match match = reg.Match(line);  
                if (match != 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 = yoffset + height / 2;   // 这样调出来的效果是ok的,原理未知  
//                    info.glyphWidth = width;  
//                    info.glyphHeight = -height; // 同上,不知道为什么要用负的,可能跟unity纹理uv有关  
//                    info.advance = xadvance;  

                    info.uv.x = (float) x / texWidth;
                    info.uv.y = (float) y / texHeight;
                    info.uv.width = (float) width / texWidth;
                    info.uv.height = (float) height / texHeight;

                    info.vert.x = xoffset;
                    info.vert.y = yoffset;
                    info.vert.width = width;
                    info.vert.height = 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+)");  
                Match match = reg2.Match(line);  
                if (match != 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();  
        }  

        Font customFont = AssetDatabase.LoadAssetAtPath<Font>(customFontPath);  
        customFont.characterInfo = charList.ToArray();  
        AssetDatabase.SaveAssets();  
        AssetDatabase.Refresh();  
        Debug.Log(customFont);  
    }
}

 

本作品采用 知识共享署名-非商业性使用 4.0 国际许可协议 进行许可
标签: 暂无
最后更新:2021年3月31日

苹果蘸大蒜

有人看我志气昂,谁又听我半世伤; 只盼白发江渚上,执手泪眼话沧桑。

打赏 点赞
< 上一篇
下一篇 >

文章评论

取消回复

COPYRIGHT © 2021 RuoYu ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS

豫ICP备19041692号-1