目录
问题:预制体的文本在Inspector直接用\n换行不生效
原因:因为unity会默认把\n替换成\\n
问题:预制体的文本在Inspector直接用\n换行不生效
预制体文本用换行符直接换好,如果要用代码替换换行,使用\n换行没有生效。文章来源:https://www.toymoban.com/news/detail-502013.html
原因:因为unity会默认把\n替换成\\n
需要把\\n替换成\n,以下是我写的一个替换工具文章来源地址https://www.toymoban.com/news/detail-502013.html
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
using System;
using System.IO;
/// <summary>
/// prefab文本换行替换
/// </summary>
public class PrefabNewLineEditor:EditorWindow
{
private static string PREFAB_PATH = "Assets/Prefab";
[MenuItem("Tools/UI/文本换行替换")]
public static void PrefabNewLineText()
{
string[] allPath = AssetDatabase.FindAssets("t:Prefab",new string[] { PREFAB_PATH } );
for (int i = 0; i < allPath.Length; i++)
{
string path = AssetDatabase.GUIDToAssetPath(allPath[i]);
var obj = PrefabUtility.InstantiatePrefab(AssetDatabase.LoadAssetAtPath(path, typeof(GameObject))) as GameObject ;
if (obj != null)
{
var texts = obj.GetComponentsInChildren<Text>(true);
foreach (Text text in texts)
{
text.text = text.text.Replace("\\n", "\n");
}
}
PrefabUtility.SaveAsPrefabAsset(obj, path);
GameObject.DestroyImmediate(obj);
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
}
到了这里,关于unity 预制文本\n换行不起作用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!