imageItem 缓存池
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
import { NodePool, instantiate, Node, isValid } from "cc";
|
||||
import { DetailImageItem } from "../uiitems/DetailImageItem";
|
||||
|
||||
export class DetailImageItemPool {
|
||||
private static _instance: DetailImageItemPool = null;
|
||||
|
||||
private _pool: NodePool;
|
||||
private _template: Node = null;
|
||||
|
||||
private constructor() {
|
||||
this._pool = new NodePool();
|
||||
}
|
||||
|
||||
public static get Instance(): DetailImageItemPool {
|
||||
if (!this._instance) {
|
||||
this._instance = new DetailImageItemPool();
|
||||
}
|
||||
return this._instance;
|
||||
}
|
||||
|
||||
public setTemplate(template: Node): void {
|
||||
this._template = template;
|
||||
}
|
||||
|
||||
public get(): Node {
|
||||
let node: Node = null;
|
||||
|
||||
if (this._pool.size() > 0) {
|
||||
node = this._pool.get();
|
||||
} else {
|
||||
if (this._template) {
|
||||
node = instantiate(this._template);
|
||||
}
|
||||
}
|
||||
|
||||
if (node) {
|
||||
const item = node.getComponent(DetailImageItem);
|
||||
if (item && item.reset) {
|
||||
item.reset();
|
||||
}
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
public put(node: Node): void {
|
||||
if (node && isValid(node)) {
|
||||
node.removeFromParent();
|
||||
this._pool.put(node);
|
||||
}
|
||||
}
|
||||
|
||||
public clear(): void {
|
||||
this._pool.clear();
|
||||
}
|
||||
|
||||
public getPoolSize(): number {
|
||||
return this._pool.size();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "da0fc97b-079e-4f2f-b288-779e5729c21b",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
Reference in New Issue
Block a user