123456789101112131415161718192021222324252627282930313233343536 |
- import { _decorator, Component, instantiate, Node, NodePool, Prefab } from 'cc';
- export class GameObjectPool {
- private _pool: NodePool;
- private _prefab: Prefab;
- constructor(prefab: Prefab, initialSize: number){
- this._prefab = prefab;
- this._pool = new NodePool();
- this.preLoad(initialSize);
- }
- private preLoad(count: number){
- for(let i = 0; i < count; i++){
- const node = instantiate(this._prefab);
- this._pool.put(node);
- }
- }
- //获取节点
- public get(): Node{
- let node: Node;
- if(this._pool.size() > 0){
- node = this._pool.get();
- } else {
- node = instantiate(this._prefab)
- }
- return node;
- }
- public put(node: Node){
- this._pool.put(node);
- }
- }
|