UILobbyComponent.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using ETModel;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace ETHotfix
  6. {
  7. [ObjectSystem]
  8. public class UiLobbyComponentSystem : AwakeSystem<UILobbyComponent>
  9. {
  10. public override void Awake(UILobbyComponent self)
  11. {
  12. self.Awake();
  13. }
  14. }
  15. public class UILobbyComponent : Component
  16. {
  17. private GameObject enterMap;
  18. private Text text;
  19. public void Awake()
  20. {
  21. ReferenceCollector rc = this.GetParent<UI>().GameObject.GetComponent<ReferenceCollector>();
  22. GameObject sendBtn = rc.Get<GameObject>("Send");
  23. GameObject sendRpcBtn = rc.Get<GameObject>("" + "SendRpc");
  24. sendBtn.GetComponent<Button>().onClick.Add(this.OnSend);
  25. sendRpcBtn.GetComponent<Button>().onClick.Add(this.OnSendRpc);
  26. GameObject transfer1Btn = rc.Get<GameObject>("Transfer1");
  27. GameObject transfer2Btn = rc.Get<GameObject>("Transfer2");
  28. transfer1Btn.GetComponent<Button>().onClick.Add(this.OnTransfer1);
  29. transfer2Btn.GetComponent<Button>().onClick.Add(this.OnTransfer2);
  30. enterMap = rc.Get<GameObject>("EnterMap");
  31. enterMap.GetComponent<Button>().onClick.Add(this.EnterMap);
  32. this.text = rc.Get<GameObject>("Text").GetComponent<Text>();
  33. }
  34. private void OnSend()
  35. {
  36. // 发送一个actor消息
  37. ETModel.SessionComponent.Instance.Session.Send(new Actor_Test() { Info = "message client->gate->map->gate->client" });
  38. }
  39. private async void OnSendRpc()
  40. {
  41. try
  42. {
  43. // 向actor发起一次rpc调用
  44. Actor_TestResponse response = (Actor_TestResponse) await ETModel.SessionComponent.Instance.Session.Call(new Actor_TestRequest() { Request = "request actor test rpc" });
  45. Log.Info($"recv response: {JsonHelper.ToJson(response)}");
  46. }
  47. catch (Exception e)
  48. {
  49. Log.Error(e);
  50. }
  51. }
  52. private async void OnTransfer1()
  53. {
  54. try
  55. {
  56. Actor_TransferResponse response = (Actor_TransferResponse) await ETModel.SessionComponent.Instance.Session.Call(new Actor_TransferRequest() {MapIndex = 0});
  57. Log.Info($"传送成功! {JsonHelper.ToJson(response)}");
  58. }
  59. catch (Exception e)
  60. {
  61. Log.Error(e);
  62. }
  63. }
  64. private async void OnTransfer2()
  65. {
  66. Actor_TransferResponse response = (Actor_TransferResponse)await ETModel.SessionComponent.Instance.Session.Call(new Actor_TransferRequest() { MapIndex = 1 });
  67. Log.Info($"传送成功! {JsonHelper.ToJson(response)}");
  68. }
  69. private async void EnterMap()
  70. {
  71. try
  72. {
  73. // 加载Unit资源
  74. ResourcesComponent resourcesComponent = ETModel.Game.Scene.GetComponent<ResourcesComponent>();
  75. await resourcesComponent.LoadBundleAsync($"unit.unity3d");
  76. // 加载场景资源
  77. await ETModel.Game.Scene.GetComponent<ResourcesComponent>().LoadBundleAsync("map.unity3d");
  78. // 切换到map场景
  79. using (SceneChangeComponent sceneChangeComponent = ETModel.Game.Scene.AddComponent<SceneChangeComponent>())
  80. {
  81. await sceneChangeComponent.ChangeSceneAsync(SceneType.Map);
  82. }
  83. G2C_EnterMap g2CEnterMap = await ETModel.SessionComponent.Instance.Session.Call(new C2G_EnterMap()) as G2C_EnterMap;
  84. PlayerComponent.Instance.MyPlayer.UnitId = g2CEnterMap.UnitId;
  85. Game.Scene.AddComponent<OperaComponent>();
  86. Game.Scene.GetComponent<UIComponent>().Remove(UIType.UILobby);
  87. }
  88. catch (Exception e)
  89. {
  90. Log.Error(e);
  91. }
  92. }
  93. }
  94. }