亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Unity怎么實現多語言轉換工具

發布時間:2020-06-28 17:08:01 來源:億速云 閱讀:254 作者:清晨 欄目:開發技術

小編給大家分享一下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<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;
     _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";
  /*if (!File.Exists(Path))
   File.Create(Path);*/
  string json = JsonUtility.ToJson(data);
  File.WriteAllText(Path, json);

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

 static Dictionary<string, PrefabData> m_dicTextData = new Dictionary<string, PrefabData>();

 [ExecuteInEditMode]
 [MenuItem("LanguageTool/一鍵替換LanguageTool字體")]
 static void ChangText()
 {
  TextAsset text = Resources.Load<TextAsset>("LanguageTool");
  TextData _data = JsonUtility.FromJson<TextData>(text.text);
  m_dicTextData = new Dictionary<string, PrefabData>();
  foreach (var item in _data.Data)
  {
   m_dicTextData[item.PrefabName] = item;
  }

  /*ChangeLabelText(Application.dataPath + "/App");
  ChangeLabelText(Application.dataPath + "/Cosmos");
  ChangeLabelText(Application.dataPath + "/Scenes");*/
  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<Text>(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<PrefabData> Data = new List<PrefabData>();
}

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

 public List<TextItemData> _PrefabData = new List<TextItemData>();
}

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

看完了這篇文章,相信你對Unity怎么實現多語言轉換工具有了一定的了解,想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

晋城| 宁蒗| 富平县| 佛坪县| 岚皋县| 施甸县| 南投县| 甘洛县| 雅安市| 延吉市| 阳高县| 资兴市| 宁波市| 四川省| 伊宁市| 河南省| 神农架林区| 玉屏| 河津市| 沂水县| 惠州市| 千阳县| 肇源县| 怀远县| 丰顺县| 将乐县| 东阿县| 东至县| 黄浦区| 彭阳县| 旺苍县| 丰县| 革吉县| 枝江市| 长治县| 千阳县| 武城县| 沙雅县| 泰州市| 新巴尔虎右旗| 玉龙|