unity 如何在text中的单词过长时 避免自动添加空格并换行


在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;
    }
}

使用上述脚本后

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM