UILoginComponent.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 UILoginComponentEvent : ObjectEvent<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.GetEntity<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. private void OnLogin()
  28. {
  29. Session session = null;
  30. IPEndPoint connetEndPoint = NetworkHelper.ToIPEndPoint(GlobalConfigComponent.Instance.GlobalProto.Address);
  31. session = Game.Scene.GetComponent<NetOuterComponent>().Create(connetEndPoint);
  32. string text = this.account.GetComponent<InputField>().text;
  33. session.CallWithAction(new C2R_Login() { Account = text, Password = "111111" }, (response) => LoginOK(response));
  34. }
  35. private void LoginOK(AResponse response)
  36. {
  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. }