UIJoinRoomComponentSystem.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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, params object[] param)
  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 async void OnInputNumber(this UIJoinRoomComponent self,int index)
  49. {
  50. await SoundManager.Instance.PlaySound("clickbtnsound");
  51. self.updateShowText(index.ToString());
  52. }
  53. public static async void OnInputBack(this UIJoinRoomComponent self)
  54. {
  55. await SoundManager.Instance.PlaySound("clickbtnsound");
  56. if (self.inputText.Length > 0)
  57. {
  58. self.inputText = self.inputText.Substring(0, self.inputText.Length - 1);
  59. self.showTxt.GetComponent<UnityEngine.UI.Text>().text = self.inputText;
  60. }
  61. }
  62. public static async void OnInputClear(this UIJoinRoomComponent self)
  63. {
  64. await SoundManager.Instance.PlaySound("clickbtnsound");
  65. self.inputText = string.Empty;
  66. self.updateShowText();
  67. }
  68. static async ETTask JoinRoom(this UIJoinRoomComponent self)
  69. {
  70. if (string.IsNullOrEmpty(self.inputText) || self.inputText.Length < 6)
  71. {
  72. Log.Error("输入房间号");
  73. return;
  74. }
  75. try
  76. {
  77. var scene = self.ClientScene();
  78. var session = scene.GetComponent<SessionComponent>().Session;
  79. if (session != null)
  80. {
  81. G2C_JoinRoom g2JoinRoom = (G2C_JoinRoom)await session.Call(
  82. new C2G_JoinRoom() { RoomId = int.Parse(self.inputText) });
  83. if (g2JoinRoom.Error != ErrorCode.ERR_Success)
  84. {
  85. Log.Error($"g2JoinRoom错误...errCode={g2JoinRoom.Error}");
  86. return;
  87. }
  88. //改成推送了
  89. //var startFightRoomComponment = scene.GetComponent<StartFightRoomComponment>();
  90. //if (startFightRoomComponment == null)
  91. //{
  92. // startFightRoomComponment = scene.AddComponent<StartFightRoomComponment>();
  93. //}
  94. //startFightRoomComponment.ClearStartFightRoomInfo();
  95. //startFightRoomComponment.SetStartFightRoomInfo(g2JoinRoom);
  96. //目前就用一个场景吧,后面看再单独分开。
  97. //关闭主界面
  98. await UIHelper.Remove(scene, UIType.UIMain);
  99. await UIHelper.Remove(scene, UIType.UIJoinRoom);
  100. //打开开始打牌界面
  101. await UIHelper.Create(scene, UIType.UIStartFightRoom, UILayer.Mid);
  102. }
  103. }
  104. catch (Exception e)
  105. {
  106. Log.Error($"创建房间出错...{e.Message}");
  107. }
  108. }
  109. public static async void OnCloseAsync(this UIJoinRoomComponent self)
  110. {
  111. await SoundManager.Instance.PlaySound("clickbtnsound");
  112. await UIHelper.Remove(self.ClientScene(),UIType.UIJoinRoom);
  113. }
  114. }
  115. }