UILoginComponent.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System.Net;
  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 void OnLogin()
  27. {
  28. Session session = null;
  29. IPEndPoint connetEndPoint = NetworkHelper.ToIPEndPoint(GlobalConfigComponent.Instance.GlobalProto.Address);
  30. session = Game.Scene.GetComponent<NetOuterComponent>().Create(connetEndPoint);
  31. string text = this.account.GetComponent<InputField>().text;
  32. session.CallWithAction(new C2R_Login() { Account = text, Password = "111111" }, (response) => LoginOK(session, response));
  33. }
  34. private void LoginOK(Session loginSession, AResponse response)
  35. {
  36. loginSession.Dispose();
  37. R2C_Login r2CLogin = (R2C_Login) response;
  38. if (r2CLogin.Error != ErrorCode.ERR_Success)
  39. {
  40. Log.Error(r2CLogin.Error.ToString());
  41. return;
  42. }
  43. IPEndPoint connetEndPoint = NetworkHelper.ToIPEndPoint(r2CLogin.Address);
  44. Session gateSession = Game.Scene.GetComponent<NetOuterComponent>().Create(connetEndPoint);
  45. Game.Scene.AddComponent<SessionComponent>().Session = gateSession;
  46. SessionComponent.Instance.Session.CallWithAction(new C2G_LoginGate() { Key = r2CLogin.Key },
  47. (response2)=>LoginGateOk(response2)
  48. );
  49. }
  50. private void LoginGateOk(AResponse response)
  51. {
  52. G2C_LoginGate g2CLoginGate = (G2C_LoginGate) response;
  53. if (g2CLoginGate.Error != ErrorCode.ERR_Success)
  54. {
  55. Log.Error(g2CLoginGate.Error.ToString());
  56. return;
  57. }
  58. Log.Info("登陆gate成功!");
  59. // 创建Player
  60. Player player = Model.EntityFactory.CreateWithId<Player>(g2CLoginGate.PlayerId);
  61. PlayerComponent playerComponent = Game.Scene.GetComponent<PlayerComponent>();
  62. playerComponent.MyPlayer = player;
  63. Hotfix.Scene.GetComponent<UIComponent>().Create(UIType.UILobby);
  64. Hotfix.Scene.GetComponent<UIComponent>().Remove(UIType.UILogin);
  65. }
  66. }
  67. }