版权申明:

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

概要

  • 在init.cs中:

    • 首先,await到DownloadBundle完毕(即使是异步的)
    • 然后,Game.Hotfix.GotoHotfix()负责启动Hotfix层,进而启动UILogin画面
    • 本节就主要分析UILogin的启动流程,以及其事件处理

先上Hotfix入口代码:

public void LoadHotfixAssembly()
{
Game.Scene.GetComponent<ResourcesComponent>().LoadBundle($"code.unity3d");
GameObject code = (GameObject)Game.Scene.GetComponent<ResourcesComponent>().GetAsset("code.unity3d", "Code"); byte[] assBytes = code.Get<TextAsset>("Hotfix.dll").bytes;
byte[] pdbBytes = code.Get<TextAsset>("Hotfix.pdb").bytes; #if ILRuntime
Log.Debug($"当前使用的是ILRuntime模式");
// ...
#else
Log.Debug($"当前使用的是Mono模式"); this.assembly = Assembly.Load(assBytes, pdbBytes); Type hotfixInit = this.assembly.GetType("ETHotfix.Init");
this.start = new MonoStaticMethod(hotfixInit, "Start"); this.hotfixTypes = this.assembly.GetTypes().ToList();
#endif Game.Scene.GetComponent<ResourcesComponent>().UnloadBundle($"code.unity3d");
} public void GotoHotfix()
{
#if ILRuntime
ILHelper.InitILRuntime(this.appDomain);
#endif
this.start.Run();
}
  • 由于ILRT模式无法调试,所以开发阶段选择Mono模式,下面的分析也是以Mono方式为例的,这也是ET作者建议的工作流程(开发用Mono,发布用ILRT)
  • LoadHotfixAssembly:得到start方法
    • start方法实际就是ETHotfix命名空间下的Init类的Start方法
  • GotoHotfix:执行start方法

ETHotfix.Init.Start():

namespace ETHotfix
{
public static class Init
{
public static void Start()
{
#if ILRuntime
if (!Define.IsILRuntime)
{
Log.Error("Model层是mono模式, 但是Hotfix层是ILRuntime模式");
}
#else
if (Define.IsILRuntime)
{
Log.Error("Model层是ILRuntime模式, Hotfix层是mono模式");
}
#endif try
{
// 注册热更层回调
ETModel.Game.Hotfix.Update = () => { Update(); };
ETModel.Game.Hotfix.LateUpdate = () => { LateUpdate(); };
ETModel.Game.Hotfix.OnApplicationQuit = () => { OnApplicationQuit(); }; Game.Scene.AddComponent<UIComponent>();
Game.Scene.AddComponent<OpcodeTypeComponent>();
Game.Scene.AddComponent<MessageDispatcherComponent>(); // 加载热更配置
ETModel.Game.Scene.GetComponent<ResourcesComponent>().LoadBundle("config.unity3d");
Game.Scene.AddComponent<ConfigComponent>();
ETModel.Game.Scene.GetComponent<ResourcesComponent>().UnloadBundle("config.unity3d"); UnitConfig unitConfig = (UnitConfig)Game.Scene.GetComponent<ConfigComponent>().Get(typeof(UnitConfig), 1001);
Log.Debug($"config {JsonHelper.ToJson(unitConfig)}"); Game.EventSystem.Run(EventIdType.InitSceneStart);
}
catch (Exception e)
{
Log.Error(e);
}
}
}
}
  • 注意:第86行,和上一课的分析类似,也是在初始化阶段通过引发事件来创建UI界面,只不过这次的事件是InitSceneStart

InitSceneStart_CreateLoginUI.Run():

namespace ETHotfix
{
[Event(EventIdType.InitSceneStart)]
public class InitSceneStart_CreateLoginUI: AEvent
{
public override void Run()
{
UI ui = UILoginFactory.Create(); // 注意:这次是调用Hotfix层的UILoginFactory的Create工厂方法来创建UI实体,下面会重点分析
Game.Scene.GetComponent<UIComponent>().Add(ui); // 注意:这次是添加Hotfix层的UIComponent组件,其代码与模型层类似,不再赘述
}
}
}

