UILobbyComponent.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using Model;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace Hotfix
  6. {
  7. [ObjectEvent(EntityEventId.UILobbyComponent)]
  8. public class UILobbyComponent: Component, IAwake
  9. {
  10. public void Awake()
  11. {
  12. ReferenceCollector rc = this.GetOwner<UI>().GameObject.GetComponent<ReferenceCollector>();
  13. GameObject createRoom = rc.Get<GameObject>("CreateRoom");
  14. GameObject joinRoom = rc.Get<GameObject>("JoinRoom");
  15. createRoom.GetComponent<Button>().onClick.Add(OnCreateRoom);
  16. joinRoom.GetComponent<Button>().onClick.Add(OnJoinRoom);
  17. }
  18. private static async void OnCreateRoom()
  19. {
  20. Session session = null;
  21. try
  22. {
  23. session = Hotfix.Scene.ModelScene.GetComponent<NetOuterComponent>().Create("127.0.0.1:10002");
  24. R2C_Login r2CLogin = await session.Call<R2C_Login>(new C2R_Login() { Account = "abcdef", Password = "111111" });
  25. Session gateSession = Hotfix.Scene.ModelScene.GetComponent<NetOuterComponent>().Create(r2CLogin.Address);
  26. G2C_LoginGate g2CLoginGate = await gateSession.Call<G2C_LoginGate>(new C2G_LoginGate(r2CLogin.Key));
  27. Log.Debug($"{JsonHelper.ToJson(g2CLoginGate)}");
  28. Log.Info("登陆gate成功!");
  29. // 发送一个actor消息
  30. gateSession.Send(new Actor_Test() { Info = "message client->gate->map->gate->client" });
  31. // 向actor发起一次rpc调用
  32. ActorRpc_TestResponse response = await gateSession.Call<ActorRpc_TestResponse>(new ActorRpc_TestRequest() { request = "request actor test rpc" });
  33. Log.Info($"recv response: {JsonHelper.ToJson(response)}");
  34. }
  35. catch (Exception e)
  36. {
  37. Log.Error(e.ToStr());
  38. }
  39. finally
  40. {
  41. session?.Dispose();
  42. }
  43. }
  44. private static void OnJoinRoom()
  45. {
  46. }
  47. }
  48. }