当前位置: 首页 > news >正文

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

}

}

}

http://www.lryc.cn/news/17485.html

相关文章:

  • Netty常用核心类说明
  • ingress服务
  • java 抽象类 详解
  • MySQL的安装(详解)
  • 界面控件DevExpress WinForm——轻松构建类Visual Studio UI(二)
  • BabylonJS之放烟花
  • vue3 布局样式的原理
  • Qt程序使用路径方式和注意事项
  • 和日期相关的代码和bug——一道力扣题中的小发现
  • 如何在2023年学习编程并获得开发者工作
  • Python实战之小说下载神器(三)排行榜所有小说:最全热门小说合集,总有一款适合你,好多好多好多超赞的小说...(源码分享学习)
  • 前端监控之用户行为监控实践1(数据收集)
  • 【网络原理7】认识HTTP
  • SPI实验
  • 去基线处理
  • 模拟信号4-20mA /0-5V/0-75mV/0-100mV转RS-485/232,数据采集A/D转换模块 YL21
  • [USB]键盘数据格式以及按键键值
  • web客户端-websocket
  • mysql间隙锁
  • 华为OD机试 - 计算面积(Java) | 机试题+算法思路+考点+代码解析 【2023】
  • Python 之 Pandas 时间戳、通过时间间隔实现 datetime 加减、时间转化、时期频率转换和 shift() 时间频率进行移位)
  • 一篇文章搞定linux网络模型
  • 惠普庆祝在中国40年,强化中国发展战略
  • C++小作业
  • Python基础 — lambda匿名函数
  • MongoDB安装和使用过程常见问题
  • AWS攻略——使用中转网关(Transit Gateway)连接同区域(Region)VPC
  • Rouge | 自动文摘及机器翻译评价指标
  • 【Python入门第十五天】Python字典
  • java学习思路