UILoginFactory.Create():

namespace ETHotfix
{
public static class UILoginFactory
{
public static UI Create()
{
try
{
ResourcesComponent resourcesComponent = ETModel.Game.Scene.GetComponent<ResourcesComponent>();
resourcesComponent.LoadBundle(UIType.UILogin.StringToAB());
GameObject bundleGameObject = (GameObject)resourcesComponent.GetAsset(UIType.UILogin.StringToAB(), UIType.UILogin);
GameObject gameObject = UnityEngine.Object.Instantiate(bundleGameObject); UI ui = ComponentFactory.Create<UI, string, GameObject>(UIType.UILogin, gameObject, false); ui.AddComponent<UILoginComponent>();
return ui;
}
catch (Exception e)
{
Log.Error(e);
return null;
}
}
}
}```
- 注意:
- 这次加载的是从AB包加载UILogin预制体资源,而上节课是从Resources文件夹加载UILoading资源,不一样!
- 虽然通过ComponentFactory.Create创建的Entity还是UI Entity,但该Entity添加的组件和上节课不一样,是“UILoginComponent”! # UILoginComponent事件处理:
```csharp
namespace ETHotfix
{
[ObjectSystem]
public class UiLoginComponentSystem : AwakeSystem<UILoginComponent>
{
public override void Awake(UILoginComponent self)
{
self.Awake();
}
} public class UILoginComponent: Component
{
private GameObject account;
private GameObject loginBtn; public void Awake()
{
ReferenceCollector rc = this.GetParent<UI>().GameObject.GetComponent<ReferenceCollector>();
loginBtn = rc.Get<GameObject>("LoginBtn");
loginBtn.GetComponent<Button>().onClick.Add(OnLogin);
this.account = rc.Get<GameObject>("Account");
} public void OnLogin()
{
LoginHelper.OnLoginAsync(this.account.GetComponent<InputField>().text).Coroutine();
}
}
}
  • 事件处理在UILoginComponent.Awake方法中被注册
  • 在Entity.AddComponent时,AwakeSystem.Awake()->UILoginComponent.Awake方法链会被调用

