EzPooler

PRs Welcome License: MIT

EzPooler is a Unity package that provides an efficient way to manage object pooling in your game.

View in DocFx

Features

  • Efficient object pooling to improve performance.
  • Easy-to-use methods for spawning and despawning GameObjects.
  • Support for pre-instantiating and caching GameObjects in the Unity Editor for efficient use during gameplay.

Installation

Inside the Unity Editor using the Package Manager:

  • Click the (+) button in the Package Manager and select "Add package from Git URL" (requires Unity 2019.4 or later).
  • Paste the Git URL of this package into the input box: https://github.com/ebukaracer/EzPooler.git#upm
  • Click Add to install the package.
  • If your project uses Assembly Definitions, make sure to add a reference to this package under Assembly Definition References.

Quick Usage

Attach PoolManager.cs to the GameObject that will be responsible for spawning objects, then in you ExampleUsage.cs script:

using Racer.EzPooler.Core;
using UnityEngine;

public class ExampleUsage : MonoBehaviour
{
    [SerializeField] private GameObject prefab;
    private PoolManager _poolManager;

    private void Start()
    {    
	    // Assuming this script is sitting on the gameobject
        _poolManager = GetComponent<PoolManager>();

        // Spawn an object
        var spawnedObject = _poolManager.SpawnObject(Vector3.zero, Quaternion.identity);

        // Despawn the object after 2 seconds
        spawnedObject.InvokeDespawn(2f);
    }
}

Example Use Case

Suppose you intend to spawn objects like missiles, barrels, crates, and bottles. You can organize your setup by creating separate GameObjects to hold the respective spawner scripts:

  • MissileSpawnerMissileSpawner.cs
  • BarrelSpawnerBarrelSpawner.cs
  • CrateSpawnerCrateSpawner.cs
  • BottleSpawnerBottleSpawner.cs

Attach the PoolManager.cs script to each of these GameObjects. Then, implement the specific spawning logic within each corresponding script, as shown above.

Samples and Best Practices

  • Optionally import this package's demo from the package manager's Samples tab.
  • To remove this package completely(leaving no trace), navigate to: Racer > EzPooler > Remove package

Contributing

Contributions are welcome! Please open an issue or submit a pull request.