轉自:https://blog.csdn.net/AD_118/article/details/64695528
Unity5.5
一:准備工作
1、把字體圖片放到Assets->Resources里面
2、設置改圖片
- Sprite Mode為multiple
- Sprite Editor,先Slice 然后在Apply
3、Sprite Editor命名自己規范一下!我現在命名是使用的名字的最后一個字符去定義的,類似我下面的截圖:
二、實現圖片到字體的轉換
1、在Assets下創建一個Editor的文件夾
在里面創建一個CustomFont的類
類的內容如下:
using UnityEngine; using UnityEditor; using System.IO; public class CustomFont : MonoBehaviour { //本方法是通過裁切的sprite導出字體文件,裁切使用的是unity自帶的sprite editor,方便操作。 //另外,裁切之后,每個sprite的名字的最后一個字符對應了ascii碼的編碼,比如: //0: 我們只要將sprite的名字命名成xxx0,就可以了! //由於使用到的了sprite加載,所以字體圖片請放在Resources目錄下面,等制作完畢,再把他們放到fonts文件夾或者其他文件夾中即可。 [MenuItem ("Assets/CreateMyFontSprite")] static void CreateMyFontSprite () { Debug.LogWarning ("abc"); if (Selection.objects == null) return; if (Selection.objects.Length == 0) { Debug.LogWarning ("沒有選中Sprite文件,需要將Sprite Mode設置成Multiple,切分好,並且以以名字的最后一個字符當做ascii碼"); return; } string resoursePath = "Resources"; UnityEngine.Object o = Selection.objects [0]; if (o.GetType () != typeof(Texture2D)) { Debug.LogWarning ("選中的並不是圖片文件"); return; } string selectionPath = AssetDatabase.GetAssetPath (o); if (selectionPath.Contains (resoursePath)) { string selectionExt = 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;//字體行間距,下面會根據最高的字體得到行間距,如果是固定高度,可以在這里自行調整 loadPath = Path.GetFileNameWithoutExtension (selectionPath); Sprite[] sprites = Resources.LoadAll<Sprite> (loadPath); if (sprites.Length > 0) { //以textrue方式獲得該資源,可以設置到創建的材質中去 Texture2D tex = o as Texture2D; //創建字體材質,並且將圖片設置好 Material mat = new Material (Shader.Find ("GUI/Text Shader")); AssetDatabase.CreateAsset (mat, matPathName); mat.SetTexture ("_MainTex", tex); //創建字體文件,設置字體文件的材質 Font m_myFont = new Font (); m_myFont.material = mat; AssetDatabase.CreateAsset (m_myFont, fontPathName); //創建字體中的字符集數組 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 (); //設置ascii碼,使用切分sprite的最后一個字母 info.index = (int)spr.name [spr.name.Length - 1]; Rect rect = spr.rect; //根據pivot設置字符的偏移,具體需要做成什么樣的,可以根據自己需要修改公式 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); //設置字符映射到材質上的坐標 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; } // lineSpace += 2; m_myFont.characterInfo = characterInfo; EditorUtility.SetDirty (m_myFont);//設置變更過的資源 AssetDatabase.SaveAssets ();//保存變更的資源 AssetDatabase.Refresh ();//刷新資源,貌似在Mac上不起作用 //由於上面fresh之后在編輯器中依然沒有刷新,所以暫時想到這個方法, //先把生成的字體導出成一個包,然后再重新導入進來,這樣就可以直接刷新了 //這是在Mac上遇到的,不知道Windows下面會不會出現,如果不出現可以把下面這一步注釋掉 AssetDatabase.ExportPackage (fontPathName, "temp.unitypackage"); AssetDatabase.DeleteAsset (fontPathName); AssetDatabase.ImportPackage ("temp.unitypackage", true); AssetDatabase.Refresh (); //最佳高度:上下各留一個像素的間距,如果不需要可以注釋掉,根據需求更改 //打印是為了使使用者方便填寫行高,因為font不支持設置行高。 Debug.Log ("創建字體成功, 最大高度:" + lineSpace + ", 最佳高度:" + (lineSpace + 2)); } else { Debug.LogWarning ("沒有選中Sprite文件,需要將Sprite放到Resources文件夾下面,可以參考函數上方的說明操作"); } } } }
2、生成字體
我們運行的程序然后在Assets下發現了CreateMyFontSprite菜單
[MenuItem ("Assets/CreateMyFontSprite")]
- 1
選中Resources中圖片->點擊Assets下的CreateMyFontSprite,就可以生成並導入字體了
點擊CreateMyFontSprite會提示一個導入字體的提示窗
選擇導入有如圖
然后就可以使用這個字體了