最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Unity多語(yǔ)言轉(zhuǎn)換工具的實(shí)現(xiàn)

 更新時(shí)間:2020年06月28日 11:32:16   作者:貝塔小可愛(ài)  
這篇文章主要為大家詳細(xì)介紹了Unity多語(yǔ)言轉(zhuǎn)換工具的實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Unity多語(yǔ)言轉(zhuǎn)換工具的具體代碼,供大家參考,具體內(nèi)容如下

說(shuō)明

遍歷Unity場(chǎng)景和Prefab,提取Text組件文字,并導(dǎo)出Json表??蓪son文本進(jìn)行多語(yǔ)言翻譯后,利用工具將內(nèi)容替換回原場(chǎng)景或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;
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

安泽县| 平远县| 莎车县| 长汀县| 藁城市| 资兴市| 土默特左旗| 加查县| 巨鹿县| 高尔夫| 永宁县| 咸丰县| 恩施市| 东源县| 峨边| 晋宁县| 开封市| 湘阴县| 晋州市| 吉木萨尔县| 板桥市| 抚远县| 永州市| 青川县| 永定县| 镇江市| 开江县| 呼和浩特市| 天长市| 石景山区| 克什克腾旗| 商洛市| 沁源县| 星子县| 承德市| 唐海县| 仪征市| 寿宁县| 张家川| 乌鲁木齐市| 增城市|