在unity中,text里如果單詞過長會自動往此行末尾留空並換行。
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class WenBenHuanHang : MonoBehaviour { [SerializeField] Text mText; private bool m_IsSpace=true; // Use this for initialization void Start () { settext(); } // Update is called once per frame void Update () { } private void settext() { //獲取Text的文本內容 string temp_content = mText.text; string temp_indent = ""; //首行縮進的字符數 int m_Text_indent = 2; for (int i = 0; i < m_Text_indent; i++) { temp_indent = string.Format("{0}{1}", temp_indent, "\u3000"); } temp_content = string.Format("{0}{1}", temp_indent, mText.text); //處理空格 if (m_IsSpace) { temp_content = temp_content.Replace(" ", "\u3000"); } //首行縮進替換 temp_content = temp_content.Replace("\n", "\n" + temp_indent); //重新設置Text文字 mText.text = temp_content; } }
使用上述腳本后