当前位置:网站首页>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);回收节点时注意:如果对这个节点做了什么改变,比如它的旋转、缩放等,注意在再次使用时它的属性。
边栏推荐
- BarcodeX(ActiveX打印控件) v5.3.0.80 免费版使用
- lora同步字设置
- Assembly and interface technology experiment 5-8259 interrupt experiment
- Chapter 3: detailed explanation of class loading process (class life cycle)
- 3DMAX assign face map
- Clip +json parsing converts the sound in the video into text
- Management background --1 Create classification
- Anaconda installs third-party packages
- 【sdx62】WCN685X将bdwlan.bin和bdwlan.txt相互转化操作方法
- Research and investment strategy report of China's VOCs catalyst industry (2022 Edition)
猜你喜欢
随机推荐
How do I write Flask's excellent debug log message to a file in production?
【雅思口语】安娜口语学习记录part1
每日一题:力扣:225:用队列实现栈
Oracle-控制文件及日志文件的管理
SQL Server生成自增序号
如何用程序确认当前系统的存储模式?
中国1,4-环己烷二甲醇(CHDM)行业调研与投资决策报告(2022版)
新手程序员该不该背代码?
What are the interface tests? What are the general test points?
C#实现水晶报表绑定数据并实现打印4-条形码
NPDP认证|产品经理如何跨职能/跨团队沟通?
GPS from getting started to giving up (12), Doppler constant speed
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
Daily question 1: force deduction: 225: realize stack with queue
That's why you can't understand recursion
signed、unsigned关键字
Self made j-flash burning tool -- QT calls jlinkarm DLL mode
Attack and defense world miscall
Mysql database basic operations DML
C # réalise la liaison des données du rapport Crystal et l'impression du Code à barres 4









