UILoginComponent.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using System.Net;
  3. using Model;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. namespace Hotfix
  7. {
  8. [ObjectEvent]
  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. Session session = Game.Scene.GetComponent<NetOuterComponent>().Create(connetEndPoint);
  33. string text = this.account.GetComponent<InputField>().text;
  34. R2C_Login r2CLogin = (R2C_Login)await session.Call(new C2R_Login() { Account = text, Password = "111111" }, true);
  35. if (r2CLogin.Error != ErrorCode.ERR_Success)
  36. {
  37. Log.Error(r2CLogin.Error.ToString());
  38. return;
  39. }
  40. connetEndPoint = NetworkHelper.ToIPEndPoint(r2CLogin.Address);
  41. Session gateSession = Game.Scene.GetComponent<NetOuterComponent>().Create(connetEndPoint);
  42. Game.Scene.AddComponent<SessionComponent>().Session = gateSession;
  43. G2C_LoginGate g2CLoginGate = (G2C_LoginGate)await SessionComponent.Instance.Session.Call(new C2G_LoginGate() { Key = r2CLogin.Key }, true);
  44. Log.Info("登陆gate成功!");
  45. // 创建Player
  46. Player player = Model.EntityFactory.CreateWithId<Player>(g2CLoginGate.PlayerId);
  47. PlayerComponent playerComponent = Game.Scene.GetComponent<PlayerComponent>();
  48. playerComponent.MyPlayer = player;
  49. Hotfix.Scene.GetComponent<UIComponent>().Create(UIType.UILobby);
  50. Hotfix.Scene.GetComponent<UIComponent>().Remove(UIType.UILogin);
  51. }
  52. catch (Exception e)
  53. {
  54. Log.Error(e.ToString());
  55. }
  56. }
  57. }
  58. }