Giter Club home page Giter Club logo

unity-object-pooling-system's Introduction

Unity Generic Object Pool

An Updated Object pooler to increase performance when pooling many objects.

Why use Object Pooling?

It is computationally expensive to instantiate and destroy objects, such as bullets, that get re-used a lot. Its a lot more performant to instantiate them all and to keep reference and to recycle them.

To use this object pool you will need to create a script for every pool you will need, and only one object is allowed per pool.

Why use Queue instead of List?

Queue is benificial to use because it acts similar to a LinkedList in C++. With a Queue, when we Dequeue an object it Pops the first element from the Queue. When we Enqueue an object, it is added to the end of the queue, creating a first in, first out system. We will not need to access objects at specific indecies, therefor a Queue is perfect for what we need. Read More about when to use Lists or Queues here!

The Setup

  1. Create a new script, call it what you want your pool to be named, IE a enemy pool could be called EnemyPool.cs and attach it to a GameObject
  2. In the script, we will follow the example below. The script will inherite from GenericObjectPool.cs, we will then give it a component type, such as ObjectTobePooled.cs, this could be any script or component that is attachable to a GameObject. We also need to override the virtual function AddPoolReference(), we want to copy this snipped of code. The interface we are accessing doesn't exist yet, so that is our next step.
public class GenericPool : GenericObjectPool<ObjectTobePooled>
{
    public override void AddPoolReference(ObjectTobePooled objectToAddReference) => objectToAddReference.GetComponent<IObjectPool>().pool = this;
}
  1. We now create the interface on every object that will be pooled. We access the interface to give the pooled object the reference to the pool that it belongs to. This is done so that all we need to do is call pool.Recycle() when the object is no longer needed and it will know the proper pool to Recycle the object into.
internal interface IObjectPool
{
        public GenericPool pool { get; set; }
}

How to use the Pool?

To access the pool, we want to call our new PoolScript and use the Get() method to return the object we have pooled.

unity-object-pooling-system's People

Contributors

2401lucas avatar

Stargazers

 avatar  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.