使用Unity中的Sprite。Unity支持把一个Sprite切割成多个。

需要手动做的工作是将图集的TextureType设置为Sprite,然后把SpriteMode设为Multiple,打开SpriteEditor,对图片进行切割。Slice就基本可以完成这个工作,如果需要再手动微调一下。

一张图按照字符的位置分割成了10个Sprite。然后选中每一个Sprite把Name设置成字符对应的ASCII码。同样写一个Editor脚本把这些信息写入到CustomFont里面,并不用手动去创建。

// ========================================================

// 描 述:制作自定义字体

// 作 者:SW

// 创建时间:2019/02/21 10:36:15

// 版 本:v 1.0

// ========================================================

using UnityEngine;

using UnityEditor;

using System.IO;

public class CreateFont : EditorWindow

{

[MenuItem("Tools/创建字体(sprite)")]

public static void Open()

{

GetWindow("创建字体");

}

private Texture2D tex;

private string fontName;

private string fontPath;

private void OnGUI()

{

GUILayout.BeginVertical();

GUILayout.BeginHorizontal();

GUILayout.Label("字体图片:");

tex = (Texture2D)EditorGUILayout.ObjectField(tex, typeof(Texture2D), true);

GUILayout.EndHorizontal();

GUILayout.BeginHorizontal();

GUILayout.Label("字体名称:");

fontName = EditorGUILayout.TextField(fontName);

GUILayout.EndHorizontal();

GUILayout.BeginHorizontal();

if (GUILayout.Button(string.IsNullOrEmpty(fontPath) ? "选择路径" : fontPath))

{

fontPath = EditorUtility.OpenFolderPanel("字体路径", Application.dataPath, "");

if (string.IsNullOrEmpty(fontPath))

{

Debug.Log("取消选择路径");

}

else

{

fontPath = fontPath.Replace(Application.dataPath, "") + "/";

}

}

GUILayout.EndHorizontal();

GUILayout.BeginHorizontal();

if (GUILayout.Button("创建"))

{

Create();

}

GUILayout.EndHorizontal();

GUILayout.EndVertical();

}

private void Create()

{

if (tex == null)

{

Debug.LogWarning("创建失败,图片为空!");

return;

}

if (string.IsNullOrEmpty(fontPath))

{

Debug.LogWarning("字体路径为空!");

return;

}

if (fontName == null)

{

Debug.LogWarning("创建失败,字体名称为空!");

return;

}

else

{

if (File.Exists(Application.dataPath + fontPath + fontName + ".fontsettings"))

{

Debug.LogError("创建失败,已存在同名字体文件");

return;

}

if (File.Exists(Application.dataPath + fontPath + fontName + ".mat"))

{

Debug.LogError("创建失败,已存在同名字体材质文件");

return;

}

}

string selectionPath = AssetDatabase.GetAssetPath(tex);

if (selectionPath.Contains("/Resources/"))

{

string selectionExt = Path.GetExtension(selectionPath);

if (selectionExt.Length == 0)

{

Debug.LogError("创建失败!");

return;

}

string fontPathName = fontPath + fontName + ".fontsettings";

string matPathName = fontPath + fontName + ".mat";

float lineSpace = 0.1f;

//string loadPath = selectionPath.Remove(selectionPath.Length - selectionExt.Length).Replace("Assets/Resources/", "");

string loadPath = selectionPath.Replace(selectionExt, "").Substring(selectionPath.IndexOf("/Resources/") + "/Resources/".Length);

Sprite[] sprites = Resources.LoadAll(loadPath);

if (sprites.Length > 0)

{

Material mat = new Material(Shader.Find("GUI/Text Shader"));

mat.SetTexture("_MainTex", tex);

Font m_myFont = new Font();

m_myFont.material = mat;

CharacterInfo[] characterInfo = new CharacterInfo[sprites.Length];

for (int i = 0; i < sprites.Length; i++)

{

if (sprites[i].rect.height > lineSpace)

{

lineSpace = sprites[i].rect.height;

}

}

for (int i = 0; i < sprites.Length; i++)

{

Sprite spr = sprites[i];

CharacterInfo info = new CharacterInfo();

try

{

info.index = System.Convert.ToInt32(spr.name);

}

catch

{

Debug.LogError("创建失败,Sprite名称错误!");

return;

}

Rect rect = spr.rect;

float pivot = spr.pivot.y / rect.height - 0.5f;

if (pivot > 0)

{

pivot = -lineSpace / 2 - spr.pivot.y;

}

else if (pivot < 0)

{

pivot = -lineSpace / 2 + rect.height - spr.pivot.y;

}

else

{

pivot = -lineSpace / 2;

}

int offsetY = (int)(pivot + (lineSpace - rect.height) / 2);

info.uvBottomLeft = new Vector2((float)rect.x / tex.width, (float)(rect.y) / tex.height);

info.uvBottomRight = new Vector2((float)(rect.x + rect.width) / tex.width, (float)(rect.y) / tex.height);

info.uvTopLeft = new Vector2((float)rect.x / tex.width, (float)(rect.y + rect.height) / tex.height);

info.uvTopRight = new Vector2((float)(rect.x + rect.width) / tex.width, (float)(rect.y + rect.height) / tex.height);

info.minX = 0;

info.minY = -(int)rect.height - offsetY;

info.maxX = (int)rect.width;

info.maxY = -offsetY;

info.advance = (int)rect.width;

characterInfo[i] = info;

}

AssetDatabase.CreateAsset(mat, "Assets" + matPathName);

AssetDatabase.CreateAsset(m_myFont, "Assets" + fontPathName);

m_myFont.characterInfo = characterInfo;

EditorUtility.SetDirty(m_myFont);

AssetDatabase.SaveAssets();

AssetDatabase.Refresh();//刷新资源

Debug.Log("创建字体成功");

}

else

{

Debug.LogError("图集错误!");

}

}

else

{

Debug.LogError("创建失败,选择的图片不在Resources文件夹内!");

}

}

}

