60 lines
1.2 KiB
TypeScript
60 lines
1.2 KiB
TypeScript
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();
|
||
|
|
}
|
||
|
|
}
|