ts版本的简易对象池 ,目前主要支持3D的物体,也可以将其改成其他类型
要使用首先调用InitPool 方法
`/*
使用说明: 使用必须先调用 InitPool 方法将对象池初始化
然后 DequeueItem 会根据对象池指定的编号 返回编号指定对象池中的一个物体,然后在外部进行信息设置
EnqueueItem 调用对象回池,根据指定编号放置到对应的对象池中
ClearPool 清理对象池,将指定对象池中的物体放到Laya默认的内存回收机制中,等待回收
备注:
使用时需将某一类对象池与该对象池对应的编号对应好,否则使用时会出问题
*/
export default class PoolManager {
static _instance:any;
private node:Laya.Node; //3D物体父节点
public m_ObjectPoolDic:{[key:number]: Laya.MeshSprite3D}={}
constructor() { this. node=new Laya.Node(); this. node.name="Node"; Laya.stage.addChild(this.node);}
static getInstance() {
return this._instance || (this._instance = new PoolManager() )
}
//初始化
//加载到内存中创建出来之后添加到这里面
public InitPool(key:number,MeshSprite: Laya.MeshSprite3D){
//初始化id对应对象池, 第一次初始化则必须保证池中最少存在1个
if (this.m_ObjectPoolDic[key]){
console.log("初始化有误,该值已经有对应的 对象池了");
}
else{
this.m_ObjectPoolDic[key]=(MeshSprite);
let childNode=new Laya.Node();
childNode.name=key.toString();
this.node.addChild(childNode)
}
}
//按照编号取出对应对象池中的一个物体
public DequeueItem(key:number): Laya.MeshSprite3D {
if(this.m_ObjectPoolDic[key])
{
let childNode=this.node.getChildByName(key.toString())
if(childNode.numChildren==0){
let tempobj= this.m_ObjectPoolDic[key].clone() as Laya.MeshSprite3D;;
tempobj.name=key.toString();
return tempobj;
}
else{
let tempobj=childNode.getChildAt(0)as Laya.MeshSprite3D; ;
tempobj.removeSelf();
return tempobj ;
}
}else
{
//对象池中不存在该 key 对应的池
console.log("获取对象错误,没有 该值所对应的对象池");
return null;
}
}
//对象回池,对象的状态信息,尽可能在回池前设置成默认状态
// 比如说物体上添加的一些脚本,可以在回池前去掉或者 后面再对脚本进行判断
//需要记住该物体对象池所对应的指定的编号,否则一旦添加错误,无法修改
public EnqueueItem(key:number,MeshSprite:Laya.MeshSprite3D):void{
if(this.m_ObjectPoolDic[key])
{
//对象池中存在该 key 对应的池
let childNode=this.node.getChildByName(key.toString())
MeshSprite.active=false;
MeshSprite.removeSelf();
childNode.addChild(MeshSprite);
}else
{
//对象池中不存在该 key 对应的池
console.log("获取对象错误,没有 该值所对应的对象池");
return;
}
}
//清理对象池 根据编号清理指定的对象池
public ClearPool(key:number):void{
if(this.m_ObjectPoolDic[key])
{
//对象池中存在该 key 对应的池
let QueuePool=this.m_ObjectPoolDic[key];
this.m_ObjectPoolDic[key].destroy();
delete this.m_ObjectPoolDic[key];
let childNode=this.node.getChildByName(key.toString())
childNode.destroyChildren();
childNode.destroy();
}else
{
//对象池中不存在该 key 对应的池
console.log("获取对象错误,没有 该值所对应的对象池");
return ;
}
}
//获取对象池长度 根据编号获取指定的对象池的长度
public GetPoolLenght(key:number):number{
if(this.m_ObjectPoolDic[key])
{
//对象池中存在该 key 对应的池
let childNode=this.node.getChildByName(key.toString())
return childNode.numChildren;
}else
{
//对象池中不存在该 key 对应的池
console.log("获取对象错误,没有 该值所对应的对象池");
return null;
}
}
}
使用方法:
var Cube:Laya.MeshSprite3D=new Laya.MeshSprite3D(Laya.PrimitiveMesh.createBox(1, 1, 1)) as Laya.MeshSprite3D;
PoolManager.getInstance().InitPool(1,Cube); //这一步必须进行
for (let i=0 ;i<10;i++){
var CubeSwpan=scene.addChild( PoolManager.getInstance().DequeueItem(1)) as Laya.MeshSprite3D;
CubeSwpan.transform.localPosition=new Laya.Vector3 (-i+5,0,-i+5);
this. List[i]=CubeSwpan;
}
debugger;
console.log(PoolManager.getInstance().GetPoolLenght(1));
for (let i=0 ;i<10;i++){
let Cube= this.List[i] as Laya.MeshSprite3D;
Cube.active=false;
PoolManager.getInstance().EnqueueItem(1,Cube);
}
debugger;
console.log(PoolManager.getInstance().GetPoolLenght(1));
}
List ={key:Laya.MeshSprite3D};`