private static void RemoveTextLocalization(UnityEditor.MenuCommand cmd)
        {
            UITextLocalizationManager current = cmd.context as UITextLocalizationManager;

            if (current == null)
            {
                return;
            }

            current.RemoveTextLocalization();
            EditorUtility.DisplayDialog("移除Text本地化组件", "移除Text本地化组件完成!", "OK");
        }
        private static void GetAllUITextLocalizations(UnityEditor.MenuCommand cmd)
        {
            UITextLocalizationManager current = cmd.context as UITextLocalizationManager;

            if (current == null)
            {
                Debug.LogError("统计失败!");
                return;
            }

            current.GetAllUITextLocalizations();
            Debug.Log("统计所有的Text本地化组件完成!");
        }
Пример #3
0
        /// <summary>
        /// 生成配置表关键字
        /// </summary>
        private void GenTextLocalization()
        {
            GenConfigKey();

            string filePath = EditorUtility.SaveFilePanel("生成配置表关键字", Application.dataPath + "/Resources/Localization/cn/ui_static", fileName + ".txt", "txt");

            if (string.IsNullOrEmpty(filePath))
            {
                Debug.Log("已取消生成配置表关键字!");
                return;
            }
            fileName = Path.GetFileNameWithoutExtension(filePath);

            foreach (var obj in System.Enum.GetValues(typeof(Language)))
            {
                Language languageType = (Language)obj;
                string   languageName = LanguageMgr.Instance.GetLanguageStr(languageType);

                filePath = string.Format("{0}/Resources/Localization/{1}/ui_static/{2}.txt", Application.dataPath, languageName, fileName);
                JsonData jsonData = new JsonData();
                //if (File.Exists(filePath))
                //{
                //    jsonData = JsonMapper.ToObject(ResourceMgr.instance.Load<TextAsset>(
                //        string.Format("Localization/{0}/ui_static/{1}", languageName, fileName)).text);
                //}

                //string value = GetComponent<TMPro.TextMeshProUGUI>().text;
                //if (languageType != Language.Chinese)
                //    value += string.Format("({0})", languageName);
                //jsonData[key] = value;

                //StringBuilder sb = new StringBuilder();
                //sb.Append("{\n");
                //foreach (string key in jsonData.Keys)
                //{
                //    sb.Append(string.Format("\"{0}\":\"{1}\",\n", key, jsonData[key]));
                //}
                //sb.Append("}");

                //StreamWriter sw = new StreamWriter(filePath, false, Encoding.UTF8);
                //sw.Write(sb.ToString());
                //sw.Close();
            }

            UITextLocalizationManager mgr = gameObject.GetComponentInParent <UITextLocalizationManager>();

            if (mgr != null)
            {
                mgr.GetAllUITextLocalizations();
            }
        }
