200 lines
6.1 KiB
C#
200 lines
6.1 KiB
C#
|
|
using System.Collections;
|
||
|
|
using System.IO;
|
||
|
|
using Michsky.UI.Reach;
|
||
|
|
using UI.Settings;
|
||
|
|
using UnityEngine;
|
||
|
|
using UnityEngine.SceneManagement;
|
||
|
|
|
||
|
|
namespace UI.Title
|
||
|
|
{
|
||
|
|
public class TitleMenuController : MonoBehaviour
|
||
|
|
{
|
||
|
|
[SerializeField] private bool debugLogs = true;
|
||
|
|
|
||
|
|
[Header("Scene")]
|
||
|
|
[SerializeField] private int nextSceneBuildIndex = 2;
|
||
|
|
|
||
|
|
[Header("Buttons")]
|
||
|
|
[SerializeField] private ButtonManager startOrRestartButton;
|
||
|
|
[SerializeField] private ButtonManager continueButton;
|
||
|
|
[SerializeField] private ButtonManager settingsButton;
|
||
|
|
[SerializeField] private ButtonManager quitButton;
|
||
|
|
|
||
|
|
[Header("Settings Panel")]
|
||
|
|
[SerializeField] private SettingsController settingsController;
|
||
|
|
|
||
|
|
[Header("Labels")]
|
||
|
|
[SerializeField] private string loadingText = "正在加载";
|
||
|
|
[SerializeField] private string startText = "开始游戏";
|
||
|
|
[SerializeField] private string restartText = "重新开始";
|
||
|
|
[SerializeField] private string continueText = "继续游戏";
|
||
|
|
|
||
|
|
private AsyncOperation loadOperation;
|
||
|
|
private bool isWired;
|
||
|
|
private bool hasSave;
|
||
|
|
|
||
|
|
private void Awake()
|
||
|
|
{
|
||
|
|
Cursor.lockState = CursorLockMode.None;
|
||
|
|
Cursor.visible = true;
|
||
|
|
|
||
|
|
if (settingsController == null)
|
||
|
|
{
|
||
|
|
settingsController = FindObjectOfType<SettingsController>(true);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (debugLogs) Debug.Log($"[TitleMenu] Awake. settingsController={(settingsController!=null)} nextSceneBuildIndex={nextSceneBuildIndex}");
|
||
|
|
RefreshSaveState();
|
||
|
|
WireOnce();
|
||
|
|
UpdateButtons();
|
||
|
|
}
|
||
|
|
|
||
|
|
private void Start()
|
||
|
|
{
|
||
|
|
BeginPreload();
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OnEnable()
|
||
|
|
{
|
||
|
|
Cursor.lockState = CursorLockMode.None;
|
||
|
|
Cursor.visible = true;
|
||
|
|
|
||
|
|
RefreshSaveState();
|
||
|
|
UpdateButtons();
|
||
|
|
}
|
||
|
|
|
||
|
|
public void BeginPreload()
|
||
|
|
{
|
||
|
|
if (loadOperation != null) { return; }
|
||
|
|
|
||
|
|
if (nextSceneBuildIndex < 0 || nextSceneBuildIndex >= SceneManager.sceneCountInBuildSettings)
|
||
|
|
{
|
||
|
|
if (debugLogs) Debug.Log($"[TitleMenu] Invalid nextSceneBuildIndex={nextSceneBuildIndex}");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (debugLogs) Debug.Log($"[TitleMenu] BeginPreload buildIndex={nextSceneBuildIndex}");
|
||
|
|
StartCoroutine(PreloadRoutine());
|
||
|
|
}
|
||
|
|
|
||
|
|
private IEnumerator PreloadRoutine()
|
||
|
|
{
|
||
|
|
loadOperation = SceneManager.LoadSceneAsync(nextSceneBuildIndex);
|
||
|
|
if (loadOperation == null) { yield break; }
|
||
|
|
|
||
|
|
loadOperation.allowSceneActivation = false;
|
||
|
|
|
||
|
|
while (loadOperation.progress < 0.9f)
|
||
|
|
{
|
||
|
|
UpdateButtons();
|
||
|
|
yield return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (debugLogs) Debug.Log("[TitleMenu] Preload ready (progress>=0.9).");
|
||
|
|
UpdateButtons();
|
||
|
|
}
|
||
|
|
|
||
|
|
private bool IsPreloadReady()
|
||
|
|
{
|
||
|
|
return loadOperation != null && loadOperation.progress >= 0.9f;
|
||
|
|
}
|
||
|
|
|
||
|
|
private void WireOnce()
|
||
|
|
{
|
||
|
|
if (isWired) { return; }
|
||
|
|
|
||
|
|
if (startOrRestartButton != null) { startOrRestartButton.onClick.AddListener(OnStartOrRestartClicked); }
|
||
|
|
if (continueButton != null) { continueButton.onClick.AddListener(OnContinueClicked); }
|
||
|
|
if (settingsButton != null) { settingsButton.onClick.AddListener(OnSettingsClicked); }
|
||
|
|
if (quitButton != null) { quitButton.onClick.AddListener(OnQuitClicked); }
|
||
|
|
|
||
|
|
isWired = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
private void RefreshSaveState()
|
||
|
|
{
|
||
|
|
string savePath = Path.Combine(Application.persistentDataPath, "savegame.json");
|
||
|
|
hasSave = File.Exists(savePath);
|
||
|
|
}
|
||
|
|
|
||
|
|
private void UpdateButtons()
|
||
|
|
{
|
||
|
|
bool preloadReady = IsPreloadReady();
|
||
|
|
|
||
|
|
if (startOrRestartButton != null)
|
||
|
|
{
|
||
|
|
if (!preloadReady)
|
||
|
|
{
|
||
|
|
startOrRestartButton.SetText(loadingText);
|
||
|
|
startOrRestartButton.Interactable(false);
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
startOrRestartButton.SetText(hasSave ? restartText : startText);
|
||
|
|
startOrRestartButton.Interactable(true);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (continueButton != null)
|
||
|
|
{
|
||
|
|
continueButton.gameObject.SetActive(hasSave);
|
||
|
|
|
||
|
|
if (hasSave)
|
||
|
|
{
|
||
|
|
if (!preloadReady)
|
||
|
|
{
|
||
|
|
continueButton.SetText(loadingText);
|
||
|
|
continueButton.Interactable(false);
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
continueButton.SetText(continueText);
|
||
|
|
continueButton.Interactable(true);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OnStartOrRestartClicked()
|
||
|
|
{
|
||
|
|
if (!IsPreloadReady()) { return; }
|
||
|
|
|
||
|
|
if (hasSave)
|
||
|
|
{
|
||
|
|
string savePath = Path.Combine(Application.persistentDataPath, "savegame.json");
|
||
|
|
if (File.Exists(savePath)) { File.Delete(savePath); }
|
||
|
|
hasSave = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
ActivateLoadedScene();
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OnContinueClicked()
|
||
|
|
{
|
||
|
|
if (!hasSave) { return; }
|
||
|
|
if (!IsPreloadReady()) { return; }
|
||
|
|
ActivateLoadedScene();
|
||
|
|
}
|
||
|
|
|
||
|
|
private void ActivateLoadedScene()
|
||
|
|
{
|
||
|
|
if (loadOperation == null) { return; }
|
||
|
|
loadOperation.allowSceneActivation = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OnSettingsClicked()
|
||
|
|
{
|
||
|
|
if (debugLogs) Debug.Log($"[TitleMenu] Settings clicked. settingsController={(settingsController!=null)}");
|
||
|
|
if (settingsController != null)
|
||
|
|
{
|
||
|
|
settingsController.OpenAtIndex(0);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OnQuitClicked()
|
||
|
|
{
|
||
|
|
Application.Quit();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|