Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class GameObjectPooler
- {
- public int maxPoolSize = 5;
- private GameObject poolObject;
- private List<GameObject> pooledObjects;
- /// <summary>
- /// Creates a new instance of GameObjectPooler and
- /// Instantiates the maximum number of GameObjects.
- /// </summary>
- /// <param name="t">Original copy of GameObject to pool.</param>
- public GameObjectPooler(GameObject newPooledObject)
- {
- poolObject = newPooledObject;
- for (int i = 0; i < maxPoolSize; i++)
- {
- GameObject newObject = Object.Instantiate(poolObject);
- newObject.gameObject.SetActive(false);
- pooledObjects.Add(newObject);
- }
- }
- /// <summary>
- /// Retrieves the first inactive pooled GameObject and
- /// returns it.
- /// </summary>
- /// <returns>First inactive pooled GameObject.</returns>
- public GameObject GetPooledObject()
- {
- for (int i = 0; i < pooledObjects.Count; i++)
- {
- if (!pooledObjects[i].gameObject.activeInHierarchy)
- {
- return pooledObjects[i];
- }
- }
- return null;
- }
- /// <summary>
- /// Returns an active pooled GameObject to inactive state.
- /// </summary>
- /// <param name="toPoolObject">Pooled GameObject to be returned.</param>
- public void ReturnPooledObject(GameObject toPoolObject)
- {
- toPoolObject.gameObject.SetActive(false);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement