UILoginComponent.cs 2.1 KB

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