不多說!直接上源碼!
if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
using System.Collections;
//本方法是通過裁切的sprite導(dǎo)出字體文件,裁切使用的是unity自帶的sprite editor,方便操作。
//另外,裁切之后,每個sprite的名字的最后一個字符對應(yīng)了ascii碼的編碼,比如:
//0: 我們只要將sprite的名字命名成xxx0,就可以了!
//由于使用到的了sprite加載,所以字體圖片請放在Resources目錄下面,等制作完畢,再把他們放到fonts文件夾或者其他文件夾中即可。
public class FontMaker
{
[MenuItem("Assets/CreateMyFontSprite")]
static void CreateMyFontSprite()
{
if (Selection.objects == null) return;
if (Selection.objects.Length == 0)
{
Debug.LogWarning("沒有選中Sprite文件,需要將Sprite Mode設(shè)置成Multiple,切分好,并且以以名字的最后一個字符當(dāng)做ascii碼");
return;
}
string resoursePath = "Resources";
Object o = Selection.objects[0];
if (o.GetType() != typeof(Texture2D))
{
Debug.LogWarning("選中的并不是圖片文件");
return;
}
string selectionPath = AssetDatabase.GetAssetPath(o);
if (selectionPath.Contains(resoursePath))
{
string selectionExt = System.IO.Path.GetExtension(selectionPath);
if (selectionExt.Length == 0)
{
return;
}
string loadPath = selectionPath.Remove(selectionPath.Length - selectionExt.Length);
string fontPathName = loadPath + ".fontsettings";
string matPathName = loadPath + ".mat";
float lineSpace = 0.1f;//字體行間距,下面會根據(jù)最高的字體得到行間距,如果是固定高度,可以在這里自行調(diào)整
loadPath = System.IO.Path.GetFileNameWithoutExtension(selectionPath);
Sprite[] sprites = Resources.LoadAll<Sprite>(loadPath);
if (sprites.Length > 0)
{
//以textrue方式獲得該資源,可以設(shè)置到創(chuàng)建的材質(zhì)中去
Texture2D tex = o as Texture2D;
//創(chuàng)建字體材質(zhì),并且將圖片設(shè)置好
Material mat = new Material(Shader.Find("GUI/Text Shader"));
AssetDatabase.CreateAsset(mat, matPathName);
mat.SetTexture("_MainTex", tex);
//創(chuàng)建字體文件,設(shè)置字體文件的材質(zhì)
Font m_myFont = new Font();
m_myFont.material = mat;
AssetDatabase.CreateAsset(m_myFont, fontPathName);
//創(chuàng)建字體中的字符集數(shù)組
CharacterInfo[] characterInfo = new CharacterInfo[sprites.Length];
//得到最高的高度,設(shè)置行高和進(jìn)行偏移計算
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();
//設(shè)置ascii碼,使用切分sprite的最后一個字母
info.index = (int)spr.name[spr.name.Length - 1];
Rect rect = spr.rect;
//根據(jù)pivot設(shè)置字符的偏移,具體需要做成什么樣的,可以根據(jù)自己需要修改公式
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;
}
Debug.Log(pivot);
int offsetY = (int)(pivot + (lineSpace - rect.height) / 2);
//設(shè)置字符映射到材質(zhì)上的坐標(biāo)
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);
//設(shè)置字符頂點的偏移位置和寬高
info.minX = 0;
info.minY = -(int)rect.height - offsetY;
info.maxX = (int)rect.width;
info.maxY = -offsetY;
//設(shè)置字符的寬度
info.advance = (int)rect.width;
characterInfo[i] = info;
}
// lineSpace += 2;
m_myFont.characterInfo = characterInfo;
EditorUtility.SetDirty(m_myFont);//設(shè)置變更過的資源
AssetDatabase.SaveAssets();//保存變更的資源
AssetDatabase.Refresh();//刷新資源,貌似在Mac上不起作用
//由于上面fresh之后在編輯器中依然沒有刷新,所以暫時想到這個方法,
//先把生成的字體導(dǎo)出成一個包,然后再重新導(dǎo)入進(jìn)來,這樣就可以直接刷新了
//這是在Mac上遇到的,不知道Windows下面會不會出現(xiàn),如果不出現(xiàn)可以把下面這一步注釋掉
AssetDatabase.ExportPackage(fontPathName, "temp.unitypackage");
AssetDatabase.DeleteAsset(fontPathName);
AssetDatabase.ImportPackage("temp.unitypackage", true);
AssetDatabase.Refresh();
//最佳高度:上下各留一個像素的間距,如果不需要可以注釋掉,根據(jù)需求更改
//打印是為了使使用者方便填寫行高,因為font不支持設(shè)置行高。
Debug.Log("創(chuàng)建字體成功, 最大高度:" + lineSpace + ", 最佳高度:" + (lineSpace + 2));
}
else
{
Debug.LogWarning("沒有選中Sprite文件,需要將Sprite放到Resources文件夾下面,可以參考函數(shù)上方的說明操作");
}
}
}
}
endif
// 使用方法
首先載入一張0-9的數(shù)字圖片在Resources文件夾下,設(shè)置為Sprite-Multiple自動切割1行10列
apply
然后點擊Unity菜單欄 Assets/CreateMyFontSprite
Import導(dǎo)入字體