unity將圖片轉換成字體的方法
本文實例為大家分享了unity利用圖片來生成字體的具體代碼,供大家參考,具體內容如下
開發(fā)中,可能會用到需要將圖片轉換成字體的需求。
BMFONT 插件 導入圖片
然后生成 .fnt 和 .png 兩個文件 (文件格式可以在設置中更改)
將這兩個文件導入unity 將png 切割成精靈
創(chuàng)建材質、將貼圖拖上去。
創(chuàng)建字體、將材質拖上去。
數(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("請設置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("生成成功."); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
C#使用Win32?Api實現(xiàn)進程注入到wechat的過程
這篇文章主要介紹了C#使用Win32?Api實現(xiàn)進程注入到wechat,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-09-09Windows系統(tǒng)中C#調用WinRAR來壓縮和解壓縮文件的方法
這篇文章主要介紹了Windows系統(tǒng)中C#調用WinRAR來壓縮和解壓縮文件的方法,個人感覺在Windows中WinRAR相對7-zip更加穩(wěn)定一些,需要的朋友可以參考下2016-04-04C#連接到sql server2008數(shù)據(jù)庫的實例代碼
這篇文章主要介紹了C#連接到sql server2008數(shù)據(jù)庫的實例代碼,需要的朋友可以參考下2017-09-09