暂时增加初版编辑器及时预览多语言切换

This commit is contained in:
陈思海 2025-12-10 17:37:20 +08:00
parent 6061b51dbd
commit 5084130abe
4 changed files with 65 additions and 1 deletions

View File

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Linq;
using AlicizaX.Localization;
using AlicizaX.Localization.Editor;
using TMPro;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
@ -172,6 +173,7 @@ namespace UnityEngine.UI
localizationID.intValue = 0;
m_localizationKey.stringValue = string.Empty;
selectedSelectionIndex = 0;
(target as TextMeshProUGUI).text = string.Empty;
}
else
{
@ -186,7 +188,6 @@ namespace UnityEngine.UI
}
}
}
serializedObject.ApplyModifiedProperties();
}
}

View File

@ -0,0 +1,53 @@
#if UNITY_EDITOR
using System.Collections.Generic;
using System.Linq;
using AlicizaX.Localization;
using AlicizaX.Localization.Runtime;
using UnityEditor;
namespace UnityEngine.UI
{
internal static class LocalizationRefreshHelper
{
private static Dictionary<string, string> previewLabelDic = new();
internal static string GetPreviewLabel(string key)
{
Init();
if (previewLabelDic.ContainsKey(key))
{
return previewLabelDic[key];
}
return string.Empty;
}
static void Init()
{
previewLabelDic.Clear();
List<GameLocaizationTable> allTables = new();
string[] guids = AssetDatabase.FindAssets("t:GameLocaizationTable");
foreach (string guid in guids)
{
string assetPath = AssetDatabase.GUIDToAssetPath(guid);
GameLocaizationTable table = AssetDatabase.LoadAssetAtPath<GameLocaizationTable>(assetPath);
if (table != null)
{
allTables.Add(table);
}
}
string language = EditorPrefs.GetString(LocalizationComponent.PrefsKey, "None");
var localizationLanguage = allTables.Select(e => e.Languages.Find(t => t.LanguageName == language)).ToList();
foreach (var localization in localizationLanguage)
{
foreach (var item in localization.Strings)
{
previewLabelDic.TryAdd(item.Key, item.Value);
}
}
}
}
}
#endif

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 169b0bd7883944ed897310ddf0085517
timeCreated: 1765358242

View File

@ -9,6 +9,13 @@ namespace UnityEngine.UI
[SerializeField] private int m_localizationID;
[SerializeField] private string m_localizationKey = "";
#if UNITY_EDITOR
protected override void OnValidate()
{
base.OnValidate();
if (Application.isEditor) text = LocalizationRefreshHelper.GetPreviewLabel(m_localizationKey);
}
#endif
protected override void Start()
{
base.Start();