下面是常用的ASCII码ASCII码对照表|ASCII编码_911查询​ascii.911cha.com

unity 字体width_Unity中制作自定义字体相关推荐

  1. Unity中制作自定义字体的两种方式

    Unity支持自定义图片字体(CustomFont),网上有很多教程,细节不尽相同,当概括起来基本就是两种方式.一是使用BMFont,导出图集和.fnt文件,再使用图集在Unity中设置得到字体.二是 ...

  2. java引入外部字体_Java中如何自定义字体文件(引用外部字体)?-字体文件

    图源来自网络 有时候我们在程序中,会使用到Java字体,但不是所有的字体系统中都会有,我们就可能会使用外部自定义字体,这样在程序迁移部署中就会少些工作,最近在一个项目中使用到了自定义字体文件,理顺了, ...

  3. android应用添加字体,android中的自定义字体(适用于整个应用程序...

    您可以使用字样为textview中的文本设置自定义字体.因此,每当需要textview的自定义字体时,都可以使用以下字体. activity_main.xml中 android:orientation ...

  4. unity 使用BMFont 制作自定义字体

    unity cocos 使用BMFont,生成自定义字体 1.BMFont下载地址 http://www.angelcode.com/products/bmfont/ 本篇讲解如何利用美工提供的字符图 ...

  5. python中matplotlib自定义设置图像标题使用的字体类型:获取默认的字体族及字体族中对应的字体、自定义设置图像标题使用的字体类型

    python中matplotlib自定义设置图像标题使用的字体类型:获取默认的字体族及字体族中对应的字体.自定义设置图像标题使用的字体类型 目录

  6. android studio 自定义字体,Android Studio中的自定义字体

    如何在android studio中创建自定义字体? 我试图使用自定义字体,我读过,我想将字体放在资产/字体. 我已经搜索了很长时间,但很难找到帮助. 我哪里错了?我真的不知道该怎么做. 我写下了所有 ...

  7. iOS游戏开发中使用自定义字体的方法

    方法1: 添加对应的字体(.ttf或.odf)到工程的resurce,使用cocos2d中的FontLabel库,FontLabel继承于UILabel,象UILabel一样使用就好了 fontNam ...

  8. android 使用 系统字体,Android_解析Android中使用自定义字体的实现方法,1、Android系统默认支持三种字 - phpStudy...

    解析Android中使用自定义字体的实现方法 1.Android系统默认支持三种字体,分别为:"sans", "serif", "monospace ...

  9. 在iPhone App中使用自定义字体

    如何在iPhone程序中使用自定义字体?比如要添加方正兰亭字体,首先需要一个ttf的字体库.到一些字体库的网站上去下载吧 然后把这个文件添加到工程里面  在plist里面添加 添加成功后,就可以使用啦 ...

最新文章

  1. OCP12C题库,63数据库管理( Administration Workshop- 63)(新增)
  2. pythonwhile循环结束语句_Python while循环语句
  3. java8学习_java8学习
  4. 全球及中国凝胶植入物市场专项调研与供需现状分析报告2022年
  5. 矩形嵌套(NYOJ-16)
  6. 软件工程师工作内容和从业要求
  7. 玩游戏学python的网站_娱教于乐!四大游戏类编程网站,学Python再也不枯燥无味了...
  8. android dp转px的公式_dp与px的转换对照表
  9. excel查标准正态分布_利用Excel的NORMSDIST计算正态分布函数表
  10. 中基鸿业人人都要懂的投资理财常识
  11. 抖抖.....抖个不停的伺服电机——转动惯量匹配技术及资料分享
  12. cubeIDE快速开发流程
  13. thinkpadt410接口介绍_【ThinkPadT410s(2912BR7)评测】丰富接口满足日常所需-中关村在线...
  14. 析因设计知识点重点讲解
  15. android Camera 设置焦距
  16. background 背景图
  17. 计算机组成原理swbus,计算机组成原理实验 2.1 总线与寄存器 赖晓铮.ppt
  18. Excel打开csv或文本文件乱码怎么办?
  19. 70 后,80 后硅谷派 AI 创业者的观察与思考
  20. pika安装以及测试

热门文章

  1. 站长常用Shell脚本整理分享51-59
  2. 解压缩软件中如何提前设置文件存储名称相冲突的解决方法
  3. 特征值和特征向量的几何意义、计算及其性质(转载)
  4. 一个案例搞懂工厂模式和单例模式
  5. OpenGL纹理映射总结
  6. 英特尔CPU升级了,戴尔消费及游戏产品也进化了
  7. 第一章 略说中医的学习与研究(5)
  8. 如何优雅的写单词_lduoj_kmp
  9. freeswitch conference命令解释(mod_ conference)
  10. [附源码]计算机毕业设计基于Springboot的中点游戏分享网站