Add A SpawnPool Component
| 1 . Create an empty GameObject, select it and add...
Component > Path-o-logical > PoolManager > SpawnPool.
You should now see this in the Inspector:
|SETUP TIP: You can add a
SpawnPool component to any GameObject, or even add several. For
example, you could have all the pools you need for a particular
level/map all in one GameObject, then make that a prefab and load it
with the level. However, it would probably be easier to work with if you kept 1 per GameObject and just made them all a child of a prefab. |
You could also keep a prefab per SpawnPool as templates.
SETUP TIP: Keep your Pool
Names as generic as possible. Think about archetypes in your game, e.g.
"Enemies", "Projectiles", etc.
2. Enter the name you want to use to access the pool. If you don't enter a name, PoolManager will use the name of the GameObject (without the word "Pool" if found, so "MyPool" would automatically create a pool named "My" - this only happens when the field is left blank, otherwise the field value will be used as is).
When an instance is initially created, it will be scaled relative to the Spawn Pool's GameObject. This is great for sprite-based GUI systems, such as nGUI, which often uses a small scale.
When an instance is initially created, it's layer will be set to match Spawn Pool's GameObject.
Activates Unity's DontDestroyOnLoad behavior so the SpawnPool's GameObject is persistent. See the Unity documentation for more information.
Even if you use PoolManager exclusively through scripting, Log Messages
may come in handy during development. Turn this on and start the game to see what PoolManager is doing (Messages are shown in the Unity Console). You can also set this on and off during game-play using PoolManager.LogMessages
You can choose to log messages for the entire SpawnPool, or per-prefab (explained on the next page)
First, add the namespace 'using' line to the top of the script file, e.g. 'using PathologicalGames;'
Then you can access the code in PoolManager to begin integrating it with your project. For example, you would do this with Unity's methods:
// Create an instance
GameObject myInstance = Instantiate(myPrefab);
// Destroy an instance
With PoolManager, using a pool named "Shapes", you would do this instead:
// Spawn an instance (with same argument options as Instantiate())
Transform myInstance = PoolManager.Pools["Shapes"].Spawn(myPrefab.transform);
// Despawn an instance (makes an instance available for reuse)
Prefabs versus Instances
To clarify the difference, think of instances as copies, or clones, of a prefab. When you use Spawn() you are telling PoolManager you want a clone, so pass a prefab to Spawn()
. When despawning, you are telling an instance you are done with it, so pass an instance to Despawn()
. If you pass an instance to Spawn(), you will make a new instance but it will also start a new PrefabPool because it is a copy of something new.
Manage Instances using the OnSpawned() and OnDespawned() Events
Awake() only runs when an object is instantiated and will not be called again when spawned by PoolManager. You can continue to use Awake() to initialize references and do any time-expensive initialization tasks, but two events are made (optionally) available to manage the behavior of instances as they spawn and de-spawn: "OnSpawned()" and "OnDespawned()".
For example, use Awake() to cache your Transform for internal use in your script, and use OnSpawned() to re-initiliazes states, such as the level of a monster.