日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Unity自定义美术字体(图片字体fontsettings)

發布時間:2024/3/26 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Unity自定义美术字体(图片字体fontsettings) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

  • 本文內容
  • 效果
  • 工具代碼
  • 制作美術字體
    • 1 資源
      • 1.1 資源方式
      • 1.2 資源命名
      • 1.3 資源命名配置
    • 2 打包字體
      • 2.1 創建字體
      • 2.2 更新字體
    • 3 字體使用

本文內容

開發過程有個很常見的需求:使用美術提供的圖片來表現美術字體。

Unity官方為此提供了一個自定義字體,用于在UI上顯示圖片字體,具體各參數意義可以參考https://docs.unity3d.com/2022.2/Documentation/Manual/class-Font.html。

效果

如下,我們可以很自由的展示圖片字體。

工具代碼

using System.Collections.Generic; using System.IO; using UnityEditor; using UnityEngine;public class CrisFontWindow: EditorWindow {private string fontPath = "Assets/crisFont.fontsettings";private Font font;private Rect[] rects;private TextAsset characterCfg;void OnGUI(){GUILayout.BeginVertical();GUILayout.BeginHorizontal();GUILayout.Label("字體:", GUILayout.Width(100.0f));font = (Font)EditorGUILayout.ObjectField(font, typeof(Font), true);GUILayout.EndHorizontal();if (font == null){EditorGUILayout.BeginHorizontal();GUILayout.Label("字體路徑", GUILayout.Width(100));fontPath = GUILayout.TextField(fontPath);EditorGUILayout.EndHorizontal();if (GUILayout.Button("新建字體")){Font newFont = new Font();try{AssetDatabase.CreateAsset(newFont, fontPath);font = AssetDatabase.LoadAssetAtPath<Font>(fontPath);}catch{}if (font == null) Debug.LogError("路徑錯誤:" + fontPath);}}else{GUILayout.BeginHorizontal();GUILayout.Label("字體配置:", GUILayout.Width(100.0f));characterCfg = (TextAsset)EditorGUILayout.ObjectField(characterCfg, typeof(TextAsset), true);GUILayout.EndHorizontal();GUILayout.Label("請選中需要打入的字體文件");GUILayout.BeginHorizontal();if (GUILayout.Button("更新文本")) UpdateFont();GUILayout.EndHorizontal();}GUILayout.EndVertical();}void UpdateFont(){string fontPath = AssetDatabase.GetAssetPath(font);string fontDir = Path.GetDirectoryName(fontPath);var texSavePath = Path.Combine(fontDir, Path.GetFileNameWithoutExtension(fontPath) + ".png");CombineTexture(texSavePath);var tex = AssetDatabase.LoadAssetAtPath<Texture2D>(texSavePath);if (tex == null) Debug.LogError("未發現生成的圖集");var matPath = Path.Combine(fontDir, Path.GetFileNameWithoutExtension(fontPath) + ".mat");var mat = AssetDatabase.LoadAssetAtPath<Material>(matPath);if (mat == null){var newMat = new Material(Shader.Find("GUI/Text Shader"));AssetDatabase.CreateAsset(newMat, matPath);mat = newMat;}mat.SetTexture("_MainTex", tex);EditorUtility.SetDirty(mat);var list = new List<CharacterInfo>();float maxHeight = 0;for (int i = 0; i < rects.Length; i++){if (rects[i].height > maxHeight) maxHeight = rects[i].height;}for (int i = 0; i < rects.Length; i++){Rect rect = ConvertToPixels(rects[i], tex.width, tex.height);int asciiIndex = ac[i];var charInfo = new CharacterInfo();charInfo.index = asciiIndex;Rect uv = ConvertToTexCoords(rect, tex.width, tex.height);charInfo.uvBottomLeft = new Vector2(uv.x, uv.y);charInfo.uvTopLeft = new Vector2(uv.x, uv.y + uv.height);charInfo.uvBottomRight = new Vector2(uv.x + uv.width, uv.y);charInfo.uvTopRight = new Vector2(uv.x + uv.width, uv.y + uv.height);charInfo.minX = 0;charInfo.maxX = (int)rect.width;charInfo.minY = (int)(tex.height / 2 - (maxHeight - rect.height) / 2 - rect.height);charInfo.maxY = (int)(tex.height / 2 - (maxHeight - rect.height) / 2);charInfo.advance = (int)rect.width;list.Add(charInfo);}font.material = mat;font.characterInfo = list.ToArray();EditorUtility.SetDirty(font);AssetDatabase.SaveAssets();AssetDatabase.Refresh();Debug.Log("更新成功");}Rect ConvertToPixels(Rect rect, int width, int height){Rect r = rect;r.xMin = Mathf.RoundToInt(rect.xMin * width);r.xMax = Mathf.RoundToInt(rect.xMax * width);r.yMin = Mathf.RoundToInt((1 - rect.yMax) * height);r.yMax = Mathf.RoundToInt((1 - rect.yMin) * height);return r;}Rect ConvertToTexCoords(Rect rect, int width, int height){Rect r = rect;if (width != 0 && height != 0){r.xMin = rect.xMin / width;r.xMax = rect.xMax / width;r.yMin = 1 - rect.yMax / height;r.yMax = 1 - rect.yMin / height;}return r;}List<int> ac = new List<int>();void CombineTexture(string texSavePath){ac.Clear();int num = Selection.objects.Length;if (num <= 0){Debug.LogError("請選中需要合并的數字圖片");return;}Dictionary<string, object> cfgDic = null;if (characterCfg != null){cfgDic = MiniJSON.Json.Deserialize(characterCfg.text) as Dictionary<string, object>;}for (int i = 0; i < num; ++i){Texture2D tex = Selection.objects[i] as Texture2D;string name = Path.GetFileNameWithoutExtension(tex.name);if (cfgDic != null && cfgDic.ContainsKey(name))ac.Add((int)(cfgDic[name].ToString()[0]));elseac.Add((int)(name[0]));}Texture2D r = new Texture2D(2, 2, TextureFormat.RGBA32, false);Texture2D[] textures = new Texture2D[num];for (int i = 0; i < num; i++){Texture2D tex = Selection.objects[i] as Texture2D;textures[i] = tex;}rects = r.PackTextures(textures, 1);r.Apply();SaveTexture(r, texSavePath);}void SaveTexture(Texture2D texture, string path){if (texture == null) return;File.WriteAllBytes(path, texture.EncodeToPNG());AssetDatabase.ImportAsset(path);AssetDatabase.Refresh();}[MenuItem("CrisTools/UI/美術字體", false, 1102)]public static void OpenWin(){EditorWindow.GetWindow<CrisFontWindow>("美術字體");} }

