UnityPatterns/ObjectPool — pull request #1 by alfredbaudisch (2013-11-09 15:15:53+00:00)
Title: Instantiate a custom amount of objects on startup to save even more performance

With the actual code, if you have a fire rate that allows you to shoot let's say 100 bullets at a time, it will instantiate 100 game objects at runtime for the first shoots. Now if you have different pools running at once like bullets, explosions, coins, power ups, etc (like I have in my game, 7 pools) think about instantiating all these objects when the game is running the first time we need the objects, there is a noticeable frame rate and performance drop during the gameplay, specially in fast paced games.

For that reason I improved the class to allow the pre-instantiation of a defined amount of objects at startup. For the Turrent demo, on startup 10 bullets and 10 explosions are added to the pool and recycled before we even need them, this way when we shoot, the pool will never create another object, since 10 is more than enough to the demo's fire rate.

The magic is in the new CreatePool(T prefab, int initialAmount) and RecycleAll(T obj) methods.