| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- 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);
- }
- }
- }
|