当前位置:网站首页>打造基于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热更教程源码, 对着源码看更酸爽。
边栏推荐
- Open Source Database Innovation in the Digital Economy Era | 2022 Open Atom Global Open Source Summit Database Sub-Forum Successfully Held
- 简易网络文件拷贝的C实现
- 聚变云原生,赋能新里程 | 2022开放原子全球开源峰会云原生分论坛圆满召开
- STM32HAL library modifies Hal_Delay to us-level delay
- Recursive implementation of the Tower of Hanoi problem
- How Zotero removes auto-generated tags
- PWN ROP
- BUG消灭者!!实用调试技巧超全整理
- 论治理与创新 | 2022开放原子全球开源峰会OpenAnolis分论坛圆满召开
- 专访 | 阿里巴巴首席技术官程立:云+开源共同形成数字世界的可信基础
猜你喜欢

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

Industry landing presents new progress | 2022 OpenAtom Global Open Source Summit OpenAtom OpenHarmony sub-forum was successfully held

ERROR 1819 (HY000) Your password does not satisfy the current policy requirements

mysql数据库安装(详细)

SOLVED: After accidentally uninstalling pip (two ways to manually install pip)

Unity2D 自定义Scriptable Tiles的理解与使用(四)——开始着手构建一个基于Tile类的自定义tile(下)

Vue项目通过node连接MySQL数据库并实现增删改查操作
![[C language] General method of base conversion](/img/28/954af5f47a79ff02d3cc0792ac8586.jpg)
[C language] General method of base conversion

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

STM32HAL库修改Hal_Delay为us级延时
随机推荐
errno error code and meaning (Chinese)
three.js make 3D photo album
Understanding of the presence of a large number of close_wait states
interprocess communication
(8) Math class, Arrays class, System class, Biglnteger and BigDecimal classes, date class
open failed: EACCES (Permission denied)
PCL 计算点云坐标最值及其索引
[shell basics] determine whether the directory is empty
【小土堆补充】Pytorch学习笔记_Anaconda虚拟环境使用
高斯分布及其极大似然估计
(八)Math 类、Arrays 类、System类、Biglnteger 和 BigDecimal 类、日期类
Can't load /home/Iot/.rnd into RNG
The BP neural network
Minesweeper game - C language
idea工程明明有依赖但是文件就是显示没有,Cannot resolve symbol ‘XXX‘
input输入框展示两位小数之precision
$parent/$children and ref
Postgresql 15 source code analysis (5) - pg_control
Solved (the latest version of selenium framework element positioning error) NameError: name 'By' is not defined
Two address pools r2 are responsible for managing the address pool r1 is responsible for managing dhcp relays