Unity制作圖片字體的方法
本文實(shí)例為大家分享了Unity制作圖片字體的具體代碼,供大家參考,具體內(nèi)容如下
主要步驟:
1.新建一個(gè)字體。
2.新建一個(gè)材質(zhì)。
3.把上一步新建的材質(zhì)賦值給第一步創(chuàng)建的字體。
4.設(shè)置字體的Character Rects的大小來(lái)劃分字體。
例如劃分這個(gè)圖片,因其要分為12個(gè)字體所以Character Rects大小為12:
Character Rects中每個(gè)單元格的計(jì)算方式如圖所標(biāo)記(比較的復(fù)雜,了解怎么計(jì)算就行,后期通過(guò)編輯器可以計(jì)算出來(lái)。)
5.把設(shè)置好的字體直接賦值給Text中的Font,在Text中輸入對(duì)應(yīng)字體便可顯示了。
標(biāo)題上述制作字體設(shè)置參數(shù)比較繁瑣,可以直接用編輯器一鍵生成字體和材質(zhì)。編輯器代碼如下。
public class ImageFontMaker { [MenuItem("Assets/CreateImageFont")] static void CreateImageFont() { if ( Selection.objects == null ) { return; } for (int i = 0; i < Selection.objects.Length; i++) { if (Selection.objects[i].GetType() == typeof(Texture2D)) { CreateImageFont(Selection.objects[i] as Texture2D); } } } public static void CreateImageFont( Texture2D texture ) { if ( texture == null ) return; string texturePath = AssetDatabase.GetAssetPath(texture); string textureExtension = Path.GetExtension(texturePath); string filePath = texturePath.Remove(texturePath.Length - textureExtension.Length); string matPath = filePath + ".mat"; string fontPath = filePath + ".fontsettings"; Font myFont = AssetDatabase.LoadAssetAtPath<Font>(fontPath); if ( myFont == null ) { myFont = new Font(); // 設(shè)置材質(zhì) Material mat = new Material(Shader.Find("GUI/Text Shader")); mat.SetTexture("_MainTex", texture); AssetDatabase.CreateAsset(mat, matPath); myFont.material = mat; AssetDatabase.CreateAsset(myFont, fontPath); } // 設(shè)置字符信息 Sprite[] sprites = LoadSpritesByPath(texturePath); if (sprites.Length == 0 ) return; CharacterInfo[] characterInfos = new CharacterInfo[sprites.Length]; for (int i = 0; i < characterInfos.Length; i++) { characterInfos[i] = new CharacterInfo(); // 設(shè)置 ascii 碼 characterInfos[i].index = sprites[i].name[sprites[i].name.Length - 1]; // 設(shè)置 字符 uv Rect rect = sprites[i].rect; characterInfos[i].uvBottomLeft = new Vector2( rect.x / texture.width , rect.y / texture.height); characterInfos[i].uvBottomRight = new Vector2((rect.x +rect.width) / texture.width, rect.y / texture.height); characterInfos[i].uvTopRight = new Vector2((rect.x + rect.width) / texture.width, (rect.y + rect.height) / texture.height); characterInfos[i].uvTopLeft = new Vector2(rect.x / texture.width,(rect.y + rect.height) / texture.height); // 字符的偏移 和 寬高 characterInfos[i].minX = 0; characterInfos[i].maxX = (int)rect.width; characterInfos[i].minY = 0 - (int)sprites[i].pivot.y; characterInfos[i].maxY = (int)rect.height - (int)sprites[i].pivot.y; characterInfos[i].advance = (int)rect.width; } myFont.characterInfo = characterInfos; EditorUtility.SetDirty(myFont); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } public static Sprite[] LoadSpritesByPath(string path) { List<Sprite> sprites = new List<Sprite>(); Object[] objects = AssetDatabase.LoadAllAssetsAtPath(path); foreach (var item in objects) { if ( item.GetType() == typeof(Sprite) ) { sprites.Add(item as Sprite); } } return sprites.ToArray(); } }
編輯器使用方法:選中要生成字體的Texture 右鍵選擇CreateImageFont即可生成對(duì)應(yīng)字體和材質(zhì)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Unity 百度AI實(shí)現(xiàn)人像動(dòng)漫化效果
這篇文章主要介紹了Unity如何接入百度AI接口, 運(yùn)用對(duì)抗生成網(wǎng)絡(luò)技術(shù),為用戶(hù)量身定制千人千面的二次元?jiǎng)勇蜗?,并支持通過(guò)參數(shù)設(shè)置,生成二次元?jiǎng)勇讼?。感興趣的可以學(xué)習(xí)一下2022-01-01Unity Blend Tree動(dòng)畫(huà)混合樹(shù)使用入門(mén)教程
這篇文章主要為大家詳細(xì)介紹了Unity Blend Tree動(dòng)畫(huà)混合樹(shù)使用入門(mén)教程,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11C#實(shí)現(xiàn)文本文件讀寫(xiě)方法匯總
本文給大家匯總介紹了C#實(shí)現(xiàn)文本文件讀寫(xiě)的方法,十分的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下。2015-06-06C#遍歷文件夾及其子目錄的完整實(shí)現(xiàn)方法
這篇文章主要介紹了C#遍歷文件夾及其子目錄的方法,涉及C#文件與目錄的基本操作技巧,簡(jiǎn)單實(shí)用,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06