版权申明:

  • 本文原创首发于以下网站:
  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. Bzoj: 2073 [POI2004]PRZ 题解

    2073: [POI2004]PRZ Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 401  Solved: 296[Submit][Status][D ...

  2. 手动部署EJB于WebLogic

    转载自http://blog.sina.com.cn/s/blog_678530f60100hy6c.html 说是转载,其实是我个人几年前在新浪博客上发表的一篇文章 上一篇说道如何使用Eclipse ...

  3. 解决springboot项目请求出现非法字符问题 java.lang.IllegalArgumentException:Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986

    springboot版本: 2.1.5 最近使用springboot搭建了一个App后台服务的项目,开发接口的时候在本机使用postman工具做请求测试,请求返回一直很正常,但是在前端开发使用h5请求 ...

  4. C#控制台打开VM虚拟机

    添加引用->VixCOM.dll (在vix文件夹下) VixWrapper.cs using System; using System.Collections.Generic; using S ...

  5. 「PowerBI」Tabular Editor 一个对中文世界很严重的bug即将修复完成

    之前介绍过Tabular Editor这款开源工具,对PowerBI建模来说,非常好用,可以极大的增强自动化水平. 详细可查看此文章: 「PowerBI相关」一款极其优秀的DAX建模工具Tabular ...

  6. springboot4自动配置的原理(浅层)

    自动配置的原理(浅层) @Configuration //这是一个配置类 @EnableConfigurationProperties(HttpProperties.class)//启用Configu ...

  7. 从0系统学Android-2.3使用 Intent 在 Activity 之间穿梭

    2.3 使用 Intent 在 Activity 之间穿梭 在上一节中我们已经学会了如何创建一个 Activity 了.对于一个应用程序来说,肯定不可能只有一个 Activity.下面就来学习多个 A ...

  8. C#7.1 新增功能

    连载目录    [已更新最新开发文章,点击查看详细] C# 7.1 是 C# 语言的第一个点版本(更新版本). 它标志着该语言发布节奏的加速. 理想情况下,可以在每个新功能准备就绪时更快推出新功能.  ...

  9. [leetcode] 72. Edit Distance (hard)

    原题 dp 利用二维数组dp[i][j]存储状态: 从字符串A的0~i位子字符串 到 字符串B的0~j位子字符串,最少需要几步.(每一次删增改都算1步) 所以可得边界状态dp[i][0]=i,dp[0 ...

  10. 算法与数据结构基础 - 广度优先搜索(BFS)

    BFS基础 广度优先搜索(Breadth First Search)用于按离始节点距离.由近到远渐次访问图的节点,可视化BFS 通常使用队列(queue)结构模拟BFS过程,关于queue见:算法与数 ...