Unity文字排版错位问题

先看效果。

这几天在开发的过程中使用字符串拼接的时候遇到这个问题,之后发现是因为字符串分割过后有隐藏的字符串产生的(比如下面的"\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;
    }
}

然后运行

相关推荐
一个人旅程~3 天前
如何用命令行把win10/win11设置为长期暂停更新?
linux·windows·经验分享·电脑
爱搞虚幻的阿恺3 天前
Niagara粒子系统-超炫酷的闪电特效(加餐 纸牌螺旋上升效果)
游戏·游戏引擎
_Li.3 天前
Simulink - 6DOF (Euler Angles)
人工智能·算法·机器学习·游戏引擎·cocos2d
weixin_424294673 天前
Unity 调用Steamworks API 的 SteamUserStats.RequestCurrentStats()报错
unity·游戏引擎·steamwork
Factory_Audit3 天前
亚马逊社会责任验厂审核标准及注意事项
大数据·经验分享
江南小书生3 天前
制造业系统赋能成熟度自测表(实操版)
经验分享·非标制造
HoFunGames3 天前
Unity小地图,Easy Minimap System MT-GPS插件
unity·游戏引擎
三流架构师3 天前
述职报告资源合集
经验分享
徐先生 @_@|||3 天前
时间序列异常检测框架CrossAD论文阅读
经验分享·python·机器学习
LaughingZhu3 天前
Product Hunt 每日热榜 | 2026-02-25
数据库·人工智能·经验分享·神经网络·chatgpt