UIJoinRoomComponentSystem.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using static System.Net.Mime.MediaTypeNames;
  7. namespace ET.Client
  8. {
  9. [FriendOf(typeof(UIJoinRoomComponent))]
  10. public static class UIJoinRoomComponentSystem
  11. {
  12. [ObjectSystem]
  13. public class UIJoinRoomComponentAwakeSystem : AwakeSystem<UIJoinRoomComponent>
  14. {
  15. protected override void Awake(UIJoinRoomComponent self)
  16. {
  17. ReferenceCollector rc = self.GetParent<UI>().GameObject.GetComponent<ReferenceCollector>();
  18. self.inputBackBtn = rc.Get<GameObject>("inputBack");
  19. self.inputClearBtn = rc.Get<GameObject>("inputClear");
  20. self.closeBtn = rc.Get<GameObject>("closeBtn");
  21. self.showTxt = rc.Get<GameObject>("showTxt");
  22. for (int i = 0;i <= 9;i++)
  23. {
  24. var tempInput = rc.Get<GameObject>("input" + i);
  25. int index = i;
  26. tempInput.GetComponent<Button>().onClick.AddListener(() => { self.OnInputNumber(index); });
  27. }
  28. self.inputBackBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnInputBack(); });
  29. self.inputClearBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnInputClear(); });
  30. self.closeBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnCloseAsync(); });
  31. self.showTxt.GetComponent<UnityEngine.UI.Text>().text = "";
  32. }
  33. }
  34. private static void updateShowText(this UIJoinRoomComponent self, string add = "")
  35. {
  36. if (!string.IsNullOrEmpty(add) && self.inputText.Length < 6)
  37. {
  38. self.inputText = self.inputText + add;
  39. }
  40. if (self.inputText.Length == 6)
  41. {
  42. //已经6位了。。。
  43. //加入房间请求。。
  44. self.JoinRoom().Coroutine();
  45. }
  46. self.showTxt.GetComponent<UnityEngine.UI.Text>().text = self.inputText;
  47. }
  48. public static void OnInputNumber(this UIJoinRoomComponent self,int index)
  49. {
  50. self.updateShowText(index.ToString());
  51. }
  52. public static void OnInputBack(this UIJoinRoomComponent self)
  53. {
  54. if (self.inputText.Length > 0)
  55. {
  56. self.inputText = self.inputText.Substring(0, self.inputText.Length - 1);
  57. self.showTxt.GetComponent<UnityEngine.UI.Text>().text = self.inputText;
  58. }
  59. }
  60. public static void OnInputClear(this UIJoinRoomComponent self)
  61. {
  62. self.inputText = string.Empty;
  63. self.updateShowText();
  64. }
  65. static async ETTask JoinRoom(this UIJoinRoomComponent self)
  66. {
  67. if (string.IsNullOrEmpty(self.inputText) || self.inputText.Length < 6)
  68. {
  69. Log.Error("输入房间号");
  70. return;
  71. }
  72. try
  73. {
  74. var scene = self.ClientScene();
  75. var session = scene.GetComponent<SessionComponent>().Session;
  76. if (session != null)
  77. {
  78. G2C_JoinRoom g2JoinRoom = (G2C_JoinRoom)await session.Call(
  79. new C2G_JoinRoom() { RoomId = self.inputText });
  80. if (g2JoinRoom.Error != ErrorCode.ERR_Success)
  81. {
  82. Log.Error($"g2JoinRoom错误...errCode={g2JoinRoom.Error}");
  83. return;
  84. }
  85. var startFightRoomComponment = scene.GetComponent<StartFightRoomComponment>();
  86. if (startFightRoomComponment == null)
  87. {
  88. startFightRoomComponment = scene.AddComponent<StartFightRoomComponment>();
  89. }
  90. startFightRoomComponment.ClearStartFightRoomInfo();
  91. startFightRoomComponment.SetStartFightRoomInfo(g2JoinRoom);
  92. //目前就用一个场景吧,后面看再单独分开。
  93. //关闭主界面
  94. await UIHelper.Remove(scene, UIType.UIMain);
  95. await UIHelper.Remove(scene, UIType.UIJoinRoom);
  96. //打开开始打牌界面
  97. await UIHelper.Create(scene, UIType.UIStartFightRoom, UILayer.Mid);
  98. }
  99. }
  100. catch (Exception e)
  101. {
  102. Log.Error($"创建房间出错...{e.Message}");
  103. }
  104. }
  105. public static async void OnCloseAsync(this UIJoinRoomComponent self)
  106. {
  107. await UIHelper.Remove(self.ClientScene(),UIType.UIJoinRoom);
  108. }
  109. }
  110. }