AppStartInitFinish_CreateLoginUI.cs 5.9 KB

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