LoginHelper.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System;
  2. using ETModel;
  3. namespace ETHotfix
  4. {
  5. public static class LoginHelper
  6. {
  7. public static async ETVoid OnLoginAsync(string account)
  8. {
  9. try
  10. {
  11. // 创建一个ETModel层的Session
  12. ETModel.Session session = ETModel.Game.Scene.GetComponent<NetOuterComponent>().Create(GlobalConfigComponent.Instance.GlobalProto.Address);
  13. // 创建一个ETHotfix层的Session, ETHotfix的Session会通过ETModel层的Session发送消息
  14. Session realmSession = ComponentFactory.Create<Session, ETModel.Session>(session);
  15. R2C_Login r2CLogin = (R2C_Login) await realmSession.Call(new C2R_Login() { Account = account, Password = "111111" });
  16. realmSession.Dispose();
  17. // 创建一个ETModel层的Session,并且保存到ETModel.SessionComponent中
  18. ETModel.Session gateSession = ETModel.Game.Scene.GetComponent<NetOuterComponent>().Create(r2CLogin.Address);
  19. ETModel.Game.Scene.AddComponent<ETModel.SessionComponent>().Session = gateSession;
  20. // 创建一个ETHotfix层的Session, 并且保存到ETHotfix.SessionComponent中
  21. Game.Scene.AddComponent<SessionComponent>().Session = ComponentFactory.Create<Session, ETModel.Session>(gateSession);
  22. G2C_LoginGate g2CLoginGate = (G2C_LoginGate)await SessionComponent.Instance.Session.Call(new C2G_LoginGate() { Key = r2CLogin.Key });
  23. Log.Info("登陆gate成功!");
  24. // 创建Player
  25. Player player = ETModel.ComponentFactory.CreateWithId<Player>(g2CLoginGate.PlayerId);
  26. PlayerComponent playerComponent = ETModel.Game.Scene.GetComponent<PlayerComponent>();
  27. playerComponent.MyPlayer = player;
  28. Game.EventSystem.Run(EventIdType.LoginFinish);
  29. // 测试消息有成员是class类型
  30. G2C_PlayerInfo g2CPlayerInfo = (G2C_PlayerInfo) await SessionComponent.Instance.Session.Call(new C2G_PlayerInfo());
  31. }
  32. catch (Exception e)
  33. {
  34. Log.Error(e);
  35. }
  36. }
  37. }
  38. }