问题原因:
在使用TEXT的时候 会发现 如果在中文与中文之间打空格会导致文本换行,这是因为中文半角空格 与英文空格相同,text默认为英文空格,会自动判断你空格后单词能否放下,如果这行放不下就自动给你换行,应用在中文字符上,就判断后面的所有为一个单词,所以就换行了。
解决办法:
1.换成全角空格
简单粗暴直接, 不过很难看(放弃)
2.替换text中的空格编码格式
using UnityEngine.UI;
using UnityEngine;
[RequireComponent(typeof(Text))]
public class NonBreakingSpaceTextComponent : MonoBehaviour
{
public static readonly string no_breaking_space = "\u00A0";
protected Text text;
// Use this for initialization
void Awake ()
{
text = this.GetComponent<Text>();
text.RegisterDirtyVerticesCallback(OnTextChange);
}
public void OnTextChange()
{
if (text.text.Contains(" "))
{
text.text = text.text.Replace(" ", no_breaking_space);
}
}
}
将NoBreakingSpaceTextComponent挂在Text组件上,每当Text设置text文字,准备重新绘制Text网格时,NoBreakingSpaceTextComponent会检查并替换text文字里的换行空格。
https://blog.youkuaiyun.com/qq_41516375/article/details/102860801?utm_medium=distribute.pc_relevant.none-task-blog-2defaultbaidujs_baidulandingword~default-0-102860801-blog-125375767.235v43pc_blog_bottom_relevance_base7&spm=1001.2101.3001.4242.1&utm_relevant_index=3