Пример #1
0
        void OnGUI()
        {
            this.assetBundleName = EditorGUILayout.TextField("asset bundle name", assetBundleName);

            if (GUILayout.Button("find"))
            {
                if (string.IsNullOrEmpty(this.assetBundleName))
                {
                    return;
                }

                List <UnityEngine.Object> objList = new List <UnityEngine.Object>();

                var guids = AssetDatabase.FindAssets("t:object", new string[] { "Assets/Game" });
                using (ProgressIndicator progress = new ProgressIndicator("...", guids.Length))
                {
                    foreach (var guid in guids)
                    {
                        var path = AssetDatabase.GUIDToAssetPath(guid);
                        var deps = AssetDatabase.GetDependencies(path);

                        if (progress.Show(path + "(" + deps.Length + ")"))
                        {
                            Debug.Log("break");
                            return;
                        }

                        foreach (var dep in deps)
                        {
                            var depAsset = AssetImporter.GetAtPath(dep);
                            if (depAsset != null && depAsset.assetBundleName == this.assetBundleName)
                            {
                                var assetObj = AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(path);
                                if (!objList.Contains(assetObj))
                                {
                                    objList.Add(assetObj);
                                }
                            }
                        }

                        progress.Next();
                    }
                }

                this.objs = objList.ToArray();
            }

            if (this.objs != null)
            {
                this.scrollView = GUILayout.BeginScrollView(this.scrollView);
                foreach (var obj in this.objs)
                {
                    GUILayout.BeginHorizontal();
                    GUILayout.Label(AssetDatabase.GetAssetPath(obj), GUILayout.Width(this.position.width / 2));
                    EditorGUILayout.ObjectField(obj, typeof(UnityEngine.Object), false, GUILayout.Width(this.position.width / 2 - 10));
                    GUILayout.EndHorizontal();
                }
                GUILayout.EndScrollView();
            }
        }
Пример #2
0
        bool GenerateUIKeys(ref List <string> keys)
        {
            var rootPath = this.serializedObject.FindProperty("uiPrefabRootPath").stringValue;

            if (rootPath == "" | rootPath.StartsWith("#"))
            {
                return(true);
            }

            var strRegex = this.uiTextRegex.stringValue;
            var guids    = AssetDatabase.FindAssets("t:prefab", new string[] { rootPath });

            using (ProgressIndicator progress = new ProgressIndicator("generate ui keys", guids.Length))
            {
                foreach (var guid in guids)
                {
                    var path = AssetDatabase.GUIDToAssetPath(guid);
                    if (progress.Show("generating ui:{0}", path))
                    {
                        return(false);
                    }

                    var prefab = AssetDatabase.LoadAssetAtPath <GameObject>(path);
                    var texts  = prefab.GetComponentsInChildren <Text>(true);

                    foreach (var text in texts)
                    {
                        if (text.text != "" && Regex.IsMatch(text.text, strRegex))
                        {
                            if (!keys.Contains(text.text))
                            {
                                keys.Add(text.text);
                            }
                        }
                    }

                    progress.Next();
                }
            }

            AssetDatabase.SaveAssets();

            return(true);
        }
Пример #3
0
        bool ReplaceResource()
        {
            var directories = Directory.GetDirectories("Assets/Game", @".*_I18N", SearchOption.AllDirectories);

            foreach (var dir in directories)
            {
                var targetFolder = dir.Replace("\\.", "\\").Replace("_I18N", "");
                var i18nFolder   = dir + "\\" + I18NConfig.Instance.Language;
                Debug.Log("!! dir " + dir + "," + targetFolder);

                using (ProgressIndicator progress = new ProgressIndicator("...", directories.Length))
                {
                    var filePaths = Directory.GetFiles(i18nFolder, "*", SearchOption.AllDirectories);
                    foreach (var filePath in filePaths)
                    {
                        //var fileName = filePath.Substring(filePath.LastIndexOf('\\') + 1);
                        var targetFilePath = filePath.Replace("\\.", "\\").Replace("_I18N\\zh_cht", "");

                        if (progress.Show(filePath + " -> " + targetFilePath))
                        {
                            return(false);
                        }

                        if (!File.Exists(targetFilePath))
                        {
                            Debug.LogError("replace faild = " + filePath + " -> " + targetFilePath);
                            continue;
                        }

                        File.Copy(filePath, targetFilePath, true);

                        progress.Next();
                    }
                }
            }
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();

            Debug.Log("replace i18n resources finish.");

            return(true);
        }