Unity进阶之ET网络游戏开发框架 03-Hotfix层启动的更多相关文章

  1. Unity进阶之ET网络游戏开发框架 02-ET的客户端启动流程分析

    版权申明: 本文原创首发于以下网站: 博客园『优梦创客』的空间:https://www.cnblogs.com/raymondking123 优梦创客的官方博客:https://91make.top ...

  2. Unity进阶之ET网络游戏开发框架 01-下载、运行

    版权申明: 本文原创首发于以下网站: 博客园『优梦创客』的空间:https://www.cnblogs.com/raymondking123 优梦创客的官方博客:https://91make.top ...

  3. Unity进阶之ET网络游戏开发框架 04-资源打包

    版权申明: 本文原创首发于以下网站: 博客园『优梦创客』的空间:https://www.cnblogs.com/raymondking123 优梦创客的官方博客:https://91make.top ...

  4. Unity进阶之ET网络游戏开发框架 05-搭建自己的第一个Scene

    版权申明: 本文原创首发于以下网站: 博客园『优梦创客』的空间:https://www.cnblogs.com/raymondking123 优梦创客的官方博客:https://91make.top ...

  5. Unity进阶之ET网络游戏开发框架 08-深入登录成功消息

    版权申明: 本文原创首发于以下网站: 博客园『优梦创客』的空间:https://www.cnblogs.com/raymondking123 优梦创客的官方博客:https://91make.top ...

  6. Unity进阶之ET网络游戏开发框架 06-游客登录

    版权申明: 本文原创首发于以下网站: 博客园『优梦创客』的空间:https://www.cnblogs.com/raymondking123 优梦创客的官方博客:https://91make.top ...

  7. Unity进阶之ET网络游戏开发框架 07-修正游客登录的异步BUG

    版权申明: 本文原创首发于以下网站: 博客园『优梦创客』的空间:https://www.cnblogs.com/raymondking123 优梦创客的官方博客:https://91make.top ...

  8. Unity进阶技巧 - 动态创建UGUI

    前言 项目中有功能需要在代码中动态创建UGUI对象,但是在网上搜索了很久都没有找到类似的教程,最后终于在官方文档中找到了方法,趁着记忆犹新,写下动态创建UGUI的方法,供需要的朋友参考 你将学到什么? ...

  9. Unity进阶----Lua语言知识点(2018/11/08)

    国内开发: 敏捷开发: 集中精力加班堆出来第一个版本 基本没啥大的bug 国外开发: 1).需求分析: 2).讨论 3).分模块 4).框架 5).画UML图(类图class function)(e- ...

随机推荐

  1. Linux日志系统分析:rsyslog、syslog和klog

    参考博客: https://blog.csdn.net/lidonghat/article/details/55004280 https://blog.csdn.net/u012247418/arti ...

  2. python通过TimedRotatingFileHandler按时间切割日志

    通过TimedRotatingFileHandler按时间切割日志 线上跑了一个定时脚本,每天生成的日志文件都写在了一个文件中.但是日志信息不可能输出到单一的一个文件中. 原因有二:1.日志文件越来越 ...

  3. 【题解】危险的工作-C++

    Description 给出一个数字N,N<=11.代表有N个人分担N个危险的工作. 每个人对应每个工作,有个危险值 每个人担任其中一项,问每个人危险值相加,最小值是多少. Input 第一行给 ...

  4. Linux下移动图像监测系统——motion的移植及应用

    移动图像监控主系统的开发 移动图像监控的原理方法: 通过获取摄像头图像,比较前后每一帧的图像数据,从而实现移动物体监控.所有移动监控均是如此,只是图像帧的比较算法不同. 移动图像监控系统的实现 选择开 ...

  5. [HDU4135]CO Prime(容斥)

    也许更好的阅读体验 \(\mathcal{Description}\) \(t\)组询问,每次询问\(l,r,k\),问\([l,r]\)内有多少数与\(k\)互质 \(0<l<=r< ...

  6. C语言入门3-C语言概述及数据类型

    一.          计算机程序设计语言 (计算机语言的发展历史) 1.       机器语言 机器语言 二进制代码语言,由  0和1组成的. 特点是:计算机可以直接识别,不需要进行任何的翻译. 2 ...

  7. [leetcode] 55. Jump Game (Medium)

    原题 题目意思即 每一格代表你当前最多能再往后跳几次,从第一格开始,如果能跳到最后一格返回true,反之为false. 思路:用一个下标记录当前最多能跳到哪一格,遍历一遍 --> 如果当前格子不 ...

  8. Docker 工作原理及容器化简易指南

    Docker 非常棒! 它使软件开发者无需担心配置和依赖性,在任何地方打包,发送和运行他们的应用程序.而在与 Kubernetes 相结合后,它使应用集群部署和管理变得更方便.这使得 Docker 深 ...

  9. 《VR入门系列教程》之3---运动追踪与输入设备

    运动追踪设备    第二种可以使人脑相信它真实处于虚拟世界的关键技术就是运动追踪技术,它可以通过追踪头部的运动状态实时更新渲染的场景.这与我们在真实世界中观看周围非常类似.    高速的惯性测量单元( ...

  10. 从国际象棋与象棋的走法差异,再趣说IT人提升能力和增收方式

    之前我写过篇博文,用象棋的思维趣说IT人的职业发展和钱途,发现象棋中的一些思维能应用到我们程序员平时的职业发展中. 当从大学毕业的程序员干个五六年以后,也达到了高级开发的水平,工作环境应该能摆脱动荡, ...