Unity 不使用BMFont创建Font字体
生活随笔
收集整理的這篇文章主要介紹了
Unity 不使用BMFont创建Font字体
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、Unity支持把一個Sprite切割成多個。可以用這種方式代替BMFont導(dǎo)出的fnt文件。
二、將圖集的TextureType設(shè)置為Sprite,然后把SpriteMode設(shè)為Multiple,應(yīng)用Apply。
三、打開SpriteEditor,對圖片進(jìn)行切割。然后選中每一個Sprite把Name設(shè)置成字符對應(yīng)的ASCII碼。
4、添加編輯器工具腳本
?
using UnityEngine; using UnityEditor; using System.IO;public class CreateFont : EditorWindow {[MenuItem("Tools/創(chuàng)建字體(sprite)")]public static void Open(){GetWindow<CreateFont>("創(chuàng)建字體");}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("創(chuàng)建")){Create();}GUILayout.EndHorizontal();GUILayout.EndVertical();}private void Create(){if (tex == null){Debug.LogWarning("創(chuàng)建失敗,圖片為空!");return;}if (string.IsNullOrEmpty(fontPath)){Debug.LogWarning("字體路徑為空!");return;}if (fontName == null){Debug.LogWarning("創(chuàng)建失敗,字體名稱為空!");return;}else{if (File.Exists(Application.dataPath + fontPath + fontName + ".fontsettings")){Debug.LogError("創(chuàng)建失敗,已存在同名字體文件");return;}if (File.Exists(Application.dataPath + fontPath + fontName + ".mat")){Debug.LogError("創(chuàng)建失敗,已存在同名字體材質(zhì)文件");return;}}string selectionPath = AssetDatabase.GetAssetPath(tex);if (selectionPath.Contains("/Resources/")){string selectionExt = Path.GetExtension(selectionPath);if (selectionExt.Length == 0){Debug.LogError("創(chuàng)建失敗!");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<Sprite>(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("創(chuàng)建失敗,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("創(chuàng)建字體成功");}else{Debug.LogError("圖集錯誤!");}}else{Debug.LogError("創(chuàng)建失敗,選擇的圖片不在Resources文件夾內(nèi)!");}} }?
總結(jié)
以上是生活随笔為你收集整理的Unity 不使用BMFont创建Font字体的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: creator 生成bmfont字体文件
- 下一篇: bmfont使用心得