How to realize the indentation of the first line in Unity's Text component

Generally speaking, it is impossible to enter the first line of spaces in MyText.text.
For example, enter the following content:

    第一段:如何在text里面输入空格
一般而言,MyText.text里面要输入首行空格,那是不可能的。

What method do you use? Of course, [full-width space "\u3000"] is used.

#if UNITY_EDITOR
    [ContextMenu("加载文字")]
    void LoadContent()
    {
    
    
        this.GetComponent<Text>().text = "";
        string content = "";
        foreach(string str in paraContent)
        {
    
    
            //生成了一段又一段
            content += "\u3000" + "\u3000" + str + "\n";

        }

        this.GetComponent<Text>().text = content;
    }
#endif

In this code, \u3000 represents a full-width space, through content += "\u3000" + "\u3000" + str + "\n"; This line of code realizes adding two full-width spaces before each line of text in the current paragraph space, thus achieving the effect of indenting the first line.

Specifically, + "\u3000" + "\u3000" + str is the main part of adding spaces in this code, where \u3000 represents a full-width space, and it can be repeated multiple times if more spaces are needed.

In addition, because this code uses "\n" to achieve line breaks, you need to ensure that the Text component selects the Horizontal Overflow mode that supports line breaks, for example, the value of Overflow is Wrap or Overflow. Otherwise, the text will not automatically wrap, but a horizontal scroll bar will be generated.

Guess you like

Origin blog.csdn.net/dzj2021/article/details/130100393