/**
-
对象池A
*/
class ObjectPool {
/*存储对象池的Object/
private pool: Object;public constructor() {
this.pool = {};
}/**
- 获取对象
- @className 对象类名
- @args 构造函数传参
*/
public pop(className:string, …args:any[]): any {
if(this.pool[className] == null){
this.pool[className] = [];
}
var list:Array = this.pool[className];
if(list.length > 0) {
return list.pop();
}else{
var clz: any = egret.getDefinitionByName(className);
var argsLen: number = args.length;
var obj: any;
if(argsLen == 0) {
obj = new clz();
} else if(argsLen == 1) {
obj = new clz(args[0]);
} else if(argsLen == 2) {
obj = new clz(args[0],args[1]);
} else if(argsLen == 3) {
obj = new clz(args[0],args[1],args[2]);
} else if(argsLen == 4) {
obj = new clz(args[0],args[1],args[2],args[3]);
} else if(argsLen == 5) {
obj = new clz(args[0],args[1],args[2],args[3],args[4]);
}
obj.className = className;
}
return obj;
}
/**
- 回收对象
- @obj 需要回收的对象
*/
public push(obj: any): void {
var className = obj.className;
if(this.pool[className] == null){
console.log(“回收对象的数组不存在”);
return;
}
this.pool[className].push(obj);
}
/**
- 创建对象(用于将要大量使用前,预先创建,防止使用时大量创建卡顿)
- @className 对象类定义
- @num 创建数量
- @args 构造函数传参
*/
public create(className:string, num:number, …args:any[]){
var list = [];
for(var i=0;i<num;i++){
list.push(this.pop(className,…args));
}
for(i=0;i<num;i++){
this.push(list.pop());
}
}
/**
- 获取对象池对象数量
- @className 对象类定义
- @return 对象数量
*/
public getLen(className:string):number{
if(this.pool[className]){
return this.pool[className].length;
}
return 0;
}
/**
- 清理对象
- @className 对象类定义
* @funName 清理前执行指定函数
*/
public clear(className:string, funName:string = null){
if(this.pool[className]){
funName && this.dealFun(className, funName);
this.pool[className] = null;
delete this.pool[className];
}
}
/**
- 对象池所有对象执行指定函数
- @className 对象类定义
- @funName 函数名
/
public dealFun(className:string , funName:string){
if(this.pool[className]){
var list:Array = this.pool[className];
var len = list.length;
for(var i=0;i<len;i++){
list[i][funName] && list[i]funName;
}
}
}
}
/*
-
对象池B
*/
class ObjectPool {
/*存储对象池的Object/
private static pool: Object = {};
/*存储对象的数组/
private list: Array;
/*对象类型/
private className: string;public constructor(className: string) {
this.className = className;
this.list = [];
}/*获取对象/
public getObject(): any {
if(this.list.length > 0) {
return this.list.pop();
}
var clazz: any = egret.getDefinitionByName(this.className);
return new clazz();}
/回收对象/
public returnObject(value: any): void {
this.list.push(value);
}
/*- 获取对象池,如果不存在则新建一个
- @param className 对象类名
- @param initNum 初始化对象池数量
*/
public static getPool(className: string,initNum: number = 0): ObjectPool {
if(!ObjectPool.pool[className]) {
ObjectPool.pool[className] = new ObjectPool(className);
if(initNum != 0) {
var clazz: any = egret.getDefinitionByName(className);
var pool: ObjectPool = ObjectPool.pool[className];
for(var i: number = 0;i < initNum;i++) {
pool.returnObject(new clazz());
}
}
}
return ObjectPool.pool[className];
}
}
内存优化:
游戏中除了常用的事件监听的移除、对象引用设置为null等等,对于资源加载使用后,也要即时卸载。
(https://www.cnblogs.com/gamedaybyday/p/9095545.html)