当前位置:网站首页>CocosCreator+TypeScripts自己写一个对象池
CocosCreator+TypeScripts自己写一个对象池
2022-07-06 14:48:00 【卡西莫多说】
为什么需要对象池?
在运行时进行节点的创建(
cc.instantiate)和销毁(node.destroy)操作是非常耗费性能的,因此我们在比较复杂的场景中,通常只有在场景初始化逻辑(onLoad)中才会进行节点的创建,在切换场景时才会进行节点的销毁。如果制作有大量敌人或子弹需要反复生成和被消灭的动作类游戏,我们要如何在游戏进行过程中随时创建和销毁节点呢?
很多人都会去官方文档去查看一下,有没有什么办法解决?官方文档关于对象池的链接:
使用对象池 · Cocos Creator
https://docs.cocos.com/creator/2.3/manual/zh/scripting/pooling.html?q=那么这次为大家介绍一下自己写的对象池:PoolMgr.ts
定义一个对象,将初始化的对象放到里面:
public static _dictPool: Object = {}初始化hui对象池:
/**
* 初始化对象池
* @param count 生成的数量
* @param prefab 生成为节点的预制体
*/
public static initPool(count:number,prefab:cc.Prefab){
for (let index = 0; index < count; index++) {
let node:cc.Node = cc.instantiate(prefab);
this.setNode(node);
}
}这个是自己写的初始化,一般在加载界面将对象初始化好,在游戏中就可以直接调用。
获取对象池中的某个对象:
/**
* 获取节点
* @param prefab 生成为节点的预制体
* @param parent 该节点的父节点
* @returns 生成或者对象池中的节点
*/
public static getNode(prefab:cc.Prefab,parent:cc.Node){
let name = prefab.name;
let node:cc.Node = null;
if(this._dictPool[name]){
let pool = this._dictPool[name];
if (pool && pool.size() > 0) {
node = pool.get();
} else {
node = cc.instantiate(prefab);
}
}
else{
let pool = new cc.NodePool();
this._dictPool[name] = pool;
node = cc.instantiate(prefab);
}
node.parent = parent;
return node;
}回收某个对象:
/**
* 回收节点
* @param node 回收的节点
*/
public static setNode(node:cc.Node){
const name = node.name;
let pool = null;
if(this._dictPool[name]){
pool = this._dictPool[name];
}
else{
pool = new cc.NodePool();
this._dictPool[name] = pool;
}
if(pool.size() > 100){
node.destroy();
return;
}
pool.put(node);
}使用示例:
let count:number = 10;
let path:string = "xx";
sc.load.LoadPrefab(path,(prefab:cc.Prefab)=>{
PoolMgr.initPool(count,prefab);
});加载资源时调用初始化接口,这样就初始化了10个对应路径的预制体;
设置:
let node = PoolMgr.getNode(prefab,parent);获取预制体,设置父节点,即可获得该节点;
回收:
PoolMgr.setNode(node);回收节点时注意:如果对这个节点做了什么改变,比如它的旋转、缩放等,注意在再次使用时它的属性。
边栏推荐
- 雅思口语的具体步骤和时间安排是什么样的?
- HDR image reconstruction from a single exposure using deep CNNs阅读札记
- [sciter]: encapsulate the notification bar component based on sciter
- 新手程序员该不该背代码?
- Oracle-控制文件及日志文件的管理
- Seata aggregates at, TCC, Saga and XA transaction modes to create a one-stop distributed transaction solution
- 414. The third largest digital buckle
- Force deduction question 500, keyboard line, JS implementation
- Data processing skills (7): MATLAB reads the data in the text file TXT with mixed digital strings
- Netxpert xg2 helps you solve the problem of "Cabling installation and maintenance"
猜你喜欢

Aardio - 通过变量名将变量值整合到一串文本中

将MySQL的表数据纯净方式导出

自制J-Flash烧录工具——Qt调用jlinkARM.dll方式

Netxpert xg2 helps you solve the problem of "Cabling installation and maintenance"

C#實現水晶報錶綁定數據並實現打印4-條形碼

Common sense: what is "preservation" in insurance?

Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices

Aardio - 利用customPlus库+plus构造一个多按钮组件
Learn the principle of database kernel from Oracle log parsing

Management background --1 Create classification
随机推荐
C # réalise la liaison des données du rapport Crystal et l'impression du Code à barres 4
MySQL约束的分类、作用及用法
Management background --2 Classification list
Netxpert xg2 helps you solve the problem of "Cabling installation and maintenance"
基于 QEMUv8 搭建 OP-TEE 开发环境
3DMAX assign face map
Senior soft test (Information System Project Manager) high frequency test site: project quality management
变量与“零值”的比较
Leetcode exercise - Sword finger offer 26 Substructure of tree
How do I write Flask's excellent debug log message to a file in production?
Crawler obtains real estate data
2021 geometry deep learning master Michael Bronstein long article analysis
柔性数组到底如何使用呢?
小常识:保险中的“保全”是什么?
Seata aggregates at, TCC, Saga and XA transaction modes to create a one-stop distributed transaction solution
Report on technological progress and development prospects of solid oxide fuel cells in China (2022 Edition)
如何用程序确认当前系统的存储模式?
3DMax指定面贴图
剑指offer刷题记录1
Force deduction question 500, keyboard line, JS implementation