当前位置:网站首页>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);回收节点时注意:如果对这个节点做了什么改变,比如它的旋转、缩放等,注意在再次使用时它的属性。
边栏推荐
- Netxpert xg2 helps you solve the problem of "Cabling installation and maintenance"
- Aardio - 封装库时批量处理属性与回调函数的方法
- Leetcode exercise - Sword finger offer 26 Substructure of tree
- Management background --2 Classification list
- Installation and use of labelimg
- Leetcode question brushing (XI) -- sequential questions brushing 51 to 55
- Mysql database basic operations DML
- 2020 Bioinformatics | GraphDTA: predicting drug target binding affinity with graph neural networks
- Unity3D学习笔记6——GPU实例化(1)
- HDR image reconstruction from a single exposure using deep CNN reading notes
猜你喜欢

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

2021 geometry deep learning master Michael Bronstein long article analysis

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

MySQL数据库基本操作-DML

Notes de développement du matériel (10): flux de base du développement du matériel, fabrication d'un module USB à RS232 (9): création de la Bibliothèque d'emballage ch340g / max232 SOP - 16 et Associa

That's why you can't understand recursion

A Mexican airliner bound for the United States was struck by lightning after taking off and then returned safely

NPDP认证|产品经理如何跨职能/跨团队沟通?

NetXpert XG2帮您解决“布线安装与维护”难题

Aardio - 封装库时批量处理属性与回调函数的方法
随机推荐
剪映+json解析将视频中的声音转换成文本
SQL Server生成自增序号
AI 企业多云存储架构实践 | 深势科技分享
中国1,4-环己烷二甲醇(CHDM)行业调研与投资决策报告(2022版)
Inno setup packaging and signing Guide
NetXpert XG2帮您解决“布线安装与维护”难题
硬件開發筆記(十): 硬件開發基本流程,制作一個USB轉RS232的模塊(九):創建CH340G/MAX232封裝庫sop-16並關聯原理圖元器件
Spatial domain and frequency domain image compression of images
GPS from getting started to giving up (XI), differential GPS
C#實現水晶報錶綁定數據並實現打印4-條形碼
return 关键字
What are the interface tests? What are the general test points?
Shortcut keys in the terminal
General implementation and encapsulation of go diversified timing tasks
Aardio - 不声明直接传float数值的方法
Maximum product of three numbers in question 628 of Li Kou
Oracle-控制文件及日志文件的管理
Force buckle 575 Divide candy
MySQL数据库基本操作-DML
二分图判定