当前位置:网站首页>Untiy文本框的代码换行问题

Untiy文本框的代码换行问题

2022-07-06 18:43:00 吴梓穆

如果是我们主动指定换行,我们可以使用\n来换行
例如

 infoText.text = "id:" + monitor.id + "\n" +
                        "cameraType:" + monitor.cameraType + "\n" +
                        "workshopName:" + monitor.workshopName+ "\n" +
                        "camerasIP:" + monitor.camerasIp + "\n" +
                        "camerasUser:" + monitor.camerasUser + "\n" +
                        "camerasPwd:" + monitor.camerasPwd + "\n" +
                        "camerasPort:" + monitor.camerasPort + "\n" +
                        "monitorName:" + monitor.monitorName+"\n"+
                        "region:"+monitor.region+"\n"+
                        "code:"+monitor.code

如果文字内容来自别处,比如从服务器下载,如果文字里自带 \n换行,直接把文字放进text里,\n会被识别为字符,而不会换行,这时因为Untiy非常好心的帮我们自动吧 \n替换为\n了(尽管你很确定你拿到的数据就是\n),所以我们要手动把\n替换为\n,防止被转义
例如

    BookRequestResult bookResult = JsonConvert.DeserializeObject<BookRequestResult>(request.downloadHandler.text);
                    contentText.text = bookResult.data.book;
                    contentText.text = contentText.text.Replace("\\n","\n");
                    contentText.text = contentText.text.Replace("\\r","\r");
原网站

版权声明
本文为[吴梓穆]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_44568736/article/details/125593801