先看效果。

这几天在开发的过程中使用字符串拼接的时候遇到这个问题,之后发现是因为字符串分割过后有隐藏的字符串产生的(比如下面的"\r")。下面是代码。
cs
public class Mytext : MonoBehaviour
{
public TMP_Text mytext;
void Start()
{
string text1 = "你好"+"\r";
string text2 = "World";
mytext.text = text1 + text2;
}
}
下面是解决办法,直接在使用的时候加上Trim()就可以了,Trim()可以自动帮你去掉不必要的字符。
cs
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
public class Mytext : MonoBehaviour
{
public TMP_Text mytext;
void Start()
{
string text1 = "你好"+"\r";
string text2 = "World";
mytext.text = text1.Trim() + text2;
}
}
然后运行
