Options
All
  • Public
  • Public/Protected
  • All
Menu

Class NodePool

NodePool 是用于管理节点对象的对象缓存池。
它可以帮助您提高游戏性能,适用于优化对象的反复创建和销毁
以前 cocos2d-x 中的 pool 和新的节点事件注册系统不兼容,因此请使用 NodePool 来代替。

新的 NodePool 需要实例化之后才能使用,每种不同的节点对象池需要一个不同的对象池实例,这里的种类对应于游戏中的节点设计,一个 prefab 相当于一个种类的节点。
在创建缓冲池时,可以传入一个包含 unuse, reuse 函数的组件类型用于节点的回收和复用逻辑。

一些常见的用例是:
1.在游戏中的子弹(死亡很快,频繁创建,对其他对象无副作用)
2.糖果粉碎传奇中的木块(频繁创建)。 等等....

Hierarchy

  • NodePool

Index

Constructors

Properties

Methods

Constructors

constructor

  • en

    Constructor for creating a pool for a specific node template (usually a prefab). You can pass a component (type or name) argument for handling event for reusing and recycling node.

    zh

    使用构造函数来创建一个节点专用的对象池,您可以传递一个组件类型或名称,用于处理节点回收和复用时的事件逻辑。

    example

    import { NodePool, Prefab } from 'cc'; properties: { template: Prefab }, onLoad () { // MyTemplateHandler is a component with 'unuse' and 'reuse' to handle events when node is reused or recycled. this.myPool = new NodePool('MyTemplateHandler'); } }

    Parameters

    • Optional poolHandlerComp: Constructor<IPoolHandlerComponent> | string

      @en The constructor or the class name of the component to control the unuse/reuse logic. @zh 处理节点回收和复用事件逻辑的组件类型或名称。

    Returns NodePool

Properties

Private _pool

_pool: Node[]

Optional poolHandlerComp

poolHandlerComp: Constructor<IPoolHandlerComponent> | string

缓冲池处理组件,用于节点的回收和复用逻辑,这个属性可以是组件类名或组件的构造函数。

Methods

clear

  • clear(): void

get

  • get(...args: any[]): Node | null
  • 获取对象池中的对象,如果对象池没有可用对象,则返回空。 这个函数会调用 poolHandlerComp 的 reuse 函数,如果组件和函数都存在的话。

    example

    let newNode = this.myPool.get();

    Parameters

    • Rest ...args: any[]

      向 poolHandlerComp 中的 'reuse' 函数传递的参数

    Returns Node | null

put

  • put(obj: Node): void
  • 向缓冲池中存入一个不再需要的节点对象。 这个函数会自动将目标节点从父节点上移除,但是不会进行 cleanup 操作。 这个函数会调用 poolHandlerComp 的 unuse 函数,如果组件和函数都存在的话。

    example

    import { instantiate } from 'cc'; const myNode = instantiate(this.template); this.myPool.put(myNode);

    Parameters

    Returns void

size

  • size(): number

Generated using TypeDoc