AppStartInitFinish_CreateLoginUI.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using Cysharp.Threading.Tasks;
  2. using FairyGUI;
  3. using Sirenix.Utilities;
  4. using System;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. namespace ET.Client
  8. {
  9. [Event(SceneType.Client)]
  10. public class AppStartInitFinish_CreateLoginUI : AEvent<EventType.AppStartInitFinish>
  11. {
  12. private string mIP;
  13. private int mPort;
  14. protected override async ETTask Run(Scene scene, EventType.AppStartInitFinish args)
  15. {
  16. Log.Debug("to load login ui.");
  17. var view = await UIHelper.Create("Login");
  18. InitLogin(scene, view);
  19. var preView = GRoot.inst.GetChildAt(0);
  20. GRoot.inst.RemoveChild(preView, true);
  21. UIPackage.RemovePackage("CheckForResUpdate");
  22. }
  23. private void InitServerList(GComboBox list)
  24. {
  25. var saveip = GameSetting.Instance.GetString(GameSetting.Sets.Server_str);
  26. int select = -1;
  27. List<string> servers = new();
  28. foreach (var info in ServerInfo.ServerList)
  29. {
  30. servers.Add(info.ShowName);
  31. if (select < 0 && !saveip.IsNullOrWhitespace())
  32. {
  33. if (saveip == info.HostStr)
  34. {
  35. select = servers.Count - 1;
  36. }
  37. }
  38. }
  39. if(select < 0 && !saveip.IsNullOrWhitespace())
  40. {
  41. try
  42. {
  43. var ipendpoint = NetworkHelper.ToIPEndPoint(saveip);
  44. servers.Add($"Custom[{saveip}]");
  45. ServerInfo.ServerList.Add(new ServerInfo()
  46. {
  47. ShowName = $"Custom[{saveip}]",
  48. Ip = ipendpoint.Address.ToString(),
  49. Port = ipendpoint.Port,
  50. }) ;
  51. select = servers.Count - 1;
  52. }
  53. catch(Exception)
  54. {
  55. Log.Error($"error save ip: {saveip}");
  56. }
  57. }
  58. if(select < 0)
  59. {
  60. select = 0;
  61. }
  62. var cur = ServerInfo.ServerList[select];
  63. mIP = cur.Ip;
  64. mPort = cur.Port;
  65. list.items = servers.ToArray();
  66. list.selectedIndex = select;
  67. list.onChanged.Set(() =>
  68. {
  69. var info = ServerInfo.ServerList[list.selectedIndex];
  70. GameSetting.Instance.SetString(GameSetting.Sets.Server_str, info.HostStr);
  71. mIP = info.Ip;
  72. mPort = info.Port;
  73. });
  74. }
  75. private void InitResolutionList(GComboBox list)
  76. {
  77. #if UNITY_EDITOR
  78. list.visible = false;
  79. #elif UNITY_STANDALONE
  80. list.visible = true;
  81. int val = GameSetting.Instance.GetInt(GameSetting.Sets.Resolution_int, 0);
  82. int select = -1;
  83. List<string> showlist = new();
  84. foreach (var info in ResolutionInfo.ResolutionList)
  85. {
  86. showlist.Add(info.ShowName);
  87. if (select < 0 && val == info.Y)
  88. {
  89. select = showlist.Count - 1;
  90. }
  91. }
  92. if (select < 0)
  93. {
  94. select = 0;
  95. }
  96. var cur = ResolutionInfo.ResolutionList[select];
  97. Screen.SetResolution(cur.X, cur.Y, false);
  98. Log.Debug($"Default resolution x:{cur.X}, y:{cur.Y}");
  99. list.items = showlist.ToArray();
  100. list.selectedIndex = select;
  101. list.onChanged.Set(() =>
  102. {
  103. var info = ResolutionInfo.ResolutionList[list.selectedIndex];
  104. GameSetting.Instance.SetInt(GameSetting.Sets.Resolution_int, info.Y);
  105. Screen.SetResolution(info.X, info.Y, false);
  106. Log.Debug($"SetResolution x:{info.X}, y:{info.Y}");
  107. });
  108. #endif
  109. }
  110. private void InitLogin(Scene scene, GComponent view)
  111. {
  112. var comp = view.GetChild("comp_login") as GComponent;
  113. var listSvr = view.GetChild("listServer").asComboBox;
  114. var listResolution = comp.GetChild("listResolution").asComboBox;
  115. InitServerList(listSvr);
  116. InitResolutionList(listResolution);
  117. var inputID = comp.GetChild("txtID");
  118. var btn = comp.GetChild("btn_go");
  119. btn.onClick.Add(async () =>
  120. {
  121. var imgtips = comp.GetChild("img_tips");
  122. var tips = comp.GetChild("txt_tips");
  123. tips.visible = false;
  124. imgtips.visible = false;
  125. /*if (account.text.IsNullOrWhitespace() || password.text.IsNullOrWhitespace())
  126. {
  127. tips.visible = true;
  128. tips.text = "用户名或密码为空";
  129. return;
  130. }*/
  131. imgtips.visible = true;
  132. btn.enabled = false;
  133. var ret = await LoginHelper.Login(scene, mIP, mPort, "111", "111");
  134. if (ret != ErrorCode.ERR_Success)
  135. {
  136. imgtips.visible = false;
  137. tips.visible = true;
  138. btn.enabled = true;
  139. if (ret == ErrorCode.ERR_UserNameOrPasswordFormatError ||
  140. ret == ErrorCode.ERR_UserNameOrPasswordError)
  141. {
  142. tips.text = "用户名或密码错误";
  143. }
  144. else
  145. {
  146. tips.text = "连接服务器过程中出现了问题\n" +
  147. "如重试后还是不行,请与客服联系" +
  148. "\n[color=#FF0000]QQ: 2910280670[/color]" +
  149. "\n微信:[color=#FF0000]lvlh117[/color]";
  150. }
  151. }
  152. });
  153. }
  154. }
  155. }