当前位置:网站首页>打造基于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热更教程源码, 对着源码看更酸爽。
边栏推荐
- (8) Math class, Arrays class, System class, Biglnteger and BigDecimal classes, date class
- The BP neural network
- open failed: EACCES (Permission denied)
- Unity2D 自定义Scriptable Tiles的理解与使用(四)——开始着手构建一个基于Tile类的自定义tile(下)
- Industry landing presents new progress | 2022 OpenAtom Global Open Source Summit OpenAtom OpenHarmony sub-forum was successfully held
- 【小土堆补充】Pytorch学习笔记_Anaconda虚拟环境使用
- Safety 20220722
- PCL 计算点云坐标最值及其索引
- qlib自动化quant
- 马斯克对话“虚拟版”马斯克,脑机交互技术离我们有多远
猜你喜欢
Summary of Huawei Distributed Storage FusionStorage Knowledge Points [Interview]
Unity Fighter
递归实现汉诺塔问题
Postgresql 15 source code analysis (5) - pg_control
qlib架构
30 Years of Open Source Community | 2022 Open Atom Global Open Source Summit 30 Years of Open Source Community Special Event Held Successfully
高等数学---第九章二重积分
BUG destroyer!!Practical debugging skills are super comprehensive
Solved (the latest version of selenium framework element positioning error) NameError: name 'By' is not defined
产学研用 共建开源人才生态 | 2022开放原子全球开源峰会教育分论坛圆满召开
随机推荐
MySQL模糊查询可以使用INSTR替代LIKE
ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法
Regarding the primary key id in the mysql8.0 database, when the id is inserted using replace to be 0, the actual id is automatically incremented after insertion, resulting in the solution to the repea
[Swift] Customize the shortcut that pops up by clicking the APP icon
Reinforcement learning: from entry to pit to shit
Open Source Smart Future | 2022 OpenAtom Global Open Source Summit OpenAtom openEuler sub-forum was successfully held
mysql数据库安装(详细)
C# 实现PLC的定时器
open failed: EACCES (Permission denied)
(5) final, abstract class, interface, inner class
产学研用 共建开源人才生态 | 2022开放原子全球开源峰会教育分论坛圆满召开
unity2d game
(八)Math 类、Arrays 类、System类、Biglnteger 和 BigDecimal 类、日期类
LocalDate addition and subtraction operations and comparison size
Recursive implementation of the Tower of Hanoi problem
从零开始,一镜到底,纯净系统搭建除草机(Grasscutter)
MySQL数据库备份
exsl文件预览,word文件预览网页方法
三子棋的代码实现
IDEA common shortcut keys and plug-ins