UI系统优化
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
"BaseGames.Core",
|
||||
"BaseGames.Core.Save",
|
||||
"BaseGames.Core.Events",
|
||||
"BaseGames.Localization",
|
||||
"Unity.TextMeshPro"
|
||||
],
|
||||
"autoReferenced": true,
|
||||
|
||||
@@ -56,10 +56,13 @@ namespace BaseGames.World.Map
|
||||
private List<Image> _pinImages = new();
|
||||
private List<Image> _exitImages = new();
|
||||
private string _highlightedRoomId;
|
||||
private IMapService _mapSvc; // 面板活跃期间缓存,避免高频 ServiceLocator 查询
|
||||
private readonly CompositeDisposable _subs = new();
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
_mapSvc = ServiceLocator.GetOrDefault<IMapService>();
|
||||
|
||||
// 首次打开时建立格子;后续打开只刷新探索状态,跳过重复 Instantiate
|
||||
if (_cells.Count == 0)
|
||||
BuildGrid();
|
||||
@@ -75,6 +78,7 @@ namespace BaseGames.World.Map
|
||||
private void OnDisable()
|
||||
{
|
||||
_subs.Clear();
|
||||
_mapSvc = null;
|
||||
HideTooltip();
|
||||
}
|
||||
|
||||
@@ -95,11 +99,10 @@ namespace BaseGames.World.Map
|
||||
// 面板重新打开时同步关闭期间积累的探索进度
|
||||
private void RefreshAllCells()
|
||||
{
|
||||
var svc = ServiceLocator.GetOrDefault<IMapService>();
|
||||
foreach (var (roomId, cell) in _cells)
|
||||
{
|
||||
if (cell == null) continue;
|
||||
cell.SetVisibility(GetVisibility(svc, roomId));
|
||||
cell.SetVisibility(GetVisibility(_mapSvc, roomId));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,12 +111,11 @@ namespace BaseGames.World.Map
|
||||
private void BuildGrid()
|
||||
{
|
||||
if (_database?.AllRooms == null) return;
|
||||
var svc = ServiceLocator.GetOrDefault<IMapService>();
|
||||
foreach (var room in _database.AllRooms)
|
||||
{
|
||||
if (room == null) continue;
|
||||
var cell = Instantiate(_cellPrefab, _roomContainer);
|
||||
cell.Setup(room, GetVisibility(svc, room.RoomId), ChooseIcon(room),
|
||||
cell.Setup(room, GetVisibility(_mapSvc, room.RoomId), ChooseIcon(room),
|
||||
ShowTooltip, HideTooltip);
|
||||
_cells[room.RoomId] = cell;
|
||||
}
|
||||
@@ -149,9 +151,8 @@ namespace BaseGames.World.Map
|
||||
|
||||
private void OnMapUpdated(string roomId)
|
||||
{
|
||||
var svc = ServiceLocator.GetOrDefault<IMapService>();
|
||||
if (_cells.TryGetValue(roomId, out var cell))
|
||||
cell.SetVisibility(GetVisibility(svc, roomId));
|
||||
cell.SetVisibility(GetVisibility(_mapSvc, roomId));
|
||||
}
|
||||
|
||||
// ── 玩家位置图标 ──────────────────────────────────────────────────────
|
||||
|
||||
@@ -4,6 +4,7 @@ using UnityEngine;
|
||||
using TMPro;
|
||||
using BaseGames.Core;
|
||||
using BaseGames.Core.Events;
|
||||
using BaseGames.Localization;
|
||||
|
||||
namespace BaseGames.World.Map
|
||||
{
|
||||
@@ -90,7 +91,8 @@ namespace BaseGames.World.Map
|
||||
{
|
||||
if (_regionNames != null)
|
||||
foreach (var e in _regionNames)
|
||||
if (e.RegionId == regionId) return e.DisplayName;
|
||||
if (e.RegionId == regionId)
|
||||
return e.GetDisplayName();
|
||||
return regionId;
|
||||
}
|
||||
}
|
||||
@@ -98,7 +100,26 @@ namespace BaseGames.World.Map
|
||||
[Serializable]
|
||||
public struct RegionNameEntry
|
||||
{
|
||||
[Tooltip("场景/区域 ID,与 EVT_RegionChanged 事件传递的字符串匹配。")]
|
||||
public string RegionId;
|
||||
|
||||
[Tooltip("本地化 Key(如 REGION_CITY_NAME)。设置后运行时通过 LocalizationManager 自动解析。")]
|
||||
public string LocKey;
|
||||
|
||||
[Tooltip("直接显示名(LocKey 为空时使用)。建议优先配置 LocKey,仅在开发或单语言项目中直接善用。")]
|
||||
public string DisplayName;
|
||||
|
||||
/// <summary>返回最终显示名:优先读 LocKey,其次 DisplayName,最后回退到 RegionId。</summary>
|
||||
public string GetDisplayName()
|
||||
{
|
||||
if (!string.IsNullOrEmpty(LocKey))
|
||||
{
|
||||
string localized = LocalizationManager.Get(LocKey, LocalizationTable.UI);
|
||||
// LocalizationManager 在未找到 Key 时返回 Key 本身,判断是否是真正翻译结果
|
||||
if (!string.IsNullOrEmpty(localized) && localized != LocKey)
|
||||
return localized;
|
||||
}
|
||||
return !string.IsNullOrEmpty(DisplayName) ? DisplayName : RegionId;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user