修改创建

This commit is contained in:
陈思海 2025-10-15 11:05:38 +08:00
parent 522c8802ae
commit e55bf61b9f

View File

@ -11,7 +11,6 @@ namespace AlicizaX.Localization.Editor
[CustomEditor(typeof(GameLocaizationTable))]
internal class GameLocaizationTableEditor : InspectorEditor<GameLocaizationTable>
{
[OnOpenAsset]
public static bool OnOpenAsset(int instanceId, int line)
@ -107,25 +106,71 @@ namespace AlicizaX.Localization.Editor
[MenuItem("Tools/AlicizaX/Localization/Create Localization Table")]
private static void CreateLocalizationTable()
{
string path = "Assets/Localization/";
string fileName = "LocalizationTable.asset";
string finalPath = Path.Combine(path, fileName);
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<GameLocaizationTable>();
if (!Directory.Exists(path))
string directory = Path.GetDirectoryName(path);
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(path);
Directory.CreateDirectory(directory);
}
AssetDatabase.CreateAsset(table, finalPath);
EditorUtility.SetDirty(table);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
AssetDatabase.CreateAsset(table, path);
table.TableSheet = new List<GameLocaizationTable.TableData>();
if (table.Languages == null)
{
table.Languages = new List<LocalizationLanguage>();
}
IReadOnlyList<string> languageTypes = LocalizationConfiguration.Instance.LanguageTypeNames;
for (int i = 0; i < languageTypes.Count; i++)
{
LocalizationLanguage asset = ScriptableObject.CreateInstance<LocalizationLanguage>();
@ -134,11 +179,17 @@ namespace AlicizaX.Localization.Editor
asset.Strings = new List<LocalizationLanguage.LocalizationString>();
table.Languages.Add(asset);
AssetDatabase.AddObjectToAsset(asset, table);
EditorUtility.SetDirty(asset);
}
EditorUtility.SetDirty(table);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
Selection.activeObject = table;
EditorGUIUtility.PingObject(table);
Debug.Log($"Localization table created successfully at: {path}");
}
}
}