首先创建艺术字体文件。
接着将sprite拖入FontList, 并且输入它对应的unicode字符,可以是中文也可以是数字,主要就是把字符和Sprite进行绑定。
点击生成字体,会创建3个文件,字体文件、材质文件、图集文件。
创建Text对象,拖入刚刚生成的字体文件到Font上,输入绑定的字符即可排版艺术字
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
using System; using System.Collections.Generic; using System.IO; #if UNITY_EDITOR using UnityEditor; #endif using UnityEngine; [CreateAssetMenu(menuName ="艺术字/文件")] public class UIFontAsset : ScriptableObject { [System.Serializable] public class Font { //字符 public char c; //贴图 public Texture2D texture; } //图集大小 public int Size = 256; //图集间距 public int Pandding = 1; public List<Font> FontList = new List<Font>(); } #if UNITY_EDITOR [CustomEditor(typeof(UIFontAsset))] public class UIFontAssetEditor : Editor { public override void OnInspectorGUI() { base.OnInspectorGUI(); if(GUILayout.Button("生成字体")) { UIFontAsset fontAsset = target as UIFontAsset; if (fontAsset != null && fontAsset.FontList!=null) { List<Texture2D> atlasTextures = new List<Texture2D>(); Dictionary<Texture2D, int> ascii = new Dictionary<Texture2D, int>(); foreach (var data in fontAsset.FontList) { if (data != null && data.texture) { var tex = data.texture; Texture2D @out = new Texture2D(tex.width, tex.height, TextureFormat.RGBA32, false); var width = tex.width; var height = tex.height; RenderTexture tmp = RenderTexture.GetTemporary(width,height,0,RenderTextureFormat.Default,RenderTextureReadWrite.sRGB); Graphics.Blit(tex, tmp); RenderTexture previous = RenderTexture.active; RenderTexture.active = tmp; Texture2D @new = new Texture2D(width, height); @new.ReadPixels(new Rect(0, 0, width, height), 0, 0); @new.Apply(); @out.SetPixels(0, 0, width, height, @new.GetPixels()); RenderTexture.active = previous; RenderTexture.ReleaseTemporary(tmp); atlasTextures.Add(@out); ascii[@out] = Convert.ToInt32(data.c); } } Texture2D atlas = new Texture2D(fontAsset.Size, fontAsset.Size, TextureFormat.RGBA32, false); var rects = atlas.PackTextures(atlasTextures.ToArray(), fontAsset.Pandding, fontAsset.Size); var path = AssetDatabase.GetAssetPath(fontAsset); var newPath = ChangeFileName(path, ".png"); File.WriteAllBytes(newPath, atlas.EncodeToPNG()); AssetDatabase.ImportAsset(newPath); TextureImporter importer = AssetImporter.GetAtPath(newPath) as TextureImporter; if (importer != null) { importer.textureType = TextureImporterType.Sprite; importer.mipmapEnabled = false; importer.SaveAndReimport(); } AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); CharacterInfo[] infos = new CharacterInfo[atlasTextures.Count]; for (int i = 0; i < atlasTextures.Count; i++) { var info= new CharacterInfo(); var tex = atlasTextures[i]; info.index = ascii[tex]; info.uv = rects[i]; var w = tex.width; var h= tex.height; info.advance = w; info.vert = new Rect(0, 0, w, -h); infos[i] = info; } Shader shader = Shader.Find("Unlit/Transparent"); Material material = new Material(shader); material.mainTexture = AssetDatabase.LoadAssetAtPath<Texture2D>(newPath); AssetDatabase.CreateAsset(material, ChangeFileName(path, ".mat")); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); var fontPath = ChangeFileName(path, ".fontsettings"); Font font = AssetDatabase.LoadAssetAtPath<Font>(fontPath); bool hasFont = font != null; if (!hasFont) font = new Font(); font.material = material; font.name = Path.GetFileNameWithoutExtension(fontPath); font.characterInfo = infos; if(!hasFont) AssetDatabase.CreateAsset(font,fontPath); else { EditorUtility.SetDirty(font); AssetDatabase.SaveAssetIfDirty(font); } AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } } } string ChangeFileName(string path, string newName) { var directory = Path.GetDirectoryName(path); var newFileName =Path.GetFileNameWithoutExtension(path)+newName; return Path.Combine(directory, newFileName); } } #endif |
- 本文固定链接: https://www.xuanyusong.com/archives/5180
- 转载请注明: 雨松MOMO 于 雨松MOMO程序研究院 发表
捐 赠写博客不易,如果您想请我喝一杯星巴克的话?就进来看吧!