Пример #4
0
        /// <summary>
        /// 生成配置文件关键字
        /// </summary>
        private void GenConfigKey()
        {
            UITextLocalizationManager textLocalizationMgr = gameObject.GetComponentInParent <UITextLocalizationManager>();
            Transform tfRoot = textLocalizationMgr == null ? transform.root : textLocalizationMgr.transform;

            fileName = tfRoot.name;

            key = transform.name;
            Transform tfParent = transform.parent;

            while (tfParent != null && tfParent != tfRoot.parent)
            {
                key      = tfParent.name + "/" + key;
                tfParent = tfParent.parent;
            }
        }
        private static void GenTextLocalization(UnityEditor.MenuCommand cmd)
        {
            UITextLocalizationManager current = cmd.context as UITextLocalizationManager;

            if (current == null)
            {
                return;
            }

            GameObject goSelected = current.gameObject;
            List <TMPro.TextMeshProUGUI> textMeshes = new List <TMPro.TextMeshProUGUI>();

            goSelected.GetComponentsInChildren <TMPro.TextMeshProUGUI>(true, textMeshes);
            textMeshes = textMeshes.Where((textMesh) =>
            {
                if (textMesh.tag == "TMPro.TextMeshProUGUI" || textMesh.tag == "TextIngnoreLocalization") //动态文本不处理
                {
                    return(false);
                }
                if (string.IsNullOrEmpty(textMesh.text)) //空文本不处理
                {
                    return(false);
                }
                float result;
                if (float.TryParse(textMesh.text, out result)) //数字不处理
                {
                    return(false);
                }
                return(true);
            }).ToList();

            if (textMeshes.Count == 0)
            {
                Debug.Log("没有Text组件需要本地化!");
                DestroyImmediate(current);
                return;
            }

            string fileName = goSelected.name;
            string filePath = EditorUtility.SaveFilePanel("生成Text本地化组件", Application.dataPath + "/Resources/Localization/cn/ui_static", fileName + ".txt", "txt");

            if (string.IsNullOrEmpty(filePath))
            {
                Debug.Log("已取消Text组件需要本地化!");
                return;
            }
            fileName = Path.GetFileNameWithoutExtension(filePath);

            foreach (var obj in System.Enum.GetValues(typeof(Language)))
            {
                Language languageType = (Language)obj;
                string   languageName = LanguageMgr.Instance.GetLanguageStr(languageType);

                filePath = string.Format("{0}/Resources/Localization/{1}/ui_static/{2}.txt", Application.dataPath, languageName, fileName);
                JsonData jsonData = new JsonData();
                //if (File.Exists(filePath))
                //{
                //    jsonData = JsonMapper.ToObject(ResourceMgr.instance.Load<TextAsset>(
                //        string.Format("Localization/{0}/ui_static/{1}", languageName, fileName)).text);
                //}

                //foreach (TMPro.TextMeshProUGUI textMesh in textMeshes)
                //{
                //    UITextLocalization textLocalization = textMesh.gameObject.GetComponent<UITextLocalization>();
                //    if (textLocalization == null)
                //        textLocalization = textMesh.gameObject.AddComponent<UITextLocalization>();
                //    else
                //        textLocalization.Reset();
                //    textLocalization.fileName = fileName;

                //    if (jsonData.Contains(textLocalization.key))
                //        continue;

                //    string value = textMesh.text;
                //    if (languageType != Language.Chinese)
                //        value += string.Format("({0})", languageName);
                //    jsonData[textLocalization.key] = value;
                //}

                //StringBuilder sb = new StringBuilder();
                //sb.Append("{\n");
                //foreach (string key in jsonData.Keys)
                //{
                //    sb.Append(string.Format("\"{0}\":\"{1}\",\n", key, jsonData[key]));
                //}
                //sb.Append("}");

                //StreamWriter sw = new StreamWriter(filePath, false, Encoding.UTF8);
                //sw.Write(sb.ToString());
                //sw.Close();
            }

            current.GetAllUITextLocalizations();
            AssetDatabase.Refresh();
            EditorUtility.DisplayDialog("生成Text本地化组件", "生成Text本地化组件完成!", "OK");
        }
Пример #6
0
        /// <summary>
        /// 移除配置文件关键字
        /// </summary>
        public void RemoveConfigKey()
        {
            bool removed = false;

            foreach (var obj in System.Enum.GetValues(typeof(Language)))
            {
                Language languageType = (Language)obj;
                string   languageName = LanguageMgr.Instance.GetLanguageStr(languageType);

                string filePath = string.Format("{0}/Resources/Localization/{1}/ui_static/{2}.txt", Application.dataPath, languageName, fileName);
                if (!File.Exists(filePath))
                {
                    continue;
                }

                string   staticConfigfilePath = string.Format(Define_Config.StaticTextConfigPath, languageName, fileName);
                string   dataStr  = ResourcesMgr.Instance.LoadFileSync(staticConfigfilePath);
                JsonData jsonData = JsonMapper.ToObject(dataStr);

                if (!jsonData.ContainsKey(key))
                {
                    return;
                }
                removed = true;

                if (jsonData.Keys.Count(x => x != key) == 0)
                {
                    File.Delete(filePath);
                    return;
                }

                StringBuilder sb = new StringBuilder();
                sb.Append("{\n");
                foreach (string tmpKey in jsonData.Keys)
                {
                    if (tmpKey == key)
                    {
                        continue;
                    }
                    sb.Append(string.Format("\"{0}\":\"{1}\",\n", tmpKey, jsonData[tmpKey]));
                }
                sb.Append("}");

                StreamWriter sw = new StreamWriter(filePath, false, Encoding.UTF8);
                sw.Write(sb.ToString());
                sw.Close();
            }

            if (removed)
            {
                key      = string.Empty;
                fileName = string.Empty;

                UITextLocalizationManager mgr = gameObject.GetComponentInParent <UITextLocalizationManager>();
                if (mgr != null)
                {
                    mgr.GetAllUITextLocalizations();
                }

                AssetDatabase.Refresh();
            }
        }