MenuUI.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.SceneManagement;
  4. public class MenuUI : MonoBehaviour
  5. {
  6. private const string GuestTotalGoldKey = "guest_totalGold";
  7. [Header("UI元素")]
  8. public Text welcomeText;
  9. public Text userInfoText;
  10. public Button startGameButton;
  11. public Button reviewButton;
  12. public Button achievementsButton;
  13. public Button logoutButton;
  14. [Header("场景名称")]
  15. public string gameSceneName = "GameScene";
  16. public string achievementSceneName = "AchievementScene";
  17. public string loginSceneName = "LoginScene";
  18. private void Start()
  19. {
  20. UpdateUserInfo();
  21. if (startGameButton != null)
  22. {
  23. startGameButton.onClick.AddListener(OnStartGameClicked);
  24. }
  25. if (reviewButton != null)
  26. {
  27. reviewButton.onClick.AddListener(OnReviewClicked);
  28. }
  29. if (achievementsButton != null)
  30. {
  31. achievementsButton.onClick.AddListener(OnAchievementsClicked);
  32. }
  33. if (logoutButton != null)
  34. {
  35. logoutButton.onClick.AddListener(OnLogoutClicked);
  36. }
  37. }
  38. void UpdateUserInfo()
  39. {
  40. if (LoginManager.Instance == null)
  41. {
  42. Debug.LogWarning("LoginManager未找到!");
  43. return;
  44. }
  45. UserData user = LoginManager.Instance.GetCurrentUser();
  46. if (user != null)
  47. {
  48. if (welcomeText != null)
  49. {
  50. welcomeText.text = $"欢迎,{user.username}!";
  51. }
  52. if (userInfoText != null)
  53. {
  54. string info = $"学习能力: {user.studyAbility}\n";
  55. info += $"等级: {user.currentLevel}\n";
  56. info += $"经验: {user.experience}\n";
  57. info += $"金币: {GetDisplayGold()}";
  58. if (LoginManager.Instance.IsGuest())
  59. {
  60. info += "\n(游客模式)";
  61. }
  62. userInfoText.text = info;
  63. }
  64. }
  65. else
  66. {
  67. if (LoginManager.Instance.IsGuest())
  68. {
  69. int guestLevel = PlayerPrefs.GetInt("guest_playerLevel", 1);
  70. int guestExp = PlayerPrefs.GetInt("guest_experience", 0);
  71. int guestStudyAbility = PlayerPrefs.GetInt("guest_studyAbility", 1);
  72. if (welcomeText != null)
  73. {
  74. welcomeText.text = "欢迎,游客!";
  75. }
  76. if (userInfoText != null)
  77. {
  78. int guestGold = PlayerPrefs.GetInt(GuestTotalGoldKey, 0);
  79. userInfoText.text = $"学习能力: {guestStudyAbility}\n等级: {guestLevel}\n经验: {guestExp}\n金币: {guestGold}\n(游客模式)";
  80. }
  81. }
  82. else
  83. {
  84. if (welcomeText != null)
  85. {
  86. welcomeText.text = "欢迎!";
  87. }
  88. if (userInfoText != null)
  89. {
  90. userInfoText.text = "未登录";
  91. }
  92. }
  93. }
  94. }
  95. int GetDisplayGold()
  96. {
  97. if (LoginManager.Instance != null && LoginManager.Instance.IsGuest())
  98. {
  99. return PlayerPrefs.GetInt(GuestTotalGoldKey, 0);
  100. }
  101. if (AchievementManager.Instance != null)
  102. {
  103. return AchievementManager.Instance.GetAccumulatedValue(AchievementType.CollectGold);
  104. }
  105. return 0;
  106. }
  107. void OnStartGameClicked()
  108. {
  109. Debug.Log("开始游戏");
  110. if (GameModeManager.Instance != null)
  111. {
  112. GameModeManager.Instance.SetNormalMode();
  113. }
  114. SceneManager.LoadScene(gameSceneName);
  115. }
  116. void OnReviewClicked()
  117. {
  118. Debug.Log("开始复习模式");
  119. if (GameModeManager.Instance != null)
  120. {
  121. GameModeManager.Instance.SetReviewMode();
  122. }
  123. SceneManager.LoadScene(gameSceneName);
  124. }
  125. void OnAchievementsClicked()
  126. {
  127. Debug.Log("查看成就");
  128. SceneManager.LoadScene(achievementSceneName);
  129. }
  130. void OnLogoutClicked()
  131. {
  132. Debug.Log("退出登录");
  133. if (LoginManager.Instance != null)
  134. {
  135. LoginManager.Instance.Logout();
  136. }
  137. else
  138. {
  139. SceneManager.LoadScene(loginSceneName);
  140. }
  141. }
  142. }