目录
问题:预制体的文本在Inspector直接用\n换行不生效
原因:因为unity会默认把\n替换成\\n
问题:预制体的文本在Inspector直接用\n换行不生效
预制体文本用换行符直接换好,如果要用代码替换换行,使用\n换行没有生效。
原因:因为unity会默认把\n替换成\\n
需要把\\n替换成\n,以下是我写的一个替换工具文章来源:https://uudwc.com/A/P5YBo
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();
}
}
文章来源地址https://uudwc.com/A/P5YBo