feat: 更新存档管理,添加异步存档槽管理功能,优化存档验证逻辑
This commit is contained in:
@@ -3,6 +3,7 @@ using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using TMPro;
|
||||
using BaseGames.Core;
|
||||
using BaseGames.Core.Events;
|
||||
using BaseGames.Core.Save;
|
||||
|
||||
@@ -14,7 +15,6 @@ namespace BaseGames.UI.Menus
|
||||
public class SaveSlotController : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private SaveSlotUI[] _slotUIs; // 存档槽 UI(数量由 Inspector 决定)
|
||||
[SerializeField] private GameSaveManager _saveManager;
|
||||
|
||||
[Header("Event Channels")]
|
||||
[SerializeField] private IntEventChannelSO _onSlotConfirmed; // 携带槽索引,供 GameManager 监听
|
||||
@@ -22,7 +22,6 @@ namespace BaseGames.UI.Menus
|
||||
private void OnEnable()
|
||||
{
|
||||
var task = RefreshAsync();
|
||||
// 捕获 async Task 异常,避免 async void 吞掉未处理异常
|
||||
task.ContinueWith(t =>
|
||||
{
|
||||
if (t.IsFaulted)
|
||||
@@ -32,11 +31,12 @@ namespace BaseGames.UI.Menus
|
||||
|
||||
private async Task RefreshAsync()
|
||||
{
|
||||
if (_saveManager == null) return;
|
||||
var svc = ServiceLocator.GetOrDefault<ISaveService>();
|
||||
if (svc == null) return;
|
||||
for (int i = 0; i < _slotUIs.Length; i++)
|
||||
{
|
||||
if (_slotUIs[i] == null) continue;
|
||||
var summary = await _saveManager.GetSlotSummaryAsync(i);
|
||||
var summary = await svc.GetSlotSummaryAsync(i);
|
||||
_slotUIs[i].Refresh(summary);
|
||||
}
|
||||
}
|
||||
@@ -44,16 +44,19 @@ namespace BaseGames.UI.Menus
|
||||
/// <summary>选中指定槽位(新局或继续)。由 SaveSlotUI 内部按钮调用。</summary>
|
||||
public void OnSlotSelected(int slotIndex)
|
||||
{
|
||||
if (slotIndex < 0 || slotIndex >= _slotUIs.Length || _saveManager == null) return;
|
||||
if (slotIndex < 0 || slotIndex >= _slotUIs.Length) return;
|
||||
_ = SelectSlotAsync(slotIndex);
|
||||
}
|
||||
|
||||
private async Task SelectSlotAsync(int slotIndex)
|
||||
{
|
||||
if (_saveManager.SlotExists(slotIndex))
|
||||
await _saveManager.LoadAsync(slotIndex);
|
||||
var svc = ServiceLocator.GetOrDefault<ISaveService>();
|
||||
if (svc == null) return;
|
||||
|
||||
if (svc.HasSave(slotIndex))
|
||||
await svc.LoadAsync(slotIndex);
|
||||
else
|
||||
_saveManager.CreateSlot(slotIndex);
|
||||
svc.CreateSlot(slotIndex);
|
||||
|
||||
_onSlotConfirmed?.Raise(slotIndex);
|
||||
}
|
||||
@@ -61,13 +64,15 @@ namespace BaseGames.UI.Menus
|
||||
/// <summary>删除指定槽位存档并刷新 UI。由 SaveSlotUI 内部按钮调用。</summary>
|
||||
public void OnSlotDeleteRequested(int slotIndex)
|
||||
{
|
||||
if (slotIndex < 0 || slotIndex >= _slotUIs.Length || _saveManager == null) return;
|
||||
if (slotIndex < 0 || slotIndex >= _slotUIs.Length) return;
|
||||
_ = DeleteAndRefreshAsync(slotIndex);
|
||||
}
|
||||
|
||||
private async Task DeleteAndRefreshAsync(int slotIndex)
|
||||
{
|
||||
await _saveManager.DeleteSlotAsync(slotIndex);
|
||||
var svc = ServiceLocator.GetOrDefault<ISaveService>();
|
||||
if (svc == null) return;
|
||||
await svc.DeleteSlotAsync(slotIndex);
|
||||
await RefreshAsync();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user