
As is shown in my screenshot above, at the start of my scene all of my PickAxe GameObjects are instantiated and they take up a bunch of space in my Inspector.
Is there any way for my to organize these better and have them as a child of another GameObject or have them under a tab? It’s not completely necessary but It would look a lot better, considering I am about to have 3 more types of obstacles added into the scene also. I am not sure if this helps, but I’ll post my completed ObjectPooler script below:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PickAxePoolManager : MonoBehaviour
{
Dictionary<int,Queue<GameObject>> poolDictionary = new Dictionary<int,Queue<GameObject>>();
//NOTE:
//Singleton Pattern used from lines 13 to 26
static PickAxePoolManager _instance; // Reference to the Pool Manager script
public static PickAxePoolManager instance // This is the accessor
{
get
{
if(_instance == null) // Check to see if _instance is null
{
_instance = FindObjectOfType<PickAxePoolManager>(); //Find the instance in the Pool Manager script in the currently active scene
}
return _instance;
}
}
/// <summary>
/// Creates the pool.
/// </summary>
/// <param name="prefab">Prefab.</param>
/// <param name="poolSize">Pool size.</param>
public void CreatePickAxePool(GameObject prefab, int poolSize)
{
int poolKey = prefab.GetInstanceID (); // Unique integer for every GameObject
if (!poolDictionary.ContainsKey (poolKey)) //Make sure poolKey is not already in the Dictionary,
//if it's not then we can create the pool
{
poolDictionary.Add(poolKey, new Queue<GameObject>());
for (int i = 0; i < poolSize; i++) //Instantiate the prefabs as dictated by the "poolSize" integer
{
GameObject newObject = Instantiate(prefab) as GameObject; //Instantiate as a GameObject
//newObject.SetActive(false); // Don't want it to be visible in the scene yet
poolDictionary [poolKey].Enqueue(newObject); // Add it to our Pool
}
}
}
/// <summary>
/// Reuses the object in our pool.
/// </summary>
/// <param name="prefab">Prefab.</param>
/// <param name="position">Position.</param>
/// <param name="rotation">Rotation.</param>
public void ReusePickAxe (GameObject prefab, Vector3 position, Quaternion rotation)
{
int poolKey = prefab.GetInstanceID (); // Get our pool key once again
if (poolDictionary.ContainsKey (poolKey)) // Quick check to make sure our pool dictionary contains the pool key
{
GameObject objectToReuse = poolDictionary[poolKey].Dequeue(); //Get the next object in the pool
poolDictionary[poolKey].Enqueue(objectToReuse); // Add the object back onto the end of the queue so it can be reused later
objectToReuse.SetActive(true); //Make sure the object was not disabled
objectToReuse.transform.position = position; // set position to applied values
objectToReuse.transform.rotation = rotation; // set rotation to applied values
}
}
}
Thank you! ![]()