版权申明:

  • 本文原创首发于以下网站:
  1. 博客园『优梦创客』的空间:https://www.cnblogs.com/raymondking123
  2. 优梦创客的官方博客:https://91make.top
  3. 优梦创客的游戏讲堂:https://91make.ke.qq.com
  4. 『优梦创客』的微信公众号:umaketop
  • 您可以自由转载,但必须加入完整的版权声明!

概要:

  • ET4示例程序中的登录流程大致为:实例化UILogin进行登录->实例化UILobby进行匹配→加载Map场景进行游戏
  • 由于其流程对于新手有点复杂,这里介绍一种简单的学习方式:
    • 直接创建一个StartUI
    • 点击“游客登录”按钮执行登录功能

创建一个StartUI:

  • 将启动UI做成一个预制体,并命名为StartUI,UI界面如下:



  • 由于ET4采用ab包的方式加载资源,所以要:
    • 预制体设置Asset Label为:startui.unity3d
    • 用ET4的编辑器扩展:Tools→打包工具→选择PC平台进行打包
    • 打包后的资源放在???
    • 不需要启动服务端或文件服务器,ET4框架可以找到打包后的ab包
    • 编辑器状态下会查找预制体而不从ab包加载
  • 用代码加载该预制体:
namespace ETHotfix
{
public static class MyStartUIFactory
{
public static UI Create()
{
try
{
ResourcesComponent rc = ETModel.Game.Scene.GetComponent<ResourcesComponent>();
rc.LoadBundle(MyUIType.MyStartUI.StringToAB());
GameObject goPrefab = rc.GetAsset(MyUIType.MyStartUI.StringToAB(), MyUIType.MyStartUI.ToString()) as GameObject;
GameObject go = UnityEngine.Object.Instantiate(goPrefab); UI ui = ComponentFactory.Create<UI,string,GameObject>(MyUIType.MyStartUI.ToString(), go, false); ui.AddComponent<MyStartUIComponent>(); return ui;
}
catch (Exception e)
{
Log.Error(e);
return null;
}
}
}
}

点击“游客登录”按钮执行登录功能

namespace ETHotfix
{
[ObjectSystem]
public class MyStartUIAwakeSystem : AwakeSystem<MyStartUIComponent>
{
public override void Awake(MyStartUIComponent self)
{
self.OnAwake();
}
} public class MyStartUIComponent : Component
{
GameObject goStartButton; public void OnAwake()
{
var rc = this.Parent.GameObject.GetComponent<ReferenceCollector>();
goStartButton = rc.Get<GameObject>("StartButton");
goStartButton.GetComponent<Button>().onClick.AddListener(OnStartClick);
} private void OnStartClick()
{
// Coroutine()方法模拟将OnStartClickAsync()作为协程执行的效果(确保在单线程中执行)
OnStartClickAsync().Coroutine();
} private async ETVoid OnStartClickAsync()
{
// 处理游客登录点击
try
{
// 1: 创建一个realmSession,并利用此session去call一个rpc消息(模型层session表示具体网络参数和连接,热更层负责通信逻辑,它调用模型层)
ETModel.Session realmModelsession = ETModel.Game.Scene.GetComponent<NetOuterComponent>().Create(GlobalConfigComponent.Instance.GlobalProto.Address);
Session realmHotfixSession = ComponentFactory.Create<Session, ETModel.Session>(realmModelsession); // 2:若登录失败,会收到异常;若成功,则会得到认证信息(模型层没有C2R_Login的类定义,所以必须配套由热更层的session去call)
var r2cLogin = await realmHotfixSession.Call(new C2R_Login()
{
Account = Guid.NewGuid().ToString(),
Password = "111111",
}) as R2C_Login; // 3:再利用此认证信息中的网关服务器地址,去创建gateSession
ETModel.Session gateModelsession = ETModel.Game.Scene.GetComponent<NetOuterComponent>().Create(r2cLogin.Address);
Session gateHotfixSession = ComponentFactory.Create<Session, ETModel.Session>(gateModelsession); // 4:用gateSession去登录gate服(传入登录key)
var g2cLoginGate = await gateHotfixSession.Call(new C2G_LoginGate()
{
Key = r2cLogin.Key
}) as G2C_LoginGate; // 5:登录成功后,用返回的playerId,创建Player对象
var p = ETModel.ComponentFactory.CreateWithId<Player>(g2cLoginGate.PlayerId);
ETModel.Game.Scene.GetComponent<PlayerComponent>().MyPlayer = p; // 6:发送登录完成事件
Game.EventSystem.Run(MyEventType.MyLoginFinish); // 7: 保存热更和模型层Session,以便将来直接通过SessionComponent.Instance单件访问
Game.Scene.AddComponent<SessionComponent>().Session = gateHotfixSession;
ETModel.Game.Scene.AddComponent<ETModel.SessionComponent>().Session = gateModelsession;
}
catch (Exception e)
{
Log.Error(e);
}
}
}
}
  • EventSystem.Run引发本地事件,Session.Call|Send引发网络消息

处理登录成功消息:

namespace ETHotfix
{
[Event(MyEventType.MyLoginFinish)]
class MyLoginFinish : AEvent
{
public override void Run()
{
// 移除登录UI
UI uiLogin = Game.Scene.GetComponent<UIComponent>().Get(MyUIType.MyStartUI.ToString());
uiLogin.GameObject.SetActive(false); // 加载游戏场景(也可以从ab包加载)
SceneManager.LoadScene(1);
}
}
}

最新文章

  1. 软将工程课设day9
  2. 《介绍一款开源的类Excel电子表格软件》续:七牛云存储实战(C#)
  3. How to Write and Publish a Scientific Paper: 7th Edition(科技论文写作与发表教程)(11.04更新)
  4. iOS多线程编程Part 3/3 - GCD
  5. 【asp.net爬虫】asp.NET分页控件抓取第n页数据 javascript:__doPostBack
  6. 集成对象和 JSON
  7. 树莓派学习笔记——交叉编译练习之SQLite3安装
  8. 增强SEO的div+css命名规则
  9. Linux SvN操作
  10. 【Java学习笔记之八】java二维数组及其多维数组的内存应用拓展延伸
  11. springboot项目利用Swagger2生成在线接口文档
  12. 论文笔记:SiamRPN++: Evolution of Siamese Visual Tracking with Very Deep Networks
  13. [Codeforces671D]Roads in Yusland
  14. 用postcss cli运行postcss
  15. 删除最少字符生成Palindrome
  16. 为什么重写equals还要重写hashcode??
  17. Visual Studio 2019 preview中体验C# 8.0新语法
  18. Android Studio Error:Connection timed out: connect.解决方案
  19. LA 3135 阿格斯(优先队列)
  20. Hive环境的安装部署(完美安装)(集群内或集群外都适用)(含卸载自带mysql安装指定版本)

热门文章

  1. 使用MyBatis在控制台动态打印执行的sql语句
  2. Scrum是脆弱的,不敏捷的
  3. 【题解】危险的工作-C++
  4. 细说Ansible主机清单inventory
  5. redis 发布与订阅原理分析
  6. Excel催化剂开源第35波-图片压缩及自动旋转等处理
  7. TensorFlow笔记-文件读取
  8. pyqt QT设计师制作关于对话框(软件版权申明)
  9. 非controller层获取response和request对象
  10. Python基础之用户交互、流程控制、循环语句