npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

com.jeffbert.unity-component-pool

v1.0.10

Published

Pooling system designed to work with Unity components. Reduces memory allocations by reusing disabled components.

Downloads

54

Readme

Unity Component Pool

Description

Pooling system designed to work with Unity components.

Installation

Install with Unity's Package Manager or by copying the repository's contents in your project.

Usage

Using the component pool essentially comes down to replacing Object.Instantiate calls with ComponentPool.Get. Component instances (created with ComponentPool.Get) are pooled by deactivating their corresponding game objects, allowing them to be reused on subsequent ComponentPool.Get calls.

Example

[SerializeField]
MyComponent sourceComponent;

MyComponent _instance;

void Awake()
{
   // Retrieve pooled instance. A new instance will be created if the source component's pool is empty.
   _instance = ComponentPool.Get(sourceComponent);
}

void Start()
{
   _instance.DoSomething();

   // Pool component instance by deactivating its game object.
   _instance.gameObject.SetActive(false);
}

Initializing instances

Component instances can be initialized with the ComponentPoolInitializer component. It's important to specify the component type that you want to pool, since pools are created per source component (and not per game object).

Inspector

You can change the component that will be pooled by clicking the gear button.

Selector

Different types of components can be initialized within the same ComponentPoolInitializer component.

Source Component

API

ComponentPool.Get

Get an instance of the source. A new instance will be created if the pool is empty.

[SerializeField]
MyComponent source;

void Awake()
{
   MyComponent instance = ComponentPool.Get(source, transform.position, transform.rotation);
}

ComponentPool.GetMany

Gets many instances in a single call.

ICollection: Adds the specified amount of instances to the collection.

[SerializeField]
MyComponent source;

private List<MyComponent> _instances = new List<MyComponent>();

void Awake()
{
   ComponentPool.GetMany(_instances, source, 10, transform.position, transform.rotation);
}

Array: Replaces all elements of the array with instances.

[SerializeField]
MyComponent source;

private MyComponent[] _instances = new MyComponent[10];

void Awake()
{
   ComponentPool.GetMany(_instances, source, transform.position, transform.rotation);
}

ComponentPool.DestroySourceInstances

Destroys all component instances of the specified source and clears the source's pool.

[SerializeField]
MyComponent source;

[SerializeField]
MyComponent otherSource;

void Awake()
{
   MyComponent instance = ComponentPool.Get(source);
   MyComponent otherInstance = ComponentPool.Get(otherSource);

   ComponentPool.DestroySourceInstances<MyComponent>(source);

   Debug.Log(instance == null);        // True
   Debug.Log(otherInstance == null);   // False
}

ComponentPool.DestroyInstances<T>

Destroys all component instances of the specified T type and clears all T pools.

[SerializeField]
MyComponent source;

[SerializeField]
MyComponent otherSource;

void Awake()
{
   MyComponent instance = ComponentPool.Get(source);
   MyComponent otherInstance = ComponentPool.Get(otherSource);

   ComponentPool.DestroyInstances<MyComponent>();

   Debug.Log(instance == null);        // True
   Debug.Log(otherInstance == null);   // True
}

Misc