Are you shooting bullets or creating any other objects in your game at runtime using functions like Instantiate? If the answer is yes then you probably haven’t heard of object pools.
Creating and destroying objects is expensive in execution time and inefficient, it is much better to say have 10 invisible bullet objects in your scene and then simply use these when you need to, that is:
- Move the object to your gun
- Make the object visible
- Animate it (move per frame in a bullety style!)
- When you’re finished with the bullet, say it hit something, just hide it again.
- Repeat
So no creating or destroying objects, no wasted time, you are simply re-using existing objects.
There are lots of guides to help you create your own “object pool” on the internet already, there is also free source code as well as plenty of paid assets on the asset store. Here is list of some I found:
- A free open source pool on GIT: https://github.com/UnityPatterns/ObjectPool
I use this myself, has all the functions to create a pool, spawn a new item and reset all its parameters, and finally release objects. - A $5 asset store pool (I haven’t this, buy at your own risk!): http://u3d.as/6eZ
- A $50 but bigger asset that includes a lot more (I haven’t tried this, buy at your own risk!): http://u3d.as/43o
- Unity’s own live training session from April 2014: http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/object-pooling
If you know of better examples, leave a comment and I’ll add them to the list.
2 thoughts on “Unity 3D – Object Pools and why you should be using them, speed up!”
Comments are closed.