UILoginComponent.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. Hotfix.Scene.GetComponent<UIComponent>().Create(UIType.Lobby);
  39. Hotfix.Scene.GetComponent<UIComponent>().Remove(UIType.Login);
  40. }
  41. catch (Exception e)
  42. {
  43. Log.Error(e.ToStr());
  44. }
  45. finally
  46. {
  47. session?.Dispose();
  48. }
  49. }
  50. }
  51. }