当前位置:   article > 正文

Unity多语言转换工具_unity 提取所有prefab中的文字

unity 提取所有prefab中的文字

说明

遍历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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/959907
推荐阅读
相关标签
  

闽ICP备14008679号