说明
遍历Unity场景和Prefab,提取Text组件文字,并导出Json表。可将Json文本进行多语言翻译后,利用工具将内容替换回原场景或Prefab。
代码
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class ChangeTextLanguageTool : MonoBehaviour
{
[ExecuteInEditMode]
[MenuItem("LanguageTool/一键提取Prefab中文字")]
private static void GetAllPrefab()
{
LoadPath(Application.dataPath);
}
static void LoadPath(string path)
{
TextData _temData = new TextData();
string genPath = path;
string[] filesPath = Directory.GetFiles(genPath, "*.prefab", SearchOption.AllDirectories);
for (int i = 0; i < filesPath.Length; i++)
{
PrefabData _tempPrefab = new PrefabData();
filesPath[i] = filesPath[i].Substring(filesPath[i].IndexOf("Assets"));
GameObject _prefab = AssetDatabase.LoadAssetAtPath(filesPath[i], typeof(GameObject)) as GameObject;
GameObject prefabGameobject = PrefabUtility.InstantiatePrefab(_prefab) as GameObject;
_tempPrefab.PrefabName = prefabGameobject.name;
Text[] _tempAllText = prefabGameobject.transform.GetComponentsInChildren<Text>(true);
for (int j = 0; j < _tempAllText.Length; j++)
{
if (!string.IsNullOrEmpty(_tempAllText[j].text))
{
TextItemData _tempItemdata = new TextItemData();
_tempItemdata._ObjName = _tempAllText[j].gameObject.name;
_tempItemdata._TextContext = _tempAllText[j].text