栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

Unity多语言转换工具的实现

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Unity多语言转换工具的实现

本文实例为大家分享了Unity多语言转换工具的具体代码,供大家参考,具体内容如下

说明

遍历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(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;
     _tempPrefab._PrefabData.Add(_tempItemdata);
    }
   }

   if (_tempPrefab._PrefabData.Count > 0)
    _temData.Data.Add(_tempPrefab);
   Debug.Log("遍历完成===========" + prefabGameobject.name);

   MonoBehaviour.DestroyImmediate(prefabGameobject);
  }

  string[] ScenePath = Directory.GetFiles(genPath, "*.unity", SearchOption.AllDirectories);
  
  for (int i = 0; i < ScenePath.Length; i++)
  {
   PrefabData _tempPrefab = new PrefabData();


   ScenePath[i] = ScenePath[i].Substring(ScenePath[i].IndexOf("Assets"));
   Debug.Log(ScenePath[i]);
   if (ScenePath[i].Contains("Live2D"))
    continue;
   _tempPrefab.PrefabName = ScenePath[i];

   EditorSceneManager.OpenScene(ScenePath[i], OpenSceneMode.Single);
   var _temp = Resources.FindObjectsOfTypeAll(typeof(Text)) as Text[];
   //_temp.Select(data => data.gameObject.scene.isLoaded);
   foreach (Text obj in _temp)
   {
    if(!obj.gameObject.scene.isLoaded)
     continue;
    //Debug.LogError(obj.text);
    if (!string.IsNullOrEmpty(obj.text))
    {
     TextItemData _tempItemdata = new TextItemData();
     _tempItemdata._ObjName = obj.gameObject.name;
     _tempItemdata._TextContext = obj.text;
     _tempPrefab._PrefabData.Add(_tempItemdata);
    }
   }

   if (_tempPrefab._PrefabData.Count > 0)
    _temData.Data.Add(_tempPrefab);
  }

  Save(_temData);
 }
 

 static void Save(TextData data)
 {
  string Path = Application.dataPath + "/LanguageTool.json";
  
  string json = JsonUtility.ToJson(data);
  File.WriteAllText(Path, json);

  EditorUtility.DisplayDialog("成功", "Prefab文本提取处理成功", "确定");
 }

 static Dictionary m_dicTextData = new Dictionary();

 [ExecuteInEditMode]
 [MenuItem("LanguageTool/一键替换LanguageTool字体")]
 static void ChangText()
 {
  TextAsset text = Resources.Load("LanguageTool");
  TextData _data = JsonUtility.FromJson(text.text);
  m_dicTextData = new Dictionary();
  foreach (var item in _data.Data)
  {
   m_dicTextData[item.PrefabName] = item;
  }

  
  ChangeLabelText(Application.dataPath);
  EditorUtility.DisplayDialog("成功", "替换成功", "确定");
 }

 static void ChangeLabelText(string path)
 {
  string genPath = path;
  int m = 0;
  string[] filesPath = Directory.GetFiles(genPath, "*.prefab", SearchOption.AllDirectories);
  for (int i = 0; i < filesPath.Length; i++)
  {
   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;
   PrefabData _tempData = null;
   if (m_dicTextData.ContainsKey(prefabGameobject.name))
   {
    _tempData = m_dicTextData[prefabGameobject.name];
   }

   Text[] _tempAllText = prefabGameobject.transform.GetComponentsInChildren(true);
   for (int j = 0; j < _tempAllText.Length; j++)
   {
    if (!string.IsNullOrEmpty(_tempAllText[j].text))
    {
     if (null != _tempData && _tempData._PrefabData.Count > 0)
     {
      for (int z = 0; z < _tempData._PrefabData.Count; z++)
if (_tempData._PrefabData[z]._ObjName == _tempAllText[j].gameObject.name)
{
 _tempAllText[j].text = _tempData._PrefabData[z]._TextContext;
 _tempData._PrefabData.RemoveAt(z);
 break;
}
     }
    }
   }

   PrefabUtility.SaveAsPrefabAsset(prefabGameobject, filesPath[i]);

   Debug.Log("遍历完成===========" + prefabGameobject.name);

   MonoBehaviour.DestroyImmediate(prefabGameobject);
   AssetDatabase.SaveAssets();
  }
  string[] ScenePath = Directory.GetFiles(genPath, "*.unity", SearchOption.AllDirectories);
  
  for (int i = 0; i < ScenePath.Length; i++)
  {
   ScenePath[i] = ScenePath[i].Substring(ScenePath[i].IndexOf("Assets"));
   Debug.Log(ScenePath[i]);
   if (ScenePath[i].Contains("Live2D"))
    continue;
   PrefabData _tempData = null;
   if (m_dicTextData.ContainsKey(ScenePath[i]))
   {
    _tempData = m_dicTextData[ScenePath[i]];
   }

   Scene _tempScene = EditorSceneManager.OpenScene(ScenePath[i], OpenSceneMode.Single);
   


   //foreach (Text obj in UnityEngine.Object.FindObjectsOfType(typeof(Text)))
   var _temp = Resources.FindObjectsOfTypeAll(typeof(Text)) as Text[];
   //_temp.Select(data => data.gameObject.scene.isLoaded);
   foreach (Text obj in _temp)
   {
    if(!obj.gameObject.scene.isLoaded)
     continue;
    if (!string.IsNullOrEmpty(obj.text))
    {
     if (null != _tempData && _tempData._PrefabData.Count > 0)
     {
      for (int z = 0; z < _tempData._PrefabData.Count; z++)
if (_tempData._PrefabData[z]._ObjName == obj.gameObject.name)
{
 obj.text = _tempData._PrefabData[z]._TextContext;
 _tempData._PrefabData.RemoveAt(z);

 break;
}
     }
    }
   }

   EditorSceneManager.SaveScene(_tempScene);
   AssetDatabase.SaveAssets();
  }
 }
}

[Serializable]
public class TextData
{
 public List Data = new List();
}

[Serializable]
public class PrefabData
{
 public string PrefabName = string.Empty;

 public List _PrefabData = new List();
}

[Serializable]
public class TextItemData
{
 public string _ObjName = string.Empty;
 public string _TextContext = string.Empty;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/122011.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号