Exemplo n.º 1
0
    //[MenuItem("Localization Tools/一键导入所有文本(HashCode匹配)")]
    private static void ImportAllText()
    {
        string ExportText = Application.dataPath + "--LocalizationText.csv";
        Dictionary <string, List <PrefabTextInfo> > LocalizationDic = new Dictionary <string, List <PrefabTextInfo> >();

        LocalizationDic.Clear();
        System.Text.UTF8Encoding utf8 = new System.Text.UTF8Encoding();

        if (File.Exists(ExportText))
        {
            string       line;
            string[]     temp;
            int          lineIndex = 0;
            StreamReader sr        = new StreamReader(ExportText);
            while ((line = sr.ReadLine()) != null)
            {
                temp = line.Split(',');
                lineIndex++;
                if (temp.Length < 3)
                {
                    Debug.LogError("第" + lineIndex + "行数据不符合要求,已忽略");
                    continue;
                }
                string key       = temp[0];
                int    _HashCode = int.Parse(temp[1]);

                string         _Info    = utf8.GetString(utf8.GetBytes(temp[2])).Replace("\\n", "\n");
                PrefabTextInfo textInfo = new PrefabTextInfo();
                textInfo._HashCode = _HashCode;
                textInfo._Info     = _Info;
                if (LocalizationDic.ContainsKey(key))
                {
                    List <PrefabTextInfo> textList = LocalizationDic[key];
                    if (!textList.Contains(textInfo))
                    {
                        textList.Add(textInfo);
                    }

                    LocalizationDic[key] = textList;
                }
                else
                {
                    List <PrefabTextInfo> textList = new List <PrefabTextInfo>();

                    textList.Add(textInfo);
                    LocalizationDic.Add(key, textList);
                }
            }
            sr.Close();
            sr.Dispose();
        }

        //int x = 0;

        foreach (var item in LocalizationDic)
        {
            string     assetPath = item.Key;
            var        testList  = item.Value;
            GameObject go        = AssetDatabase.LoadAssetAtPath <GameObject>(assetPath);
            if (null == go)
            {
                continue;
            }
            Text[] allTextComp = go.GetComponentsInChildren <Text>();
            foreach (var textComp in allTextComp)
            {
                int _HashCode = textComp.GetHashCode();

                string textInfo = GetText(_HashCode, testList);
                if ("" != textInfo)
                {
                    textComp.text = textInfo;
                }
            }

            EditorUtility.SetDirty(go);
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }

        AssetDatabase.Refresh();
        EditorUtility.DisplayDialog("", "导入完成", "OK");
    }
Exemplo n.º 2
0
    //[MenuItem("Localization Tools/一键导出所有文本(HashCode匹配)")]
    private static void ExportAllText()
    {
        string uiDir       = "Assets/Prefabs/UI";
        string csvFileName = Application.dataPath + "/../Localization/LocalizationText.csv";

        if (!EditorUtility.DisplayDialog("提示", string.Format("将要把 {0} 下的prefab的所有text导出到文件 {1} 中, 确定吗?", uiDir, csvFileName), "确定"))
        {
            return;
        }

        Dictionary <string, List <PrefabTextInfo> > LocalizationDic = new Dictionary <string, List <PrefabTextInfo> >();

        LocalizationDic.Clear();

        string[] allGuids = AssetDatabase.FindAssets("t:Prefab", new string[] { uiDir });
        int      i        = 0;

        foreach (string guid in allGuids)
        {
            string assetPath = AssetDatabase.GUIDToAssetPath(guid);
            if (assetPath.EndsWith(".prefab"))
            {
                List <PrefabTextInfo> textList = new List <PrefabTextInfo>();
                textList.Clear();
                GameObject go = AssetDatabase.LoadAssetAtPath <GameObject>(assetPath);
                if (null == go)
                {
                    continue;
                }
                Text[] allTextComp = go.GetComponentsInChildren <Text>();

                foreach (var item in allTextComp)
                {
                    PrefabTextInfo textInfo = new PrefabTextInfo();
                    textInfo._HashCode = item.GetHashCode();

                    textInfo._Info = item.text.Replace("\n", "\\n");
                    if (!textList.Contains(textInfo))
                    {
                        textList.Add(textInfo);
                    }
                    else
                    {
                        Debug.LogError(assetPath + item.GetHashCode() + "text出现同 HashCode 预制体有误,请检查");
                    }
                }

                if (!LocalizationDic.ContainsKey(assetPath))
                {
                    LocalizationDic.Add(assetPath, textList);
                }
            }
        }

        if (LocalizationDic.Count > 0)
        {
            using (FileStream fs = new FileStream(csvFileName, FileMode.Create))
            {
                if (!fs.CanWrite)
                {
                    Debug.LogError(string.Format("The {0} Is Locked", csvFileName));
                }
                StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
                foreach (KeyValuePair <string, List <PrefabTextInfo> > item in LocalizationDic)
                {
                    foreach (var innerItem in item.Value)
                    {
                        sw.WriteLine(string.Format("{0},{1},{2}", item.Key, innerItem._HashCode, innerItem._Info));
                    }
                }
                sw.Dispose();
                sw.Close();
                fs.Dispose();
                fs.Close();
            }
            if (EditorUtility.DisplayDialog("", "导出完成, 请查看", "确定"))
            {
                OpenExplorer(csvFileName);
            }
        }
        else
        {
            EditorUtility.DisplayDialog("", "啥都没有导出个屁", "OK");
        }
    }