在unity的ugui中Text控件,有时咱们会有各类各样的需求,好比相似html中css的text-overflow属性,但愿一段文字若是不够长就全显示,若是特别长就截断而且后面加上例如...这种后缀。css
好吧这样的需求在ugui里貌似没有现成的方法,若是有的话麻烦指点一下~html
大概的思路就是dom
首先要能判断何时overflow字体
而且支持加上后缀ui
那么text控件原本是支持overflow而后直接截断的,可是比较暴力,直接砍断,不能加后缀,并不知足咱们的需求。this
而后若是简单的经过字符个数截断,那根本不行,若是根据中英文字符来根据长度截断,这个我试过,然而字体并不必定是一个中文至关于俩英文字符,因而乎若是有一大排lllll或者iii什么的,悲剧无以言表。spa
因此咱们须要知道一段文字所对应的渲染以后的长度。若是从text的preferwidth或者经过添加content size filter组件应该也能完成相似任务,不过我倾向于直接算好长度去填充。.net
这个功能核心代码为code
Font myFont = text.font; //chatText is my Text component myFont.RequestCharactersInTexture(message, text.fontSize, text.fontStyle); CharacterInfo characterInfo = new CharacterInfo(); char[] arr = message.ToCharArray(); foreach (char c in arr) { myFont.GetCharacterInfo(c, out characterInfo, text.fontSize); totalLength += characterInfo.advance; }
其中text为Text文本控件,RequestCharactersInTexture
主要至关于指定须要渲染哪些字符(而后根据CharacterInfo.characterInfo
是能够拿到本次生成的去重后的字符集)。接下来经过myFont.GetCharacterInfo(c, out characterInfo, text.fontSize);
分别去得到每一个字符的信息,而后characterInfo.advance
就拿到了每一个字符的渲染长度。component
拿到每一个字符长度以后那就简单多了,计算一下须要截断的字符总长度,若是大于限制长度,就除去后缀长度后,截取子字符串,而后再接上后缀。这个事情就搞定了。
效果以下:
所有以下,这个例子是须要一个text和一个button,点击button,随机生成文字在text上。
using UnityEngine; using System.Collections; using UnityEngine.UI; public class TextWidth : MonoBehaviour { public Text text; public Button button; const string suffix = "..."; const int MAX_WIDTH = 200; int suffixWidth = 0; string[] seeds = { "是都", "60°", "qの", "【】" , "d a", "as", "WW", "II", "fs", "as", "WW", "II", "fs" }; // Use this for initialization void Start () { Init(); button.onClick.AddListener(Rand); } void Init() { //计算后缀的长度 suffixWidth = CalculateLengthOfText(suffix); Debug.Log("suffixWidth : " + suffixWidth); } string StripLengthWithSuffix(string input, int maxWidth = MAX_WIDTH) { int len = CalculateLengthOfText(input); Debug.Log("input total length = " + len); //截断text的长度,若是总长度大于限制的最大长度, //那么先根据最大长度减去后缀长度的值拿到字符串,在拼接上后缀 if (len > maxWidth) { return StripLength(input, maxWidth - suffixWidth) + suffix; }else { return input; } } //随机生成个字符串 void Rand() { int min = 12; int max = 16; int num = (int)(Random.value * (max - min) + min); Debug.Log("-------------------------\n num : " + num); string s = ""; for (int j = 0; j < num; j++) { int len = seeds.Length; int index = (int)(Random.value * (len)); s += seeds[index]; } Debug.Log("s : " + s); text.text = StripLengthWithSuffix(s); Debug.Log("StripLength " + text.text); } /// <summary> /// 根据maxWidth来截断input拿到子字符串 /// </summary> /// <param name="input"></param> /// <param name="maxWidth"></param> /// <returns></returns> string StripLength(string input, int maxWidth = MAX_WIDTH) { int totalLength = 0; Font myFont = text.font; //chatText is my Text component myFont.RequestCharactersInTexture(input, text.fontSize, text.fontStyle); CharacterInfo characterInfo = new CharacterInfo(); char[] arr = input.ToCharArray(); int i = 0; foreach (char c in arr) { myFont.GetCharacterInfo(c, out characterInfo, text.fontSize); int newLength = totalLength + characterInfo.advance; if (newLength > maxWidth) { Debug.LogFormat("newLength {0}, totalLength {1}: ", newLength, totalLength); if (Mathf.Abs(newLength - maxWidth) > Mathf.Abs(maxWidth - totalLength)){ break; }else { totalLength = newLength; i++; break; } } totalLength += characterInfo.advance; i++; } Debug.LogFormat("totalLength {0} : ", totalLength); return input.Substring(0, i); } /// <summary> /// 计算字符串在指定text控件中的长度 /// </summary> /// <param name="message"></param> /// <returns></returns> int CalculateLengthOfText(string message) { int totalLength = 0; Font myFont = text.font; //chatText is my Text component myFont.RequestCharactersInTexture(message, text.fontSize, text.fontStyle); CharacterInfo characterInfo = new CharacterInfo(); char[] arr = message.ToCharArray(); foreach (char c in arr) { myFont.GetCharacterInfo(c, out characterInfo, text.fontSize); totalLength += characterInfo.advance; } return totalLength; } }
这个效果基本达到要求,若是仔细看的话,并不能保证每一个截取后的字符串必定是对齐的,这个也跟字符串有关,毕竟字符串长度是离散的,貌似没有办法像word同样在一行多一个文字的时候还能够挤一挤放下~
VR开发或者unity相关交流能够邮件madcloudsong@qq .com
转载请注明原文连接
http://blog.csdn.net/madcloud...