using System.Collections.Generic; using System.IO; using System.Linq; using AlicizaX.Editor; using UnityEngine; using UnityEditor; using UnityEditor.Callbacks; namespace AlicizaX.Localization.Editor { [CustomEditor(typeof(GameLocaizationTable))] internal class GameLocaizationTableEditor : InspectorEditor { [OnOpenAsset] public static bool OnOpenAsset(int instanceId, int line) { var obj = EditorUtility.InstanceIDToObject(instanceId); var asset = obj as GameLocaizationTable; if (asset == null) return false; string path = AssetDatabase.GetAssetPath(asset); EditorPrefs.SetString("LastSelectedGameLocaizationTable", path); OpenLocalizationEditor(); return true; } public override void OnEnable() { base.OnEnable(); } public override void OnInspectorGUI() { EditorDrawing.DrawInspectorHeader(new GUIContent("Game Localization Table"), Target); EditorGUILayout.Space(); serializedObject.Update(); { EditorGUILayout.HelpBox("You can edit this language in the Game Localization Table Editor window.", MessageType.Info); EditorGUILayout.Space(); using (new EditorDrawing.BorderBoxScope(new GUIContent("GenCode"), roundedBox: false)) { Properties.Draw("GenerateScriptCodePath", new GUIContent("File Path")); } } serializedObject.ApplyModifiedProperties(); using (new EditorDrawing.BorderBoxScope(new GUIContent("Languages"), roundedBox: false)) { if (Target.Languages.Count > 0) { using (new EditorGUI.DisabledGroupScope(true)) { foreach (var lang in Target.Languages) { string name = lang.LanguageName.Or("Unknown"); EditorGUILayout.ObjectField(new GUIContent(name), lang, typeof(LocalizationLanguage), false); } } } else { EditorGUILayout.HelpBox("There are currently no languages available, open the localization editor and add new languages.", MessageType.Info); } } EditorGUILayout.Space(); EditorGUILayout.BeginHorizontal(); { GUILayout.FlexibleSpace(); { if (GUILayout.Button("Open Localization Editor", GUILayout.Width(180f), GUILayout.Height(25))) { string path = AssetDatabase.GetAssetPath(target); EditorPrefs.SetString("LastSelectedGameLocaizationTable", path); OpenLocalizationEditor(); } if (GUILayout.Button("Gen Code", GUILayout.Width(180f), GUILayout.Height(25))) { LocalizationWindowUtility.GenerateCode(Target); } } GUILayout.FlexibleSpace(); } EditorGUILayout.EndHorizontal(); } [MenuItem("Tools/AlicizaX/Localization/Open Localization Editor")] private static void OpenLocalizationEditor() { EditorWindow window = EditorWindow.GetWindow(false, "Localization Editor", true); window.minSize = new(1000, 500); window.Show(); } [MenuItem("Tools/AlicizaX/Localization/Open Localization ProjectSetting")] public static void OpenSettings() { SettingsService.OpenProjectSettings("Project/Localization Settings"); } [MenuItem("Tools/AlicizaX/Localization/Create Localization Table")] private static void CreateLocalizationTable() { string selectedFolderPath = "Assets"; string defaultFileName = "LocalizationTable.asset"; string defaultPath = Path.Combine(selectedFolderPath, defaultFileName); string path = EditorUtility.SaveFilePanel("Create Localization Table", selectedFolderPath, defaultFileName, "asset"); if (string.IsNullOrEmpty(path)) return; if (!path.StartsWith(Application.dataPath)) { string relativePath = "Assets" + path.Replace(Application.dataPath, ""); if (path.StartsWith(Application.dataPath)) { path = relativePath; } else { EditorUtility.DisplayDialog("Error", "Localization table must be saved within the project's Assets folder!", "OK"); return; } } else { path = "Assets" + path.Replace(Application.dataPath, ""); } if (!path.EndsWith(".asset")) { path += ".asset"; } if (AssetDatabase.LoadMainAssetAtPath(path) != null) { bool overwrite = EditorUtility.DisplayDialog("File Exists", "A file with the same name already exists. Do you want to overwrite it?", "Yes", "No"); if (!overwrite) return; } GameLocaizationTable table = ScriptableObject.CreateInstance(); string directory = Path.GetDirectoryName(path); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } AssetDatabase.CreateAsset(table, path); table.TableSheet = new List(); if (table.Languages == null) { table.Languages = new List(); } IReadOnlyList languageTypes = LocalizationConfiguration.Instance.LanguageTypeNames; for (int i = 0; i < languageTypes.Count; i++) { LocalizationLanguage asset = ScriptableObject.CreateInstance(); asset.name = languageTypes[i]; asset.LanguageName = languageTypes[i]; asset.Strings = new List(); table.Languages.Add(asset); AssetDatabase.AddObjectToAsset(asset, table); } EditorUtility.SetDirty(table); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); Selection.activeObject = table; EditorGUIUtility.PingObject(table); Debug.Log($"Localization table created successfully at: {path}"); } } }