LoginHelper.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. namespace ET
  3. {
  4. public static class LoginHelper
  5. {
  6. public static async ETVoid OnLoginAsync(Entity domain, string address, string account)
  7. {
  8. try
  9. {
  10. // 创建一个ETModel层的Session
  11. Session session = Game.Scene.GetComponent<NetOuterComponent>().Create(address);
  12. // 创建一个ETHotfix层的Session, ETHotfix的Session会通过ETModel层的Session发送消息
  13. Session realmSession = EntityFactory.Create<Session, Session>(domain, session);
  14. R2C_Login r2CLogin = (R2C_Login) await realmSession.Call(new C2R_Login() { Account = account, Password = "111111" });
  15. realmSession.Dispose();
  16. // 创建一个ETModel层的Session,并且保存到ETModel.SessionComponent中
  17. Session gateSession = Game.Scene.GetComponent<NetOuterComponent>().Create(r2CLogin.Address);
  18. Game.Scene.AddComponent<SessionComponent>().Session = gateSession;
  19. // 创建一个ETHotfix层的Session, 并且保存到ETHotfix.SessionComponent中
  20. Game.Scene.AddComponent<SessionComponent>().Session = EntityFactory.Create<Session, Session>(Game.Scene, gateSession);
  21. G2C_LoginGate g2CLoginGate = (G2C_LoginGate)await SessionComponent.Instance.Session.Call(
  22. new C2G_LoginGate() { Key = r2CLogin.Key, GateId = r2CLogin.GateId});
  23. Log.Info("登陆gate成功!");
  24. // 创建Player
  25. Player player = EntityFactory.CreateWithId<Player>(Game.Scene, g2CLoginGate.PlayerId);
  26. PlayerComponent playerComponent = 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. }