|
Unity 创建字体
本文主要讲的是使用Unity自带的Sprite(类似图集)创建字体。
如:
image
1.通过Unity SpriteEditor 裁剪图集
根据每张图不同尺寸,设计出符合项目所需尺寸(一般高度一致)
image
2.创建字体
选择图片,输入名字、路径,点击创建
image
3.代码
#if UNITY_EDITORusing System;using UnityEngine;using UnityEditor;using System.IO;public class CreateFont : EditorWindow{ [MenuItem("Tools/创建字体(sprite)")] public static void Open() { GetWindow<CreateFont>("创建字体"); } private Texture2D tex; private string fontName; private string fontPath; /// <summary> /// 绘制Editor界面 /// </summary> 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)) { //打开项目Assets路径 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(); } /// <summary> /// 创建字体 /// </summary> 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); string selectionExt = Path.GetExtension(selectionPath); Debug.Log($"selectionExt==={selectionExt}"); if (selectionExt.Length == 0) { Debug.LogError("创建失败!"); return; } string fontPathName = fontPath + fontName + ".fontsettings"; string matPathName = fontPath + fontName + ".mat"; float lineSpace = 0.1f; UnityEngine.Object[] sprites = AssetDatabase.LoadAllAssetsAtPath(selectionPath); 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 is Sprite)) continue; Sprite sprite = sprites as Sprite; if (sprite.rect.height > lineSpace) { lineSpace = sprite.rect.height; } } for (int i = 0; i < sprites.Length; i++) { if (!(sprites is Sprite)) continue; Sprite spr = sprites as Sprite; CharacterInfo info = new CharacterInfo(); try { info.index = Convert.ToChar(spr.name); } catch { Debug.LogError("创建失败,Sprite名称错误!" + spr.name); 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 = 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("图集错误!"); } }}#endif |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|