第一次弄,按照自己的理解来的,有需要的可以看一下。
主要需要三个东西:一张装着需要字体的png、一个对应的材质、一个font字体文件。

然后赋值材质到font,并配置好下面每个字的参数。

一开始我也不知道这些参数应该填什么东西,感觉有点懵,上网百度了下,发现有张图挺有用,看完就懂怎么弄了。

既然懂了那就开搞吧,先弄一张专门装我需要的字体的png图片,然后放入大小一样的字体图片,这样比较好判断。
我弄了40*55像素的字体

然后我用ps的切片工具划分好区域摆放的,也挺方便,然后将多余边裁剪

我这张图片规格是240*256的
然后导入unity,non power of 2 选择none显示原来的像素

font一个个数值填写判断太麻烦了,我自己写了个小工具
拉入我们的包含字体的纹理,填写好每个字的宽、高、每行的字数,字体的内容,点击创建字体就ok了。

以下是我写的代码:
没有那么的花里胡哨~有用的就看一下咯
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class FontCreate : EditorWindow
{
[MenuItem("FontTool/CreateFont")]
static void CreateWindow()
{
EditorWindow.GetWindow<FontCreate>(true, "字体创建");
}
//要导入的png
Texture2D texFont;
//单个字体的宽
float texWidth = 0;
//单个字体的高
float texHeight = 0;
//png每行字的个数
int eachLineNum = 0;
//字体内容
string str = "";
private void OnGUI()
{
texWidth = EditorGUILayout.FloatField("每个字的宽度:", texWidth);
texHeight = EditorGUILayout.FloatField("每个字的高度:", texHeight);
eachLineNum = EditorGUILayout.IntField("每行多少个字:", eachLineNum);
str = EditorGUILayout.TextField("字体内容:", str);
GUILayout.BeginHorizontal();
GUILayout.Label("字体的PNG:");
texFont = (Texture2D)EditorGUILayout.ObjectField(texFont, typeof(Texture2D));
GUILayout.EndHorizontal();
if (GUILayout.Button("创建字体"))
{
CreateFont();
}
}
void CreateFont()
{
//创建材质和Font文件
Material material = new Material(Shader.Find("GUI/Text Shader"));
material.mainTexture = texFont;
material.name = texFont.name;
Font font = new Font(texFont.name);
CharacterInfo[] info = new CharacterInfo[str.Length];
for (int i = 0; i < info.Length; i++)
{
info[i].index = (int)str[i];//获得字符的ASCII值
info[i].uv.x = i * texWidth / texFont.width;
info[i].uv.y = (i / eachLineNum) * texHeight / texFont.height;
info[i].uv.width = texWidth / texFont.width;
info[i].uv.height = texHeight / texFont.height;
info[i].vert.x = 0;
info[i].vert.y = 0;
info[i].vert.width = texWidth;
info[i].vert.height = -texHeight;
info[i].advance = (int)texWidth;
}
font.characterInfo = info;
font.material = material;
AssetDatabase.Refresh();
AssetDatabase.CreateAsset(material, "Assets/Font/" + material.name + ".mat");
AssetDatabase.CreateAsset(font, "Assets/Font/" + font.name + ".fontsettings");//记得添加这个font的后缀名
}
}

配置好后,点创建字体,生成材质和font文件

看一下font文件也配置好了

测试一下,没啥问题。

感觉比网上他们写的简单些,反正也够用,也不算很麻烦。
3321



被折叠的 条评论
为什么被折叠?