Пример #4
0
        bool TranslatePrefabText()
        {
            // fonts
            var strRefFontRegex = "/Fonts+|/TTF+";
            var guids           = AssetDatabase.FindAssets("t:prefab", new string[] { "Assets/Game/UIs" });
            var strRegex        = this.uiTextRegex.stringValue;

            using (ProgressIndicator progress = new ProgressIndicator("...", guids.Length))
            {
                foreach (var guid in guids)
                {
                    var path   = AssetDatabase.GUIDToAssetPath(guid);
                    var prefab = AssetDatabase.LoadAssetAtPath <GameObject>(path);
                    var texts  = prefab.GetComponentsInChildren <Text>(true);

                    if (progress.Show("translating prefab " + path))
                    {
                        return(false);
                    }

                    bool isAnyChange = false;
                    foreach (var text in texts)
                    {
                        // text regex
                        if (text.text != "" && Regex.IsMatch(text.text, strRegex))
                        {
                            var translateText = I18NConfig.Instance.TranslateText(text.text);
                            if (string.IsNullOrEmpty(translateText))
                            {
                                Debug.LogError("translate text is null or empty, origin text = " + text.text);
                            }
                            else
                            {
                                text.text   = translateText;
                                isAnyChange = true;
                            }
                        }

                        var assetPath = AssetDatabase.GetAssetPath(
                            text.font.GetInstanceID());

                        // font reference
                        if (Regex.IsMatch(assetPath, strRefFontRegex))
                        {
                            var translateAssetPath = assetPath.Replace("Fonts", "Fonts_I18N/zh_cht");
                            translateAssetPath = translateAssetPath.Replace("TTF", "TTF_I18N/zh_cht");
                            var font = AssetDatabase.LoadAssetAtPath <Font>(translateAssetPath);
                            if (font == null)
                            {
                                Debug.LogWarning("not found " + translateAssetPath);
                            }
                            else
                            {
                                text.font   = font;
                                isAnyChange = true;
                            }
                        }
                    }

                    if (isAnyChange)
                    {
                        EditorUtility.SetDirty(prefab);
                    }

                    progress.Next();
                }
            }

            //
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            Debug.Log("translate i18n prefab finish.");

            return(true);
        }
