Unclaimed Package Is this your package? Claim it to unlock full analytics and manage your listing.
Claim This Package

Install via UPM

Add to Unity Package Manager using this URL

https://www.pkglnk.dev/maxi-f-objectpool.git

README Markdown

Copy this to your project's README.md

Style
Preview
pkglnk installs badge
## Installation

Add **Object Pool** to your Unity project via Package Manager:

1. Open **Window > Package Manager**
2. Click **+** > **Add package from git URL**
3. Enter:
```
https://www.pkglnk.dev/maxi-f-objectpool.git
```

[![pkglnk](https://www.pkglnk.dev/badge/maxi-f-objectpool.svg?style=pkglnk)](https://www.pkglnk.dev/pkg/maxi-f-objectpool)

README

Simple Object Pool

This package aims to add a simple object pool interface. It uses a Factory interface that is used in the Object Pool to create more objects if needed (This means that this object pool can be expanded).

Usage

  1. Create a Scriptable Object which will be used by the Factory.

Example:

public class NoteConfig : ScriptableObject {
    public string note;
    public float volume;
}
  1. Create a factory, which has to implement the interface IFactory:

Example:

public class NoteFactory : IFactory<NoteConfig>
    {
        private NoteConfig _creationConfig;

        public void SetConfig(NoteConfig config)
        {
            _creationConfig = config;
        }

        public GameObject CreateObject() 
        {
            GameObject instantiatedObject = GameObject.Instantiate();

            // Note in this example is just a script that uses the config
            instantiatedObject.AddComponent<Note>();
            Note note = instantiatedObject.getComponent<Note>();

            note.UseConfig(_creationConfig);

            return instantiatedObject;
        }
    }
  1. Create a class that inherits the object pool, using a scriptable object config and a Factory.

Example:

public class NotePool : ObjectPool<NoteConfig, JumpCoinFactory> {}
  1. Add the pool to an empty object in the scene. This object will now be a Singleton that will create Objects on demand.

Anywhere, you can use these methods to get or return an object to the pool:

GameObject note = NotePool.Instance.GetPooledObject();
NotePool.Instance.ReturnToPool(note);

Comments

No comments yet. Be the first!