UIJoinRoomComponentSystem.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. await CommonUtil.Instance.OpenCommonServerMsgPanel("输入房间号");
  73. return;
  74. }
  75. try
  76. {
  77. Dictionary<string, object> dic = new Dictionary<string, object>()
  78. {
  79. { "roomId",self.inputText}
  80. };
  81. await RequestServerUtil.Instance.OpenRequestServer(OuterMessage.C2G_JoinRoom, dic, async (messageObj, errorCode) =>
  82. {
  83. if (errorCode != ErrorCode.ERR_Success)
  84. {
  85. await CommonUtil.Instance.OpenCommonServerMsgPanel($"加入房间失败,errCode={errorCode}");
  86. return;
  87. }
  88. //关闭主界面
  89. await UIHelper.Remove(GameUtil.Instance.GetSceneComponent(), UIType.UIMain);
  90. await UIHelper.Remove(GameUtil.Instance.GetSceneComponent(), UIType.UIJoinRoom);
  91. //打开开始打牌界面
  92. await UIHelper.Create(GameUtil.Instance.GetSceneComponent(), UIType.UIStartFightRoom, UILayer.Low);
  93. });
  94. }
  95. catch (Exception e)
  96. {
  97. Log.Error($"创建房间出错...{e.Message}");
  98. }
  99. }
  100. public static async void OnCloseAsync(this UIJoinRoomComponent self)
  101. {
  102. await SoundManager.Instance.PlaySound("clickbtnsound");
  103. await UIHelper.Remove(self.ClientScene(),UIType.UIJoinRoom);
  104. }
  105. }
  106. }