unity 如何獲取Text組件里text內(nèi)容的長(zhǎng)度
我就廢話(huà)不多說(shuō)了,大家還是直接看代碼吧~
/// <summary> /// 計(jì)算字符串在指定text控件中的長(zhǎng)度 /// </summary> /// <param name="message"></param> /// <returns></returns> int CalculateLengthOfText(string message,Text tex) { int totalLength = 0; Font myFont = tex.font; //chatText is my Text component myFont.RequestCharactersInTexture(message, tex.fontSize, tex.fontStyle); CharacterInfo characterInfo = new CharacterInfo(); char[] arr = message.ToCharArray(); foreach (char c in arr) { myFont.GetCharacterInfo(c, out characterInfo, tex.fontSize); totalLength += characterInfo.advance; } return totalLength; }
補(bǔ)充:用Unity的TextAsset讀取TXT文檔內(nèi)容,將物品信息存入字典中
Text Asset 文本資源
Text Assets are a format for imported text files. When you drop a text file into your Project Folder, it will be converted to a Text Asset. The supported text formats are:
文本資源是導(dǎo)入的文本文件的一個(gè)格式。當(dāng)你拖入一個(gè)文本文件到你的項(xiàng)目時(shí),他會(huì)被轉(zhuǎn)換為文本資源。支持的文本格式有:
.txt .html .htm .xml .bytes
Properties 屬性
Text
The full text of the asset as a single string.
物品屬性分析
之后需要新建一個(gè)文本txt來(lái)存放物品的屬性,這里以藥品為例
TXT文檔內(nèi)容
每一行文本對(duì)應(yīng)一種物品,用逗號(hào)分隔,每個(gè)值對(duì)應(yīng)屬性表里的每一列屬性。
之后新建c#腳本解析文本內(nèi)容
private TextAsset objectsInfoListText; // 新建TextAsset變量 private Dictionary<int, ObjectInfo> objectInfoDict = new Dictionary<int,ObjectInfo>(); void Awake() { objectsInfoListText = Resources.Load("ObjectsInfoList") as TextAsset; // 從Resouces的path中讀取到文件 ReadInfo(); } void ReadInfo() { string text = objectsInfoListText.text; // 將文本內(nèi)容作為字符串讀取 string[] objInfoArray = text.Split('\n'); // 以\n為分割符將文本分割為一個(gè)數(shù)組 foreach (string str in objInfoArray) // 遍歷每一行并將每一個(gè)藥品的值放入類(lèi)對(duì)象中,并存入字典 { ObjectInfo info = new ObjectInfo(); string[] propertyArray = str.Split(','); int id = int.Parse(propertyArray[0]); string name = propertyArray[1]; string icon_name = propertyArray[2]; string str_type = propertyArray[3]; ObjectType type = ObjectType.Drug; switch (str_type) { case "Equip": type = ObjectType.Equip; break; case "Mat": type = ObjectType.Mat; break; case "Drug": type = ObjectType.Drug; break; } info.id = id; info.name = name; info.icon_name = icon_name; info.type = type; if (type == ObjectType.Drug) { int hp = int.Parse(propertyArray[4]); int mp = int.Parse(propertyArray[5]); int price_sell = int.Parse(propertyArray[6]); int price_buy = int.Parse(propertyArray[7]); info.hp = hp; info.mp = mp; info.price_sell = price_sell; info.price_buy = price_buy; } // 將物品信息存儲(chǔ)到字典中 objectInfoDict.Add(id, info); } } public enum ObjectType { Drug, Equip, Mat, }; public class ObjectInfo { public int id; public string name; public string icon_name; public ObjectType type; public int hp; public int mp; public int price_sell; public int price_buy; }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
- Unity3D運(yùn)行報(bào)DllNotFoundException錯(cuò)誤的解決方案
- unity avprovideo插件的使用詳解
- unity里獲取text中文字寬度并截?cái)嗍÷缘牟僮?/a>
- Unity3D啟動(dòng)外部程序并傳遞參數(shù)的實(shí)現(xiàn)
- unity 如何獲取button文本的內(nèi)容
- unity 如何使用文件流讀取streamingassets下的資源
- unity 文件流讀取圖片與www讀取圖片的區(qū)別介紹
- Unity之繞軸進(jìn)行旋轉(zhuǎn)的操作
- 解決unity rotate旋轉(zhuǎn)物體 限制物體旋轉(zhuǎn)角度的大坑
- 詳解Unity安卓共享紋理
相關(guān)文章
C#通過(guò)屬性名稱(chēng)獲取(讀取)屬性值的方法
本文主要介紹了C#通過(guò)屬性名稱(chēng)獲取(讀取)屬性值的方法,并提供了簡(jiǎn)化版代碼,具有很好的參考價(jià)值,需要的朋友可以看下2016-12-12輕松學(xué)習(xí)C#的foreach迭代語(yǔ)句
輕松學(xué)習(xí)C#的foreach迭代語(yǔ)句, C#語(yǔ)言提供了一個(gè)for語(yǔ)句循環(huán)的捷徑,而且還促進(jìn)了集合類(lèi)的更為一致,就是本文提到的foreach語(yǔ)句,感興趣的小伙伴們可以參考一下2015-11-11深入學(xué)習(xí)C#網(wǎng)絡(luò)編程之HTTP應(yīng)用編程(上)
這篇文章主要介紹了如何學(xué)習(xí)C#網(wǎng)絡(luò)編程之HTTP應(yīng)用編程的相關(guān)知識(shí),文中講解的非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06C# JSON格式化轉(zhuǎn)換輔助類(lèi) ConvertJson
本文介紹使用C#原生代碼實(shí)現(xiàn) JSON格式化以及各種類(lèi)型轉(zhuǎn)化JSON的輔助類(lèi),幫助開(kāi)發(fā)人員快速開(kāi)發(fā)。2016-04-04c# 重載WndProc,實(shí)現(xiàn)重寫(xiě)“最小化”的實(shí)現(xiàn)方法
在做“亦歌桌面版”的時(shí)候,發(fā)現(xiàn)當(dāng)打開(kāi)歌詞狀態(tài)下,用最小化隱藏窗體到托盤(pán)的話(huà)(如下code #1),在調(diào)出發(fā)現(xiàn)歌詞縮小了(雖然顯現(xiàn)的窗體大小跟剛才一樣),從這點(diǎn)看調(diào)用該方法其實(shí)窗體大小是改變了的(這個(gè)過(guò)程只是不可視而已)。2009-02-02