UILoginComponent.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. using Model;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace Hotfix
  6. {
  7. [ObjectEvent]
  8. public class UILoginComponentEvent : ObjectEvent<UILoginComponent>, IAwake
  9. {
  10. public void Awake()
  11. {
  12. this.Get().Awake();
  13. }
  14. }
  15. public class UILoginComponent: Component
  16. {
  17. private GameObject account;
  18. private GameObject loginBtn;
  19. public void Awake()
  20. {
  21. ReferenceCollector rc = this.GetEntity<UI>().GameObject.GetComponent<ReferenceCollector>();
  22. loginBtn = rc.Get<GameObject>("LoginBtn");
  23. loginBtn.GetComponent<Button>().onClick.Add(OnLogin);
  24. this.account = rc.Get<GameObject>("Account");
  25. }
  26. private async void OnLogin()
  27. {
  28. Session session = null;
  29. try
  30. {
  31. session = Hotfix.Scene.ModelScene.GetComponent<NetOuterComponent>().Create("127.0.0.1:10002");
  32. string text = this.account.GetComponent<InputField>().text;
  33. R2C_Login r2CLogin = await session.Call<R2C_Login>(new C2R_Login() { Account = text, Password = "111111" });
  34. Session gateSession = Hotfix.Scene.ModelScene.GetComponent<NetOuterComponent>().Create(r2CLogin.Address);
  35. Game.Scene.AddComponent<SessionComponent>().Session = gateSession;
  36. G2C_LoginGate g2CLoginGate = await SessionComponent.Instance.Session.Call<G2C_LoginGate>(new C2G_LoginGate(r2CLogin.Key));
  37. Log.Info("登陆gate成功!");
  38. // 创建Player
  39. Player player = Model.EntityFactory.CreateWithId<Player>(g2CLoginGate.PlayerId);
  40. PlayerComponent playerComponent = Game.Scene.GetComponent<PlayerComponent>();
  41. playerComponent.MyPlayer = player;
  42. Hotfix.Scene.GetComponent<UIComponent>().Create(UIType.Lobby);
  43. Hotfix.Scene.GetComponent<UIComponent>().Remove(UIType.Login);
  44. }
  45. catch (Exception e)
  46. {
  47. Log.Error(e.ToStr());
  48. }
  49. finally
  50. {
  51. session?.Dispose();
  52. }
  53. }
  54. }
  55. }