LoginView.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using UnityEngine;
  2. using FairyGUI;
  3. using UI.Login;
  4. using ET;
  5. using GFGGame.Launcher;
  6. using System;
  7. namespace GFGGame
  8. {
  9. public class LoginView : BaseView
  10. {
  11. private UI_LoginUI _ui;
  12. private GameObject _sceneObject;
  13. private bool autoLogined;
  14. public override void Dispose()
  15. {
  16. if (_sceneObject != null)
  17. {
  18. GameObject.DestroyImmediate(_sceneObject);
  19. _sceneObject = null;
  20. }
  21. if (_ui != null)
  22. {
  23. _ui.Dispose();
  24. _ui = null;
  25. }
  26. base.Dispose();
  27. }
  28. protected override void Init()
  29. {
  30. base.Init();
  31. packageName = UI_LoginUI.PACKAGE_NAME;
  32. _ui = UI_LoginUI.Create();
  33. viewCom = _ui.target;
  34. isfullScreen = true;
  35. }
  36. protected override void OnInit()
  37. {
  38. base.OnInit();
  39. _ui.m_txtVersion.text = GameGlobal.version;
  40. _ui.m_btnNotice.onClick.Add(OnClickBtnNotice);
  41. _ui.m_btnStart.onClick.Add(OnClickBtnStart);
  42. _ui.m_btnAge.onClick.Add(OnClickBtnAge);
  43. _ui.m_btnChange.visible = false;
  44. _ui.m_btnChange.onClick.Add(OnBtnChangeClick);
  45. _ui.m_loaEventa.onClick.Add(() => { FullScreenTextController.Show("event:a"); });
  46. _ui.m_loaEventb.onClick.Add(() => { FullScreenTextController.Show("event:b"); });
  47. _ui.m_loaEventc.onClick.Add(() => { FullScreenTextController.Show("event:c"); });
  48. _ui.m_btnAgree.onClick.Add(OnBtnAgreeCklick);
  49. }
  50. protected override void AddEventListener()
  51. {
  52. base.AddEventListener();
  53. EventAgent.AddEventListener(ConstMessage.SERVER_CHANGE, OnSeverChangeListener);
  54. }
  55. protected override void OnShown()
  56. {
  57. base.OnShown();
  58. _ui.m_btnStart.visible = false;
  59. _ui.m_btnStart.text = "登录中...";
  60. MusicManager.Instance.PlayCroutine(ResPathUtil.GetMusicPath(ConstMusicName.DEFAULT));
  61. if (_sceneObject == null)
  62. {
  63. PrefabManager.Instance.InstantiateAsync(
  64. ResPathUtil.GetLoginResPath("LoginSkin1/SceneLogin"),
  65. (go) =>
  66. {
  67. _sceneObject = go;
  68. if (_sceneObject != null)
  69. {
  70. // 场景对象加载完成后的后续初始化
  71. ContinueAfterSceneLoad();
  72. }
  73. else
  74. {
  75. Debug.LogError("Failed to instantiate login scene");
  76. }
  77. });
  78. }
  79. else
  80. {
  81. // 如果场景对象已存在,直接继续初始化
  82. ContinueAfterSceneLoad();
  83. }
  84. }
  85. private void ContinueAfterSceneLoad()
  86. {
  87. ServerInfosComponent serverInfosComponent = GameGlobal.zoneScene.GetComponent<ServerInfosComponent>();
  88. ServerInfo recentlyServerInfo = serverInfosComponent.recentlyServerInfo;
  89. UpdateServer(recentlyServerInfo);
  90. _ui.m_btnAgree.selected = LocalCache.GetBool(GameConst.LAST_LOGIN_IS_VISITOR_KEY, false);
  91. _ui.m_btnAgree.selected = true;
  92. if (GameConfig.openTime > TimeHelper.ClientNow())
  93. {
  94. var date = DateTimeUtil.LongTimeStampToDateTime(GameConfig.openTime);
  95. string minuteText = "";
  96. if (date.Minute > 0)
  97. {
  98. minuteText = date.Minute + "分";
  99. }
  100. _ui.m_btnStart.text = "";
  101. AlertUI.Show($" 亲爱的研究员,万世镜将于{date.Year}年{date.Month}月{date.Day}日{date.Hour}点{minuteText}开放,敬请期待!")
  102. .SetLeftButton(true, "好的", (obj) => { Application.Quit(); });
  103. return;
  104. }
  105. if (!autoLogined)
  106. {
  107. autoLogined = true;
  108. TryLogin();
  109. }
  110. }
  111. protected override void OnHide()
  112. {
  113. autoLogined = false;
  114. if (_sceneObject != null)
  115. {
  116. PrefabManager.Instance.Restore(_sceneObject);
  117. _sceneObject = null;
  118. }
  119. base.OnHide();
  120. }
  121. protected override void RemoveEventListener()
  122. {
  123. base.RemoveEventListener();
  124. EventAgent.RemoveEventListener(ConstMessage.SERVER_CHANGE, OnSeverChangeListener);
  125. }
  126. private void UpdateServer(ServerInfo info)
  127. {
  128. ServerInfosComponent serverInfosComponent = GameGlobal.zoneScene.GetComponent<ServerInfosComponent>();
  129. if (serverInfosComponent.ServerInfoList.Count == 0)
  130. {
  131. _ui.m_btnChange.visible = false;
  132. return;
  133. }
  134. _ui.m_btnChange.visible = serverInfosComponent.ServerInfoList.Count > 1;
  135. _ui.m_btnChange.title =
  136. string.Format("{0}区 {1}", NumberUtil.GetChiniseNumberText((int)info.Id), info.ServerName);
  137. LogServerHelperHttp.SendNodeLog((int)LogNode.ShowSelectServer);
  138. }
  139. private void OnSeverChangeListener(EventContext context)
  140. {
  141. ServerInfosComponent serverInfosComponent = GameGlobal.zoneScene.GetComponent<ServerInfosComponent>();
  142. UpdateServer(serverInfosComponent.ServerInfoList[serverInfosComponent.CurrentServerId]);
  143. }
  144. private void OnBtnChangeClick()
  145. {
  146. ViewManager.Show<ServerListView>();
  147. }
  148. private void OnClickBtnNotice()
  149. {
  150. if (NoticeDataManager.Instance.LastNoticeInfo == null ||
  151. NoticeDataManager.Instance.LastNoticeInfo.noticeId == 0)
  152. {
  153. PromptController.Instance.ShowFloatTextPrompt("暂无公告发布");
  154. return;
  155. }
  156. ViewManager.Show<SystemNoticeView>(new object[]
  157. { NoticeDataManager.Instance.LastNoticeInfo.title, NoticeDataManager.Instance.LastNoticeInfo.content });
  158. }
  159. private void OnClickBtnStart()
  160. {
  161. LoginController.GetRoles().Coroutine();
  162. }
  163. private void OnClickBtnAge()
  164. {
  165. ViewManager.Show<SystemNoticeView>(new object[] { LoginController.ageTipsTitle, LoginController.ageTips });
  166. }
  167. private void TryLogin()
  168. {
  169. var url = LauncherConfig.cfgUrl.Replace("{cfgName}", LauncherConfig.cfgName);
  170. url = url + "?t=" + DateTime.Now.Ticks;
  171. HttpTool.Instance.Get(url, json =>
  172. {
  173. LauncherConfig.InitPlatform(json);
  174. if (LauncherConfig.serverStatus == 1)
  175. {
  176. if (string.IsNullOrEmpty(LauncherConfig.statusPrompt))
  177. {
  178. _ui.m_btnStart.text = "维护中";
  179. LauncherConfig.statusPrompt = "游戏正在更新维护中,请稍后再试。";
  180. }
  181. AlertSystem.Show(LauncherConfig.statusPrompt)
  182. .SetLeftButton(true, "知道了", (data) => { Application.Quit(); });
  183. return;
  184. }
  185. if (!_ui.m_btnAgree.selected)
  186. {
  187. PromptController.Instance.ShowFloatTextPrompt("请仔细阅读并同意游戏用户协议、隐私保护指引、儿童隐私政策");
  188. return;
  189. }
  190. LoginHelper.H5Login(GameGlobal.zoneScene, GameConfig.LoginAddress,
  191. error =>
  192. {
  193. _ui.m_btnStart.visible = true;
  194. _ui.m_btnStart.text = "开始游戏";
  195. }).Coroutine();
  196. });
  197. }
  198. private void OnBtnAgreeCklick()
  199. {
  200. LocalCache.SetBool(LauncherConfig.LAST_LOGIN_IS_AGREE_KEY, _ui.m_btnAgree.selected);
  201. }
  202. }
  203. }