77 lines
2.2 KiB
C#
77 lines
2.2 KiB
C#
using System.IO;
|
|
using System.Linq;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace AibisDream.UI
|
|
{
|
|
public class SavesPanel : MonoBehaviour, IUIPanel
|
|
{
|
|
public bool IsOpen => gameObject.activeSelf;
|
|
public bool IsCloseable => true;
|
|
|
|
private static readonly string SaveFilePath = Application.streamingAssetsPath + "/SaveFiles";
|
|
|
|
#region 索引
|
|
public Button buttonPrefab;
|
|
public Transform buttonParent;
|
|
#endregion
|
|
|
|
#region 打开和关闭
|
|
|
|
public void Show()
|
|
{
|
|
InitLevelBtn();
|
|
gameObject.SetActive(true);
|
|
RegisterButton();
|
|
}
|
|
|
|
public void Hide()
|
|
{
|
|
gameObject.SetActive(false);
|
|
UnRegisterButton();
|
|
}
|
|
|
|
private void RegisterButton()
|
|
{
|
|
var returnBtn = transform.Find("Return").GetComponent<Button>();
|
|
returnBtn.onClick.AddListener(() => UIManager.Instance.HidePanel<SavesPanel>());
|
|
}
|
|
|
|
private void UnRegisterButton()
|
|
{
|
|
var returnBtn = transform.Find("Return").GetComponent<Button>();
|
|
returnBtn.onClick.RemoveAllListeners();
|
|
}
|
|
|
|
#endregion
|
|
|
|
private void InitLevelBtn()
|
|
{
|
|
// 清空旧按钮
|
|
foreach (Transform child in buttonParent)
|
|
{
|
|
Destroy(child.gameObject);
|
|
}
|
|
|
|
// 加载存档文件
|
|
string[] saveFiles = Directory.GetFiles(SaveFilePath, "*.json");
|
|
var orderedSaveFiles = saveFiles.OrderByDescending(item => item).ToArray();
|
|
foreach (var saveFile in orderedSaveFiles)
|
|
{
|
|
var button = Instantiate(buttonPrefab, buttonParent);
|
|
button.GetComponentInChildren<TMP_Text>().text = Path.GetFileNameWithoutExtension(saveFile);
|
|
button.onClick.AddListener(() => OnSaveFileSelected(saveFile));
|
|
}
|
|
}
|
|
|
|
private void OnSaveFileSelected(string saveFileName)
|
|
{
|
|
// 隐藏UI
|
|
gameObject.SetActive(false);
|
|
// 开始游戏
|
|
GameManager.Instance.StartWithSaveFile(saveFileName);
|
|
}
|
|
}
|
|
} |