有些单机项目开发的时候没有做本地文本配置文件,全部写死在场景的对象上面,简单记录一下怎么提取场景里面的文本并且写入到配置文件里面
using System.Collections.Generic;
using System.IO;
using TMPro;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
public class Test : MonoBehaviour
{
static List<string> txtArray = new List<string>();
static string labelStr = "_config_Label_";
[MenuItem("Tools/ÌáÈ¡Îı¾")]
static void GetAllSceneObjectsWithInactive()
{
ReadFile();
var allGos = Resources.FindObjectsOfTypeAll(typeof(GameObject));
var previousSelection = Selection.objects;
Selection.objects = allGos;
var selectedTransforms = Selection.GetTransforms(SelectionMode.Editable | SelectionMode.ExcludePrefab);
Selection.objects = previousSelection;
int index = 0;
foreach (var trans in selectedTransforms)
{
Text text = trans.GetComponent<Text>();
string textContent = "";
if (text == null)
{
TextMeshProUGUI textMeshPro = trans.GetComponent<TextMeshProUGUI>();
if (textMeshPro != null)
{
textContent = textMeshPro.text;
}
}
else
{
textContent = text.text;
}
if (!string.IsNullOrEmpty(textContent)&&!trans.name.Contains("LiberationSans SDF Atlas"))
{
UILanguageText uILanguageText= trans.gameObject.AddComponent<UILanguageText>();
index = index + 1;
string content = "";
if (textContent.Contains("\n"))
{
string[] temp = textContent.Split('\n');
for (int i = 0; i < temp.Length; i++)
{
if (i< temp.Length -1)
{
content = content + temp[i] + "\\n";
}
else
{
content = content + temp[i];
}
}
}
else
{
content = textContent;
}
string languageKey = string.Format("{0}{1}{2}", trans.name, labelStr, index);
uILanguageText.languageKey = languageKey;
txtArray.Add(string.Format("{0},{1}", languageKey, content));
Debug.Log(trans.name+":::"+ textContent);
}
}
WriteFile();
Debug.Log("ÌáÈ¡½áÊø");
}
static void WriteFile()
{
File.WriteAllLines(Application.dataPath + "/Resources/Language/CN.txt", txtArray);
}
static void ReadFile()
{
txtArray.Clear();
string[] strs = File.ReadAllLines(Application.dataPath + "/Resources/Language/CN.txt");
for (int i = 0; i < strs.Length; i++)
{
txtArray.Add(strs[i]);
}
}
}