unity將圖片轉(zhuǎn)換成字體的方法
本文實(shí)例為大家分享了unity利用圖片來生成字體的具體代碼,供大家參考,具體內(nèi)容如下
開發(fā)中,可能會(huì)用到需要將圖片轉(zhuǎn)換成字體的需求。
BMFONT 插件 導(dǎo)入圖片
然后生成 .fnt 和 .png 兩個(gè)文件 (文件格式可以在設(shè)置中更改)
將這兩個(gè)文件導(dǎo)入unity 將png 切割成精靈
創(chuàng)建材質(zhì)、將貼圖拖上去。
創(chuàng)建字體、將材質(zhì)拖上去。
數(shù)據(jù)怎么算出來的公式百度上面有,此處略去。也可以利用代碼來生成
using UnityEngine; using System.Collections; using System; using System.Xml; public class CustomFontImportor : MonoBehaviour { public Font font; public TextAsset textAsset; void Awake() { if (font == null || textAsset == null) { Debug.LogError("請(qǐng)?jiān)O(shè)置font和textAsset."); return; } XmlDocument xmlDocument = new XmlDocument(); xmlDocument.LoadXml(textAsset.text); int totalWidth = Convert.ToInt32(xmlDocument["font"]["common"].Attributes["scaleW"].InnerText); int totalHeight = Convert.ToInt32(xmlDocument["font"]["common"].Attributes["scaleH"].InnerText); XmlElement xml = xmlDocument["font"]["chars"]; ArrayList characterInfoList = new ArrayList(); for (int i = 0; i < xml.ChildNodes.Count; ++i) { XmlNode node = xml.ChildNodes[i]; if (node.Attributes == null) { continue; } int index = Convert.ToInt32(node.Attributes["id"].InnerText); int x = Convert.ToInt32(node.Attributes["x"].InnerText); int y = Convert.ToInt32(node.Attributes["y"].InnerText); int width = Convert.ToInt32(node.Attributes["width"].InnerText); int height = Convert.ToInt32(node.Attributes["height"].InnerText); int xOffset = Convert.ToInt32(node.Attributes["xoffset"].InnerText); int yOffset = Convert.ToInt32(node.Attributes["yoffset"].InnerText); int xAdvance = Convert.ToInt32(node.Attributes["xadvance"].InnerText); CharacterInfo info = new CharacterInfo(); Rect uv = new Rect(); uv.x = (float)x / totalWidth; uv.y = (float)(totalHeight - y - height) / totalHeight; uv.width = (float)width / totalWidth; uv.height = (float)height / totalHeight; info.index = index; info.uvBottomLeft = new Vector2(uv.xMin, uv.yMin); info.uvBottomRight = new Vector2(uv.xMax, uv.yMin); info.uvTopLeft = new Vector2(uv.xMin, uv.yMax); info.uvTopRight = new Vector2(uv.xMax, uv.yMax); info.minX = xOffset; info.maxX = xOffset + width; info.minY = -yOffset - height; info.maxY = -yOffset; info.advance = xAdvance; info.glyphWidth = width; info.glyphHeight = height; characterInfoList.Add(info); } font.characterInfo = characterInfoList.ToArray(typeof(CharacterInfo)) as CharacterInfo[]; Debug.Log("生成成功."); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#使用Win32?Api實(shí)現(xiàn)進(jìn)程注入到wechat的過程
這篇文章主要介紹了C#使用Win32?Api實(shí)現(xiàn)進(jìn)程注入到wechat,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09C#把數(shù)字轉(zhuǎn)換成大寫金額的代碼實(shí)例
這篇文章主要介紹了C#把數(shù)字轉(zhuǎn)換成大寫金額的代碼實(shí)例,例如把200轉(zhuǎn)換成“貳佰元”,需要的朋友可以參考下2014-05-05Windows系統(tǒng)中C#調(diào)用WinRAR來壓縮和解壓縮文件的方法
這篇文章主要介紹了Windows系統(tǒng)中C#調(diào)用WinRAR來壓縮和解壓縮文件的方法,個(gè)人感覺在Windows中WinRAR相對(duì)7-zip更加穩(wěn)定一些,需要的朋友可以參考下2016-04-04C#仿密??üδ艿暮?jiǎn)單實(shí)現(xiàn)代碼
昨天拿C#寫了個(gè)簡(jiǎn)單的密??ǔ绦颍–onsole的,偷懶了一下 哈哈),實(shí)現(xiàn)了隨機(jī)生成5x5矩陣卡、轉(zhuǎn)換為字符串、從字符串讀取矩陣卡以及簡(jiǎn)單驗(yàn)證的功能2013-04-04C#連接到sql server2008數(shù)據(jù)庫的實(shí)例代碼
這篇文章主要介紹了C#連接到sql server2008數(shù)據(jù)庫的實(shí)例代碼,需要的朋友可以參考下2017-09-09C#實(shí)現(xiàn)Json轉(zhuǎn)Unicode的方法
這篇文章主要介紹了C#實(shí)現(xiàn)Json轉(zhuǎn)Unicode的方法,可實(shí)現(xiàn)輸入為帶有json格式的文本,輸出正常文本的功能,需要的朋友可以參考下2014-09-09