大量被put到对象池后,性能效率极低,求指点

  • Creator 版本: 2.2.0

背景:
       在角色发射子弹测试负荷时(子弹拥有BoxCollider组件),将发射子弹频率调到极高(0.2s左右),发射单位每5秒新增一个,运行3分钟以后子弹的大量产生,造成特别卡屏。

分析:
       用谷歌浏览器的性能分析时,分现put到对象池后,会移除结点,调用一系列的内部函数,到最后调用removeColider也是占用最主要时间的地方。

期望解决方案:
       为了改善性能,有没有办法put到对象池时,不调用removeCollider。 或者其它方案?

占用率非常高

幕后主使

展开方法调用


代码简单说明:
       1,PoolManager new 或 get bullet。
       2,Weapon 从 PoolManager 得到 Bullet.
       3,Bullet 碰撞后put到PoolManager 的 pool到。Bullet继承于FireObject。

PoolManager.js
var PoolManager = cc.Class({
});

// 使用pool的put 会移除结点,removeCollider ,性能大大下降
PoolManager._bulletPool = new cc.NodePool(“Bullet”);

PoolManager.get = function(prefab){
var obj = null;
// console.log(prefab.data);
if(prefab.data.name == “bullet”){
if(this._bulletPool.size() > 0){
obj = this._bulletPool.get(this._bulletPool);
}else{
obj = cc.instantiate(prefab);
PoolManager.newCount++;
// _pool 为了后面的put用
obj.getComponent(“FireObject”)._pool = this._bulletPool;
// console.log(“this._bulletPool”);
// console.log(this._bulletPool);
}
}
return obj;
};
PoolManager.newCount = 0;

Weapon.js
Fire(){
//取得bullet
var obj = PoolManager.get(this.prefab);

obj.setPosition(new_w_pos);
scene.addChild(obj);

Bullet.js
onCollisionEnter: function (other, self) {
this._pool.push(this.node);
}

请各位大神指教指教!!

1赞

重写put方法 put进对象池的话 就把节点放到屏幕外然后透明度设置为0

谢谢,我想到的也只有这一种方法,还有其它方案吗?
该不会这是目前主流解决方案吧?

你先对比一下直接销毁重创建的性能?节点添加/active/改透明度 都可能导致性能有一定的损耗。

因为有3000+的sprite,可以优化办法是消耗内存来平衡性能,会重用的资源不释放,直接透明度为0,挪到屏幕外,用的时候使用map索引取到

销毁重创建也会调用removeCollider的。

谢谢,看来大家都是这样处理的,那我也跟随主流了,反正目前也没有更好办法了。