当前位置:网站首页>打造基于ILRuntime热更新的组件化开发
打造基于ILRuntime热更新的组件化开发
2022-07-31 04:42:00 【Clank的游戏栈】
打造基于ILRuntime的组件化开发
上一节我们详细的讲解了ILRuntime游戏项目框架的启动过程,以及进入到热更项目中的入口,为我们做框架打下了扎实的基础,逻辑热更项目是用C#来完成的,所以我们在逻辑热更项目这里最大限度的保证开发与普通的Unity C#没有太大的差别,所以今天我们来设计基于逻辑热更项目的组件化开发机制,我们叫它ILRBehaviour,类似与MonoBehaviour,尽量保证所有的开发习惯与MonoBehaviour很像,由于MonoBehaviour是Unity C# 域的数据对象类型,所以我们在逻辑热更项目中无法直接使用,所以我们要自己设计一套类式的机制,主要完成3件事情:
这里有个游戏开发交流小组 大家可以去领源码素材 一起学习交流

设计ILRBehaviour接口
设计ILRBehaviour接口主要是和MonoBehaviour保持一致,MonoBehaviour常用的开发习惯主要是接口+gameObject+transform对象,后面我们在做好物理引擎的碰撞检测接口等。关于MonoBehaviour的定时器,我们后续单独设计一个定时器模块来做定时器,不去模拟MonoBehaviour的定时器机制。有了这样的考虑后,我们的ILRBehaviour的数据成员与接口就可以设计出来了,
如下:
class ILRBehaviour {
public GameObject gameObject; // 模拟组件实例.gameObject;
public Transform transform; // 模拟组件实例.transform;
virtual public void Awake() {
}
virtual public void Start() {
}
virtual public void Update() {
}
virtual public void LateUpdate() {
}
virtual public void FixedUpdate() {
}
virtual public void onDestroy() {
}
}这里和MonoBehaviour不一样的是,直接把基类的接口做成虚函数,而不是像MonoBehaviour一样通过反射查找看子类是否有这个方法。所以子类重载接口函数的时,加上override关键字。
添加,查找,删除ILRBehaviour组件实例机制
Unity 开发习惯里面是GameObject类来添加组件,这里GameObject是一个natvie c#对象,所以必须要想其它的办法,这里我加了一个ILRBeahviourMgr的全局单例,用来做ILRBehaviour组件实例的添加,查找,删除。
ILRBehaviour AddILRComponent(GameObject gameObject, Type classType)
public T AddILRComponent<T>(GameObject gameObject) where T : new()
这个原理是如何实现的呢?在Unity C# GameObject中所有的MonoBehaviour组件是”挂”在GameObject节点实例上的,但是对于逻辑热更项目而言,GameObject是一个nativie C# 对象,不能直接记录到上面,我们这里设计是在ILRBehaviourMgr这里建立一个字典Dictionary<int, List<ILRBheaviour>>behaviourMap, 每个GameObject有个接口GetInstanceID,可以获取它唯一的ID号,根据这个ID号,可以获取GameObject上所有的ILRBehaviour的List, 当我们向一个gameObject添加一个ILRBehaviiour组件实例是,首先new 一个ILRBehaviour对象实例,然后根据GameObject的ID到behaviourMap里面去获取List<ILRBehaviour>对象,然后将新new 出来的ILRBehaviour实例放入到List中。这样就完成了给GameObject对象添加一个ILRBehaviour组件对象实例,接下来初始化ILRBehaviour的gameObject与transform数据成员,方便开发者访问, 最后调用组件实例的Awake与Start函数。详细的过程如图1.4-1,

图1.4-1: AddILRComponent流程详解
其它的查找,删除接口也类似,我这里就不一一分析了,大家可以对着源码查看。
让ILRBehaviour的特定接口在特定时机被调用
设计完添加,查找,删除ILRBehaviour组件的接口后,接下来就是要让ILRBehaviour组件机制中特定的接口在特定的时期被调用。目前热更新的特定时期,都是从Unity C#中来的,并且入口在main.cs 中,所以我们要让ILRBehaviour组件中的特定接口能调用到,就可以从main.cs 的特定接口开始,让他们调用ILRBehaviourMgr中的特定接口,然后在IlRBehaviourMgr中,我们遍历behaviourMap中的每个gameObject, 找到属于它的ILRBehaviour的List数组列表,遍历里面的每个ILRBehaviour组件实例,调用特定的接口就可以了,接下来我们以update为例,来分析:

整个过程,我整理在如图1.4-2中。

图1.4-2: 触发ILRBehaviour.Update调用全过程
实现完这些机制以后,我们就完整的设计了一套基于ILRBehaviour的组件化开发机制,它尽可能的复合之前Unity c#的Mono的开发习惯,这样其它小伙伴就可以无缝的来在热更项目中做开发。最后上一个架构图,加深一下印象。

图1.4-3: ILRBehaviour.机制设计架构
今天的分享就到这里了, 可以去学习小组公告,可以获取我们的ILRuntime热更教程源码, 对着源码看更酸爽。
边栏推荐
- Redis uses sorted set to cache latest comments
- Recursive implementation of the Tower of Hanoi problem
- 强化学习:从入门到入坑再到拉屎
- VScode+ESP32快速安装ESP-IDF插件
- [Linear Neural Network] softmax regression
- 扫雷小游戏——C语言
- (八)Math 类、Arrays 类、System类、Biglnteger 和 BigDecimal 类、日期类
- MySQL基础操作
- C language confession code?
- 聚变云原生,赋能新里程 | 2022开放原子全球开源峰会云原生分论坛圆满召开
猜你喜欢

论治理与创新 | 2022开放原子全球开源峰会OpenAnolis分论坛圆满召开

C语言从入门到如土——数据的存储

Minesweeper game - C language

MySQL数据库备份

LocalDate addition and subtraction operations and comparison size

Understanding and Using Unity2D Custom Scriptable Tiles (4) - Start to build a custom tile based on the Tile class (below)

"A daily practice, happy water problem" 1331. Array serial number conversion

MySQL database must add, delete, search and modify operations (CRUD)

数字经济时代的开源数据库创新 | 2022开放原子全球开源峰会数据库分论坛圆满召开

STM32HAL library modifies Hal_Delay to us-level delay
随机推荐
Basic knowledge of mysql (2)
扫雷游戏(c语言写)
Port inspection steps - 7680 port analysis - Dosvc service
interprocess communication
Safety 20220718
The BP neural network
三子棋的代码实现
问题7:列表的拼接
C language from entry to such as soil, the data store
pom文件成橘红色未加载的解决方案
open failed: EACCES (Permission denied)
log level and print log note
扫雷小游戏——C语言
Minesweeper game - C language
WeChat applet uses cloud functions to update and add cloud database nested array elements
【小土堆补充】Pytorch学习笔记_Anaconda虚拟环境使用
BUG destroyer!!Practical debugging skills are super comprehensive
Two address pools r2 are responsible for managing the address pool r1 is responsible for managing dhcp relays
$attrs/$listeners
Thinking about data governance after Didi fines