Skip to main content

NodePool

Important Attention Reminder

Coding Style wiki


NodePool is PoolSystem's simple GameObject object pool component (MonoBehaviour). It manages idle objects with a Queue (FIFO) and supports asynchronous across-frames loading (load balancing), auto growth (Auto Put), and max size limiting (Max Size). Attach it to a scene node via Add Component -> OxGKit -> PoolSystem -> NodePool (that node becomes the pool's container node).

NamespaceOxGKit.PoolSystem
Typepublic class NodePool : MonoBehaviour
SourceNodePool.cs
using OxGKit.PoolSystem;

Attention Across-frames loading is powered by UniTask; when enabled, Initialize creates objects asynchronously in batches — use IsLoadFinished to check completion.

Quick Start

using OxGKit.PoolSystem;
using UnityEngine;

// Assign the scene's NodePool in the Inspector (Source GameObject configured)
public NodePool objPool;

// Manually initialize the pool (with initializeOnStart checked, it auto-initializes on Start)
this.objPool.Initialize();

// Get from the pool with a parent (returns null if the pool is empty and autoPut is off)
var go = this.objPool.Get(this.transform);

// Put back into the pool when done
this.objPool.Put(go);

General Rules

Lifecycle

  1. Initialization: With initializeOnStart (default) checked, Initialize is called automatically on Start; uncheck it to decide the initialization timing yourself.
  2. Get/Put: Take objects out with Get (auto SetActive(true)) and return them with Put when done (auto SetActive(false) and re-parented under the pool node).
  3. Clear: On the component's OnDestroy, Clear is called automatically — it cancels pending loading tasks and destroys pooled objects.

Important Source GameObject (go) must not be null — initializing the pool without a source throws an ArgumentNullException. Also, never Destroy a pooled object yourself — always return it with Put, otherwise the pool shrinks over time.

Resetting Object State

Pooled objects are reusedGet does not reset object state. Reset it yourself in the object's OnEnable or right after taking it out (e.g., velocity, trails, coroutines); never assume a freshly-instantiated state.

Auto-Put Batch Settings

Attention In the current version (v1.0.2), the across-frames auto growth (Auto Put) flow reuses the initDelayFrameAfterSpawnCount setting as its "spawn N objects" batch count (autoDelayFrameAfterSpawnCount is not applied yet), while the delay uses autoPutDelayFrame.

Log Output

This module logs through LoggingSystem under the logger name OxGKit.PoolSystem.Logger, whose toggle and level can be configured there.


Members

MemberDescription
public GameObject goThe source object to pool (Inspector: Source GameObject), required.
public bool initializeOnStartWhether to initialize the pool automatically on Start (default true).
public int initSizeThe initial size of the pool (default 5).
public bool initLoadAcrossFramesEnables across-frames loading during initialization (default true).
public int initDelayFrameAfterSpawnCountDuring initialization, delay after spawning N objects (default 1; values <= 0 are corrected to 1).
public int initDelayFrameNumber of frames to delay per batch during initialization (default 1).
public bool autoPutWhether to auto-create objects when the pool runs dry (default false).
public int autoPutSizeThe increment count for each auto-create operation (default 1).
public bool autoPutLoadAcrossFramesEnables across-frames loading during auto growth (default true).
public int autoDelayFrameAfterSpawnCountDuring auto growth, delay after spawning N objects (default 1; see Auto-Put Batch Settings).
public int autoPutDelayFrameNumber of frames to delay per batch during auto growth (default 1).
public int maxSizeThe pool size cap: 0 or -1 means unlimited; when > 0, both initSize and autoPut growth are restricted (default 0).

Initialization & Management

Method Overview

MethodDescription
InitializeInitializes the pool (clears first, then rebuilds by initSize).
IsLoadFinishedWhether the init or auto-put loading process has finished.
CountCurrent idle object count in the pool.
ClearClears the pool (cancels loading tasks and destroys pooled objects).

Initialize

public void Initialize()

Initializes the pool: calls Clear first, then creates initSize objects into the pool (with maxSize > 0, the count is Min(initSize, maxSize)); with initLoadAcrossFrames enabled, objects are created asynchronously in batches across frames.

Reminder With initializeOnStart checked, this is called automatically on Start; calling it again is equivalent to rebuilding the pool.

// Manually initialize the pool (when initializeOnStart is unchecked)
this.objPool.Initialize();

IsLoadFinished

public bool IsLoadFinished()

Returns whether the init or auto-put loading process has finished. With across-frames loading enabled, check it before moments that need the full pool warm (e.g., before mass spawning).

if (this.objPool.IsLoadFinished())
{
// The pool is ready
}

Count

public int Count()

Returns the current idle object count in the pool (Get decrements it, Put increments it).

int count = this.objPool.Count();

Clear

public void Clear()

Clears the pool: cancels pending loading tasks, destroys all pooled objects, and resets the counter; called automatically on the component's OnDestroy.

Attention Clearing only destroys idle objects in the pool — objects already taken out by Get are unaffected. Mind your shutdown order: never call Put on a pool that has been destroyed.


Get & Put

Method Overview

MethodDescription
GetGets an object from the pool (with optional parent, position, and rotation).
PutReturns an object to the pool (destroyed directly when exceeding maxSize).

Get

public GameObject Get()

public GameObject Get(Transform parent)

public GameObject Get(Transform parent, Vector3 position)

public GameObject Get(Transform parent, Vector3 position, Quaternion rotation)

Gets an object from the pool and automatically calls SetActive(true):

  • No arguments: The object detaches from the pool node (parent set to null).
  • parent: Re-parents the object under the given parent.
  • position: Sets the object's localPosition (local space).
  • rotation: Sets the object's rotation (world space).

When the pool has no available object:

  • With autoPut enabled and autoPutSize > 0 → auto-creates and returns an object (even with across-frames loading enabled, the first object is still created in the same frame and returned immediately).
  • Without autoPut → returns null; the caller must null-check.

Attention position sets localPosition and is affected by the parent's Transform; rotation is in world space.

// Get with a parent
var go = this.objPool.Get(this.transform);
if (go != null)
{
// Use the object...
}

// Get with a parent, local position, and rotation
var go2 = this.objPool.Get(parent, Vector3.zero, Quaternion.identity);

Put

public void Put(GameObject go)

Returns an object to the pool: re-parents it under the pool node and calls SetActive(false); if maxSize > 0 and the pool is full, the object is destroyed directly and a log reminder is printed.

Reminder maxSize counts idle objects in the pool — in-flight objects returned to a full pool get destroyed, so size the cap to peak concurrent usage.

// Put back into the pool when done
this.objPool.Put(go);