UILoginComponent.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using System.Net;
  3. using Model;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. namespace Hotfix
  7. {
  8. [ObjectSystem]
  9. public class UiLoginComponentSystem : ObjectSystem<UILoginComponent>, IAwake
  10. {
  11. public void Awake()
  12. {
  13. this.Get().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. IPEndPoint connetEndPoint = NetworkHelper.ToIPEndPoint(GlobalConfigComponent.Instance.GlobalProto.Address);
  32. string text = this.account.GetComponent<InputField>().text;
  33. R2C_Login r2CLogin;
  34. using (Session session = Game.Scene.GetComponent<NetOuterComponent>().Create(connetEndPoint))
  35. {
  36. r2CLogin = (R2C_Login) await session.Call(new C2R_Login() { Account = text, Password = "111111" });
  37. }
  38. connetEndPoint = NetworkHelper.ToIPEndPoint(r2CLogin.Address);
  39. Session gateSession = Game.Scene.GetComponent<NetOuterComponent>().Create(connetEndPoint);
  40. Game.Scene.AddComponent<SessionComponent>().Session = gateSession;
  41. G2C_LoginGate g2CLoginGate = (G2C_LoginGate)await SessionComponent.Instance.Session.Call(new C2G_LoginGate() { Key = r2CLogin.Key });
  42. Log.Info("登陆gate成功!");
  43. // 创建Player
  44. Player player = Model.ComponentFactory.CreateWithId<Player>(g2CLoginGate.PlayerId);
  45. PlayerComponent playerComponent = Game.Scene.GetComponent<PlayerComponent>();
  46. playerComponent.MyPlayer = player;
  47. Hotfix.Scene.GetComponent<UIComponent>().Create(UIType.UILobby);
  48. Hotfix.Scene.GetComponent<UIComponent>().Remove(UIType.UILogin);
  49. }
  50. catch (Exception e)
  51. {
  52. Log.Error(e.ToStr());
  53. }
  54. }
  55. }
  56. }