UILoginComponent.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Net;
  3. using ETModel;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. namespace ETHotfix
  7. {
  8. [ObjectSystem]
  9. public class UiLoginComponentSystem : AwakeSystem<UILoginComponent>
  10. {
  11. public override void Awake(UILoginComponent self)
  12. {
  13. self.Awake();
  14. }
  15. }
  16. public class UILoginComponent: Component
  17. {
  18. private GameObject account;
  19. private GameObject loginBtn;
  20. public void Awake()
  21. {
  22. ReferenceCollector rc = this.GetParent<UI>().GameObject.GetComponent<ReferenceCollector>();
  23. loginBtn = rc.Get<GameObject>("LoginBtn");
  24. loginBtn.GetComponent<Button>().onClick.Add(OnLogin);
  25. this.account = rc.Get<GameObject>("Account");
  26. }
  27. public async void OnLogin()
  28. {
  29. try
  30. {
  31. string text = this.account.GetComponent<InputField>().text;
  32. // 创建一个ETModel层的Session
  33. ETModel.Session session = ETModel.Game.Scene.GetComponent<NetOuterComponent>().Create(GlobalConfigComponent.Instance.GlobalProto.Address);
  34. // 创建一个ETHotfix层的Session, ETHotfix的Session会通过ETModel层的Session发送消息
  35. Session realmSession = ComponentFactory.Create<Session, ETModel.Session>(session);
  36. R2C_Login r2CLogin = (R2C_Login) await realmSession.Call(new C2R_Login() { Account = text, Password = "111111" });
  37. realmSession.Dispose();
  38. // 创建一个ETModel层的Session,并且保存到ETModel.SessionComponent中
  39. ETModel.Session gateSession = ETModel.Game.Scene.GetComponent<NetOuterComponent>().Create(r2CLogin.Address);
  40. ETModel.Game.Scene.AddComponent<ETModel.SessionComponent>().Session = gateSession;
  41. // 创建一个ETHotfix层的Session, 并且保存到ETHotfix.SessionComponent中
  42. Game.Scene.AddComponent<SessionComponent>().Session = ComponentFactory.Create<Session, ETModel.Session>(gateSession);
  43. G2C_LoginGate g2CLoginGate = (G2C_LoginGate)await SessionComponent.Instance.Session.Call(new C2G_LoginGate() { Key = r2CLogin.Key });
  44. Log.Info("登陆gate成功!");
  45. // 创建Player
  46. Player player = ETModel.ComponentFactory.CreateWithId<Player>(g2CLoginGate.PlayerId);
  47. PlayerComponent playerComponent = ETModel.Game.Scene.GetComponent<PlayerComponent>();
  48. playerComponent.MyPlayer = player;
  49. Game.Scene.GetComponent<UIComponent>().Create(UIType.UILobby);
  50. Game.Scene.GetComponent<UIComponent>().Remove(UIType.UILogin);
  51. // 测试消息有成员是class类型
  52. G2C_PlayerInfo g2CPlayerInfo = (G2C_PlayerInfo) await SessionComponent.Instance.Session.Call(new C2G_PlayerInfo());
  53. }
  54. catch (Exception e)
  55. {
  56. Log.Error(e);
  57. }
  58. }
  59. }
  60. }