Unity提取场景中的静态文本

有些单机项目开发的时候没有做本地文本配置文件,全部写死在场景的对象上面,简单记录一下怎么提取场景里面的文本并且写入到配置文件里面

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]);

}

}

}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值