修改
This commit is contained in:
parent
97c7b8cd36
commit
dd0cbf21aa
@ -1,10 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Serialization;
|
||||
|
||||
namespace AlicizaX.Localization.Editor
|
||||
{
|
||||
[AlicizaX.Editor.Setting.FilePath("ProjectSettings/LocalizationConfiguration.asset")]
|
||||
public class LocalizationConfiguration : AlicizaX.Editor.Setting.ScriptableSingleton<LocalizationConfiguration>
|
||||
public class LocalizationConfiguration : AlicizaX.Editor.Setting.ScriptableSingleton<LocalizationConfiguration>
|
||||
{
|
||||
[SerializeField] private List<string> LanguageTypes = new List<string>()
|
||||
{
|
||||
@ -29,6 +31,7 @@ namespace AlicizaX.Localization.Editor
|
||||
"Russian",
|
||||
};
|
||||
|
||||
[SerializeField] private string _genLangaugeTypePath = "Assets/LanguageTypes.cs";
|
||||
|
||||
public IReadOnlyList<string> LanguageTypeNames => LanguageTypes;
|
||||
}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
|
||||
using System.Text;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
@ -11,8 +11,11 @@ namespace AlicizaX.Localization.Editor
|
||||
private SerializedObject _serializedObject;
|
||||
private SerializedProperty _languageTypes;
|
||||
private ReorderableList _languageList;
|
||||
private SerializedProperty _genLangaugeTypePath;
|
||||
|
||||
public LocalizationSettingsProvider() : base("Project/Localization Settings", SettingsScope.Project) { }
|
||||
public LocalizationSettingsProvider() : base("Project/Localization Settings", SettingsScope.Project)
|
||||
{
|
||||
}
|
||||
|
||||
public override void OnActivate(string searchContext, VisualElement rootElement)
|
||||
{
|
||||
@ -25,18 +28,15 @@ namespace AlicizaX.Localization.Editor
|
||||
_serializedObject?.Dispose();
|
||||
_serializedObject = new SerializedObject(setting);
|
||||
_languageTypes = _serializedObject.FindProperty("LanguageTypes");
|
||||
|
||||
_genLangaugeTypePath = _serializedObject.FindProperty("_genLangaugeTypePath");
|
||||
// 自定义 ReorderableList
|
||||
_languageList = new ReorderableList(_serializedObject, _languageTypes,
|
||||
draggable: false, // 禁止拖拽
|
||||
draggable: false, // 禁止拖拽
|
||||
displayHeader: true,
|
||||
displayAddButton: true,
|
||||
displayRemoveButton: true);
|
||||
|
||||
_languageList.drawHeaderCallback = rect =>
|
||||
{
|
||||
EditorGUI.LabelField(rect, "Language Types");
|
||||
};
|
||||
_languageList.drawHeaderCallback = rect => { EditorGUI.LabelField(rect, "Language Types"); };
|
||||
|
||||
_languageList.drawElementCallback = (rect, index, isActive, isFocused) =>
|
||||
{
|
||||
@ -59,8 +59,12 @@ namespace AlicizaX.Localization.Editor
|
||||
|
||||
_serializedObject.Update();
|
||||
EditorGUI.BeginChangeCheck();
|
||||
EditorGUILayout.PropertyField(_genLangaugeTypePath);
|
||||
if (GUILayout.Button("Generate Language Types"))
|
||||
{
|
||||
RegenerateLanguageTypes();
|
||||
}
|
||||
|
||||
// 使用 ReorderableList 绘制
|
||||
_languageList.DoLayoutList();
|
||||
|
||||
if (EditorGUI.EndChangeCheck())
|
||||
@ -70,6 +74,59 @@ namespace AlicizaX.Localization.Editor
|
||||
}
|
||||
}
|
||||
|
||||
private void RegenerateLanguageTypes()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
sb.AppendLine("using System.Collections.Generic;");
|
||||
sb.AppendLine("");
|
||||
sb.AppendLine("/// <summary>");
|
||||
sb.AppendLine("/// AutoGenerate");
|
||||
sb.AppendLine("/// </summary>");
|
||||
sb.AppendLine("public static class LanguageTypes");
|
||||
sb.AppendLine("{");
|
||||
sb.AppendLine("\tpublic static readonly IReadOnlyList<string> Types = new List<string>");
|
||||
sb.AppendLine("\t{");
|
||||
for (int i = 0; i < LocalizationConfiguration.Instance.LanguageTypeNames.Count; i++)
|
||||
{
|
||||
sb.AppendLine($"\t\t\"{LocalizationConfiguration.Instance.LanguageTypeNames[i]}\",");
|
||||
}
|
||||
|
||||
sb.AppendLine("\t};");
|
||||
|
||||
sb.AppendLine("");
|
||||
|
||||
sb.AppendLine("\tpublic static string IndexToString(int index)");
|
||||
sb.AppendLine("\t{");
|
||||
sb.AppendLine("\t\tif (index < 0 || index >= Types.Count) return \"Unknown\";");
|
||||
sb.AppendLine("\t\treturn Types[index];");
|
||||
sb.AppendLine("\t}");
|
||||
|
||||
sb.AppendLine("");
|
||||
|
||||
sb.AppendLine("\tpublic static int StringToIndex(string s)");
|
||||
sb.AppendLine("\t{");
|
||||
sb.AppendLine("\t\tint index = -1;");
|
||||
sb.AppendLine("\t\tfor (int i = 0; i < Types.Count; i++)");
|
||||
sb.AppendLine("\t\t{");
|
||||
sb.AppendLine("\t\t\tif (Types[i] == s)");
|
||||
sb.AppendLine("\t\t\t{");
|
||||
sb.AppendLine("\t\t\t\tindex = i;");
|
||||
sb.AppendLine("\t\t\t\tbreak;");
|
||||
sb.AppendLine("\t\t\t}");
|
||||
|
||||
sb.AppendLine("\t\t}");
|
||||
sb.AppendLine("");
|
||||
sb.AppendLine("\t\treturn index;");
|
||||
sb.AppendLine("\t}");
|
||||
|
||||
sb.AppendLine("");
|
||||
|
||||
sb.AppendLine("}");
|
||||
System.IO.File.WriteAllText(_genLangaugeTypePath.stringValue, sb.ToString());
|
||||
AssetDatabase.Refresh();
|
||||
}
|
||||
|
||||
public override void OnDeactivate()
|
||||
{
|
||||
base.OnDeactivate();
|
||||
@ -85,6 +142,7 @@ namespace AlicizaX.Localization.Editor
|
||||
{
|
||||
s_provider = new LocalizationSettingsProvider();
|
||||
}
|
||||
|
||||
return s_provider;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user