UILoginComponent.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. SessionWrap sessionWrap = null;
  30. try
  31. {
  32. IPEndPoint connetEndPoint = NetworkHelper.ToIPEndPoint(GlobalConfigComponent.Instance.GlobalProto.Address);
  33. string text = this.account.GetComponent<InputField>().text;
  34. Session session = ETModel.Game.Scene.GetComponent<NetOuterComponent>().Create(connetEndPoint);
  35. sessionWrap = new SessionWrap(session);
  36. R2C_Login r2CLogin = (R2C_Login) await sessionWrap.Call(new C2R_Login() { Account = text, Password = "111111" });
  37. sessionWrap.Dispose();
  38. connetEndPoint = NetworkHelper.ToIPEndPoint(r2CLogin.Address);
  39. Session gateSession = ETModel.Game.Scene.GetComponent<NetOuterComponent>().Create(connetEndPoint);
  40. Game.Scene.AddComponent<SessionWrapComponent>().Session = new SessionWrap(gateSession);
  41. ETModel.Game.Scene.AddComponent<SessionComponent>().Session = gateSession;
  42. G2C_LoginGate g2CLoginGate = (G2C_LoginGate)await SessionWrapComponent.Instance.Session.Call(new C2G_LoginGate() { Key = r2CLogin.Key });
  43. Log.Info("登陆gate成功!");
  44. // 创建Player
  45. Player player = ETModel.ComponentFactory.CreateWithId<Player>(g2CLoginGate.PlayerId);
  46. PlayerComponent playerComponent = ETModel.Game.Scene.GetComponent<PlayerComponent>();
  47. playerComponent.MyPlayer = player;
  48. Game.Scene.GetComponent<UIComponent>().Create(UIType.UILobby);
  49. Game.Scene.GetComponent<UIComponent>().Remove(UIType.UILogin);
  50. }
  51. catch (Exception e)
  52. {
  53. sessionWrap?.Dispose();
  54. Log.Error(e);
  55. }
  56. }
  57. }
  58. }