using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; public class MenuUI : MonoBehaviour { private const string GuestTotalGoldKey = "guest_totalGold"; [Header("UI元素")] public Text welcomeText; public Text userInfoText; public Button startGameButton; public Button reviewButton; public Button achievementsButton; public Button logoutButton; [Header("场景名称")] public string gameSceneName = "GameScene"; public string achievementSceneName = "AchievementScene"; public string loginSceneName = "LoginScene"; private void Start() { UpdateUserInfo(); if (startGameButton != null) { startGameButton.onClick.AddListener(OnStartGameClicked); } if (reviewButton != null) { reviewButton.onClick.AddListener(OnReviewClicked); } if (achievementsButton != null) { achievementsButton.onClick.AddListener(OnAchievementsClicked); } if (logoutButton != null) { logoutButton.onClick.AddListener(OnLogoutClicked); } } void UpdateUserInfo() { if (LoginManager.Instance == null) { Debug.LogWarning("LoginManager未找到!"); return; } UserData user = LoginManager.Instance.GetCurrentUser(); if (user != null) { if (welcomeText != null) { welcomeText.text = $"欢迎,{user.username}!"; } if (userInfoText != null) { string info = $"学习能力: {user.studyAbility}\n"; info += $"等级: {user.currentLevel}\n"; info += $"经验: {user.experience}\n"; info += $"金币: {GetDisplayGold()}"; if (LoginManager.Instance.IsGuest()) { info += "\n(游客模式)"; } userInfoText.text = info; } } else { if (LoginManager.Instance.IsGuest()) { int guestLevel = PlayerPrefs.GetInt("guest_playerLevel", 1); int guestExp = PlayerPrefs.GetInt("guest_experience", 0); int guestStudyAbility = PlayerPrefs.GetInt("guest_studyAbility", 1); if (welcomeText != null) { welcomeText.text = "欢迎,游客!"; } if (userInfoText != null) { int guestGold = PlayerPrefs.GetInt(GuestTotalGoldKey, 0); userInfoText.text = $"学习能力: {guestStudyAbility}\n等级: {guestLevel}\n经验: {guestExp}\n金币: {guestGold}\n(游客模式)"; } } else { if (welcomeText != null) { welcomeText.text = "欢迎!"; } if (userInfoText != null) { userInfoText.text = "未登录"; } } } } int GetDisplayGold() { if (LoginManager.Instance != null && LoginManager.Instance.IsGuest()) { return PlayerPrefs.GetInt(GuestTotalGoldKey, 0); } if (AchievementManager.Instance != null) { return AchievementManager.Instance.GetAccumulatedValue(AchievementType.CollectGold); } return 0; } void OnStartGameClicked() { Debug.Log("开始游戏"); if (GameModeManager.Instance != null) { GameModeManager.Instance.SetNormalMode(); } SceneManager.LoadScene(gameSceneName); } void OnReviewClicked() { Debug.Log("开始复习模式"); if (GameModeManager.Instance != null) { GameModeManager.Instance.SetReviewMode(); } SceneManager.LoadScene(gameSceneName); } void OnAchievementsClicked() { Debug.Log("查看成就"); SceneManager.LoadScene(achievementSceneName); } void OnLogoutClicked() { Debug.Log("退出登录"); if (LoginManager.Instance != null) { LoginManager.Instance.Logout(); } else { SceneManager.LoadScene(loginSceneName); } } }