Giter Site home page Giter Site logo

gilzoide / unity-prefab-pool Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 0.0 155 KB

Prefab instance pool that is configurable in the Inspector, supports any engine Object type and is available as a serializable C# class, MonoBehaviour and ScriptableObject

License: The Unlicense

C# 100.00%
object-pool object-pooling package plugin prefab unity unity3d upm-package

unity-prefab-pool's Introduction

Prefab Pool

openupm

Prefab instance pool that is configurable in the Inspector, supports any engine Object type and is available as a serializable C# class, MonoBehaviour and ScriptableObject.

Features

  • Prefab pools may live either as project assets (PrefabPoolAsset), standalone components in the scene (PrefabPoolComponent), or may be a part of your own scripts (PrefabPool), whichever fits best your use case.
  • Supports prewarming instances: configure pools to instantiate a number of prefabs when created, with an optional limit of objects per frame to avoid spikes in CPU usage.
  • Supports generic typing for customizing which prefabs can be assigned to the pool. By default, the GameObject type is used in the non-generic prefab pool classes.
  • Optionally add scripts that implement IPooledObject to the prefab for receiving callbacks when object is taken from/returned to pool. With this, you can also easily return the prefab to the pool by calling the IPooledObject.ReturnToPool() extension method.
  • Debug panel in inspector that shows all active and inactive pooled instances.
  • Addressables support: just use the AddressablePrefabPool* variants instead of the PrefabPool* ones and setup the prefab's asset reference in the Inspector. Addressables are automatically loaded when the first instance is created and released when the pool is disposed of.

How to install

Either:

  • Use the openupm registry and install this package using the openupm-cli:
    openupm add com.gilzoide.prefab-pool
    
  • Install using the Unity Package Manager with the following URL:
    https://github.com/gilzoide/unity-prefab-pool.git#1.0.0-preview3
    
  • Clone this repository or download a snapshot of it directly inside your project's Assets or Packages folder.

Usage example

using System.Collections;
using Gilzoide.PrefabPool;
using UnityEngine;

public class MyScript : MonoBehaviour
{
    // 1) Reference a prefab pool:

    // 1.a) Reference for a PrefabPoolComponent from your scene.
    //      Set this in the Inspector.
    public PrefabPoolComponent myPoolComponent;
    
    // 1.b) Reference for a PrefabPoolAsset from your project.
    //      Set this in the Inspector.
    public PrefabPoolAsset myPoolAsset;
    
    // 1.c) Private/embedded PrefabPool, needs manual prewarm/disposal.
    //      Configure it in the Inspector.
    public PrefabPool myPoolVariable;


    void Start()
    {
        // 2) (optional) Prewarm private/embedded pools.
        //    Passing "instancesPerFrame: N" makes instances be
        //    created frame by frame, avoiding CPU spikes.
        myPoolVariable.Prewarm(10, instancesPerFrame: 1);
        //    Component/asset pools prewarm automatically in their `OnEnable`.

        for (int i = 0; i < 10; i++)
        {
            StartCoroutine(UseInstanceFromPool(myPoolComponent));
            StartCoroutine(UseInstanceFromPool(myPoolAsset));
            StartCoroutine(UseInstanceFromPool(myPoolVariable));
        }
    }

    // 3) Use the pool:
    //    (All prefab pool types implement IPrefabPool<>)
    public IEnumerator UseInstanceFromPool(IPrefabPool<GameObject> pool)
    {
        // 3.1) Get an instance.
        GameObject instance = pool.Get();
        
        // 3.2) Do something with your instance.
        yield return new WaitForSeconds(1);
        
        // 3.3) Return instance to pool after done with it.
        pool.Release(instance);
    }

    // 4) Dispose of private/embedded pools.
    //    Embedded pools should always be disposed when not needed anymore!
    //    Component/asset pools dispose automatically in their `OnDisable`.
    void OnDestroy()
    {
        myPoolVariable.Dispose();
    }
}

Using generic prefab pools

Specifying the prefab type for PrefabPool<>

To customize the prefab type accepted by a prefab pool, just declare your variable with a concrete version of the PrefabPool<> class.

using Gilzoide.PrefabPool;
using UnityEngine;

public class MyScript : MonoBehaviour
{
    public PrefabPool<Transform> myTransformPool;

    void OnDestroy()
    {
        myTransformPool.Dispose();
    }
}

Inspector showing "myTransformPool" expecting an object of type "Transform"

Specifying the prefab type for PrefabPoolComponent<>

To customize the prefab type accepted by a prefab pool component, create a concrete class that inherits PrefabPoolComponent<>:

using Gilzoide.PrefabPool;

public class MyScriptPoolComponent : PrefabPoolComponent<MyScript>
{
}

Inspector showing a prefab pool component expecting a prefab of type "MyScript"

Specifying the prefab type for PrefabPoolAsset<>

To customize the prefab type accepted by a prefab pool asset, create a concrete class that inherits PrefabPoolAsset<>:

using Gilzoide.PrefabPool;
using UnityEngine;

[CreateAssetMenu(menuName = "MyScriptPoolAsset")]
public class MyScriptPoolAsset : PrefabPoolAsset<MyScript>
{
}

Inspector showing a prefab pool asset expecting a prefab of type "MyScript"

Similar projects

Why another implementation?

The reason I made a brand new implementation is because alternatives either:

  • Use singleton/static pools without explicit lifetimes
  • Don't support specifying generic prefab types, accepting only GameObjects
  • Don't have Inspector-ready serializable pool types, being too code oriented
  • Don't support pools as assets using ScriptableObjects
  • Don't support asynchronous prewarming with configurable batch size, potentially leading to CPU spikes while instantiating prefabs
  • Don't support Addressable asset references

unity-prefab-pool's People

Contributors

gilzoide avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.