制作美術字體

1 資源

1.1 資源方式

美術提供所有字的單個圖片,大小一致

重點注意:單個圖片必須打開讀寫和關閉壓縮,否則會出現打包失敗或打包卡死的情況。

1.2 資源命名

圖片名字使用圖片對應的字,比如:表示“1”的圖片命名就為1,表示“+”的圖片命名就為+

1.3 資源命名配置

如果出現圖片表示的字不能當做文件名時,則可以使用json配置。
比如表示“%”的圖片文件名可以叫做“baifenhao”,“@”的圖片叫做“at”,在json配置中
{
“baifenhao”:“%”,
“at”:“@”
}

2 打包字體

2.1 創建字體

如果需創建一個新字體,輸入創建字體的路徑即可。

如下,我們在測試路徑下生成了一個font字體文件。

2.2 更新字體

當創建了一個新字體,或者將一個已有的字體拖到字體框中后,選中碎片資源,就可以對字體進行打包更新了。
注:如有特殊字符,使用了1.3中json配置,則將對應json拖至字體配置選框中。

更新過后,就能得到字體所需的圖集和材質了。

3 字體使用


在支持富文本組件的font處選擇剛生成的字體文件即可正常使用。

注:筆者的富文本組件包含內容過于繁多,在此就不貼出了。如果有需要且不知道富本文組件怎么制作的,可以使用hub上大佬們寫的富文本組件。(https://github.com/wuxiongbin/uHyperText)

總結

以上是生活随笔為你收集整理的Unity自定义美术字体(图片字体fontsettings)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。