存档功能
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace AibisDream
|
||||
{
|
||||
public class SaveFileUIController : MonoBehaviour
|
||||
{
|
||||
private static readonly string SaveFilePath = Application.streamingAssetsPath + "/SaveFiles";
|
||||
|
||||
#region 索引
|
||||
public Button buttonPrefab;
|
||||
public Transform buttonParent;
|
||||
public Button returnButton;
|
||||
#endregion
|
||||
|
||||
private void Start()
|
||||
{
|
||||
returnButton.onClick.AddListener(HideSaveFileUI);
|
||||
}
|
||||
|
||||
public void ShowSaveFileUI()
|
||||
{
|
||||
// 清空旧按钮
|
||||
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));
|
||||
}
|
||||
|
||||
gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
private void OnSaveFileSelected(string saveFileName)
|
||||
{
|
||||
// 隐藏UI
|
||||
gameObject.SetActive(false);
|
||||
// 开始游戏
|
||||
GameLoopManager.Instance.StartWithSaveFile(saveFileName);
|
||||
}
|
||||
|
||||
public void HideSaveFileUI()
|
||||
{
|
||||
gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a351e509c8dd5944f9e6406f59ea5c0b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user