To test our implementation of the Object Pool in your own instance of Unity, you need to carry out the following steps:
- Create a new empty Unity scene.
- Copy all the scripts we just reviewed and save them in your project.
- Add an empty GameObject to the scene.
- Attach the following client script to your empty GameObject:
using UnityEngine;
namespace Chapter.ObjectPool
{
public class ClientObjectPool : MonoBehaviour
{
private DroneObjectPool _pool;
void Start()
{
_pool = gameObject.AddComponent<DroneObjectPool>();
}
void OnGUI()
{
if (GUILayout.Button("Spawn Drones"))
_pool.Spawn();
}
}
}
Once you start your scene, you should see a graphical user interface (GUI) button named Spawn Drones in the top-left corner, as we can see in the following screenshot:
Figure 8.2 – Screenshot of the code example in action
By pressing the ...