Пример #5
0
        void GenerateOriginCsvFile(bool isClear)//isClear == false
        {
            Debug.Log("start generate origin csv file...");
            var originCsvFilePath = this.serializedObject.FindProperty("originCsvFilePath").stringValue;

            // 检测原始文件是否已经存在,记录起来做增量判断
            List <string> oldKeys = null;

            if (!isClear)
            {
                using (StreamReader sr = new StreamReader(originCsvFilePath, Encoding.UTF8))
                {
                    if (sr != null)
                    {
                        var content = sr.ReadToEnd();
                        if (content != "") // 空也当做没有
                        {
                            using (CsvReader cr = new CsvReader(new StringReader(content)))
                            {
                                oldKeys = new List <string>();
                                //cr.ReadHeader();
                                while (cr.Read())
                                {
                                    oldKeys.Add(cr[0]);
                                }
                            }
                        }
                    }
                }
            }

            // 解析脚本和ui
            List <string> keys = new List <string>();

            if (!this.GenerateScriptKeys(ref keys))
            {
                return;
            }
            if (!this.GenerateUIKeys(ref keys))
            {
                return;
            }

            using (ProgressIndicator progress = new ProgressIndicator("genreate csv", 2))
            {
                if (oldKeys != null)
                {
                    // 筛选出无用key和增量k
                    progress.Show("remove old keys and add new keys");
                    for (int i = oldKeys.Count - 1; i >= 0; i--)
                    //foreach(var k in oldKeys)
                    {
                        var  oldK   = oldKeys[i];
                        bool isFind = false;
                        foreach (var newK in keys)
                        {
                            if (newK == oldK)
                            {
                                isFind = true;
                                keys.Remove(newK);
                                break;
                            }
                        }

                        // 已经用不到的key就删掉
                        if (!isFind && !oldK.StartsWith("record"))
                        {
                            oldKeys.Remove(oldK);
                        }
                    }
                    progress.Next();
                }

                if (keys.Count > 0)
                {
                    progress.Show("generate csv file");
                    using (CsvWriter cw = new CsvWriter(new StreamWriter(originCsvFilePath, false, Encoding.UTF8)))
                    {
                        cw.WriteHeader(typeof(CsvData));
                        if (oldKeys != null)
                        {
                            // 旧key
                            foreach (var key in oldKeys)
                            {
                                cw.WriteField(key);
                                cw.WriteField("");
                                cw.NextRecord();
                            }
                        }

                        // 新key
                        foreach (var key in keys)
                        {
                            cw.WriteField(key);
                            cw.WriteField("");
                            cw.NextRecord();
                        }

                        cw.WriteField("record" + DateTime.Now.ToString("yyyyMMdd-HHmmss"));
                        cw.WriteField("");
                        cw.NextRecord();
                    }
                    progress.Next();
                }
            }

            AssetDatabase.Refresh(ImportAssetOptions.Default);

            Debug.Log("finished");
        }
Пример #6
0
        bool GenerateScriptKeys(ref List <string> keys)//todo
        {
            var rootPath = this.serializedObject.FindProperty("scriptFileRootPath").stringValue;

            if (rootPath == "" | rootPath.StartsWith("#"))
            {
                return(true);
            }

            var strRegex = this.scriptRegex.stringValue;//todo
            var files    = Directory.GetFiles(
                rootPath, "*.lua", SearchOption.AllDirectories);

            using (ProgressIndicator progress = new ProgressIndicator("generate script keys", files.Length))
            {
                foreach (var file in files)
                {
                    if (file.Contains("i18n_translate.lua"))
                    {
                        continue;
                    }

                    if (progress.Show("generating script:{0}", file))
                    {
                        return(false);
                    }

                    using (StreamReader sr = new StreamReader(file, Encoding.UTF8))
                    {
                        //var content = sr.ReadToEnd();
                        //var matches = Regex.Matches(content, strRegex);
                        //for (int i=0; i<matches.Count; i++)
                        //{
                        //    var match = matches[i];
                        //    if (match.Value != "" && !keys.Contains(match.Value))
                        //        keys.Add(match.Value);
                        //}

                        string line = sr.ReadLine();
                        while (line != null)
                        {
                            var matches = Regex.Matches(line, strRegex);
                            for (int i = 0; i < matches.Count; i++)
                            {
                                var match = matches[i];

                                string fixMatch = match.Value;
                                fixMatch = fixMatch.Remove(0, 1);
                                fixMatch = fixMatch.Substring(0, fixMatch.Length - 1);
                                //Debug.Log(fixMatch);
                                fixMatch = fixMatch.Replace("\\r\\n", "\r\n");
                                fixMatch = fixMatch.Replace("\\n", "\n");
                                if (fixMatch != "" && !keys.Contains(fixMatch))
                                {
                                    keys.Add(fixMatch);
                                }
                            }

                            line = sr.ReadLine();
                        }
                    }

                    progress.Next();
                }
            }

            return(true);
        }