C# Unity Object Pooling (for a shooter game)












0












$begingroup$


I am currently developing a Bullet Hell (shoot-em-up) game for my school project.
I have implemented Object Pooling for my game to recycle mainly bullets. (Though I could use it to recycle enemies in the future if I do need to.)



Currently I have tested this object pooling on bullet and it has worked.
I am looking to receive feedback about my code in order to see if I can do anything about it to make it more efficient and cleaner.



ObjectPool.cs



using System;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;

public class ObjectPool : Singleton<ObjectPool> {

private List<GameObject> objectPool;

private void Awake() {
objectPool = new List<GameObject>();
}

/// <summary>
/// Add a gameobject to the object pool.
/// </summary>
/// <param name="objToPool">The gameobject to add to the object pool.</param>
/// <param name="deactivateObj">Deactivate this gameobject upon storing into the object pool.</param>
public void AddToPool(GameObject objToPool, bool deactivateObj = true) {
objectPool.Add(objToPool);

// If we need to deactivate this gameobject.
if (deactivateObj) {
// Set it to not active.
objToPool.SetActive(false);
}
}

/// <summary>
/// Fetch a gameobject from the pool, with the gameobject containing the desired component.
/// </summary>
/// <typeparam name="T">The type of the component.</typeparam>
/// <param name="removeFromPool">True if the fetched gameobject needs to be removed from the pool.</param>
/// <returns>The respective gameobject. (Null if none is found)</returns>
public GameObject FetchObjectByComponent<T>(bool removeFromPool = true) where T : MonoBehaviour {
GameObject fetchedObject = null;

// Foreach game object in the object pool
foreach (var gameObj in objectPool) {
// If this gameobject has the desired component.
if (gameObj.GetComponent<T>() != null) {
// Fetch this object.
fetchedObject = gameObj;
// End loop (an object with the desired component is found.)
break;
}
}

// If an object is fetched and we need to remove it from the pool.
if (fetchedObject != null && removeFromPool) {
// Remove the fetched object from the pool.
objectPool.Remove(fetchedObject);
}

return fetchedObject;
}

/// <summary>
/// Fetch an array of gameobjects that contains the desired component.
/// </summary>
/// <typeparam name="T">The type of the component the gameobject must contain.</typeparam>
/// <param name="maxSize">The max size of the array returned. (Negative for limitless)</param>
/// <param name="removeFromPool">True to remove the respective fetched gameobject from the object pool.</param>
/// <returns>The respective fetched game objects.</returns>
public GameObject FetchObjectsByComponent<T>(int maxSize = -1, bool removeFromPool = true) where T : MonoBehaviour {

List<GameObject> temp = new List<GameObject>();

// Loop through the object pool as long as the size limit it not reached.
for (int i = 0; i < objectPool.Count && i < maxSize; ++i) {
// If this current object contains the desired component.
if (objectPool[i].GetComponent<T>() != null) {
// Add to the temporary list
temp.Add(objectPool[i]);
}
}

var fetchedObjects = temp.ToArray();

// If we need to remove the fetched objects from the object pool, remove.
if (removeFromPool) {
RemoveObjectsFromPool(fetchedObjects);
}

return fetchedObjects;
}

/// <summary>
/// Fetch an array of gameobject based on the given condition.
/// </summary>
/// <param name="condition">The condition to check on when fetching gameobjects.</param>
/// <param name="maxSize">The maximum size of the array returned. (Negative for limitless.)</param>
/// <param name="removeFromPool">True to remove the respective fetched gameobject from the object pool.</param>
/// <returns>The respective fetched game objects.</returns>
public GameObject FetchObjectsByCondition(Func<GameObject, bool> condition, int maxSize = -1, bool removeFromPool = true) {
// Fetch all the matching objects.
var fetchedObjects = objectPool.Where(condition).ToArray();

// If an array size limit is given.
if (maxSize >= 1) {
List<GameObject> temp = new List<GameObject>();

// Loop through the fetched objects, adding to the list as long as the list stays in it's given size limit.
for (int i = 0; i < fetchedObjects.Length && i < maxSize; ++i) {
temp.Add(fetchedObjects[i]);
}

fetchedObjects = temp.ToArray();
}

// If we need to remove the fetched objects from the object pool
if (removeFromPool) {
RemoveObjectsFromPool(fetchedObjects);
}

return fetchedObjects;
}

#region Util

private void RemoveObjectsFromPool(GameObject objectsToRemove) {
// For each given object.
foreach (var gameObject in objectsToRemove) {
// Remove the given object from the object pool.
objectPool.Remove(gameObject);
}
}

#endregion
}


I am currently using Unity 2018.3.1f1, if that matters.









share









$endgroup$

















    0












    $begingroup$


    I am currently developing a Bullet Hell (shoot-em-up) game for my school project.
    I have implemented Object Pooling for my game to recycle mainly bullets. (Though I could use it to recycle enemies in the future if I do need to.)



    Currently I have tested this object pooling on bullet and it has worked.
    I am looking to receive feedback about my code in order to see if I can do anything about it to make it more efficient and cleaner.



    ObjectPool.cs



    using System;
    using System.Collections.Generic;
    using UnityEngine;
    using System.Linq;

    public class ObjectPool : Singleton<ObjectPool> {

    private List<GameObject> objectPool;

    private void Awake() {
    objectPool = new List<GameObject>();
    }

    /// <summary>
    /// Add a gameobject to the object pool.
    /// </summary>
    /// <param name="objToPool">The gameobject to add to the object pool.</param>
    /// <param name="deactivateObj">Deactivate this gameobject upon storing into the object pool.</param>
    public void AddToPool(GameObject objToPool, bool deactivateObj = true) {
    objectPool.Add(objToPool);

    // If we need to deactivate this gameobject.
    if (deactivateObj) {
    // Set it to not active.
    objToPool.SetActive(false);
    }
    }

    /// <summary>
    /// Fetch a gameobject from the pool, with the gameobject containing the desired component.
    /// </summary>
    /// <typeparam name="T">The type of the component.</typeparam>
    /// <param name="removeFromPool">True if the fetched gameobject needs to be removed from the pool.</param>
    /// <returns>The respective gameobject. (Null if none is found)</returns>
    public GameObject FetchObjectByComponent<T>(bool removeFromPool = true) where T : MonoBehaviour {
    GameObject fetchedObject = null;

    // Foreach game object in the object pool
    foreach (var gameObj in objectPool) {
    // If this gameobject has the desired component.
    if (gameObj.GetComponent<T>() != null) {
    // Fetch this object.
    fetchedObject = gameObj;
    // End loop (an object with the desired component is found.)
    break;
    }
    }

    // If an object is fetched and we need to remove it from the pool.
    if (fetchedObject != null && removeFromPool) {
    // Remove the fetched object from the pool.
    objectPool.Remove(fetchedObject);
    }

    return fetchedObject;
    }

    /// <summary>
    /// Fetch an array of gameobjects that contains the desired component.
    /// </summary>
    /// <typeparam name="T">The type of the component the gameobject must contain.</typeparam>
    /// <param name="maxSize">The max size of the array returned. (Negative for limitless)</param>
    /// <param name="removeFromPool">True to remove the respective fetched gameobject from the object pool.</param>
    /// <returns>The respective fetched game objects.</returns>
    public GameObject FetchObjectsByComponent<T>(int maxSize = -1, bool removeFromPool = true) where T : MonoBehaviour {

    List<GameObject> temp = new List<GameObject>();

    // Loop through the object pool as long as the size limit it not reached.
    for (int i = 0; i < objectPool.Count && i < maxSize; ++i) {
    // If this current object contains the desired component.
    if (objectPool[i].GetComponent<T>() != null) {
    // Add to the temporary list
    temp.Add(objectPool[i]);
    }
    }

    var fetchedObjects = temp.ToArray();

    // If we need to remove the fetched objects from the object pool, remove.
    if (removeFromPool) {
    RemoveObjectsFromPool(fetchedObjects);
    }

    return fetchedObjects;
    }

    /// <summary>
    /// Fetch an array of gameobject based on the given condition.
    /// </summary>
    /// <param name="condition">The condition to check on when fetching gameobjects.</param>
    /// <param name="maxSize">The maximum size of the array returned. (Negative for limitless.)</param>
    /// <param name="removeFromPool">True to remove the respective fetched gameobject from the object pool.</param>
    /// <returns>The respective fetched game objects.</returns>
    public GameObject FetchObjectsByCondition(Func<GameObject, bool> condition, int maxSize = -1, bool removeFromPool = true) {
    // Fetch all the matching objects.
    var fetchedObjects = objectPool.Where(condition).ToArray();

    // If an array size limit is given.
    if (maxSize >= 1) {
    List<GameObject> temp = new List<GameObject>();

    // Loop through the fetched objects, adding to the list as long as the list stays in it's given size limit.
    for (int i = 0; i < fetchedObjects.Length && i < maxSize; ++i) {
    temp.Add(fetchedObjects[i]);
    }

    fetchedObjects = temp.ToArray();
    }

    // If we need to remove the fetched objects from the object pool
    if (removeFromPool) {
    RemoveObjectsFromPool(fetchedObjects);
    }

    return fetchedObjects;
    }

    #region Util

    private void RemoveObjectsFromPool(GameObject objectsToRemove) {
    // For each given object.
    foreach (var gameObject in objectsToRemove) {
    // Remove the given object from the object pool.
    objectPool.Remove(gameObject);
    }
    }

    #endregion
    }


    I am currently using Unity 2018.3.1f1, if that matters.









    share









    $endgroup$















      0












      0








      0





      $begingroup$


      I am currently developing a Bullet Hell (shoot-em-up) game for my school project.
      I have implemented Object Pooling for my game to recycle mainly bullets. (Though I could use it to recycle enemies in the future if I do need to.)



      Currently I have tested this object pooling on bullet and it has worked.
      I am looking to receive feedback about my code in order to see if I can do anything about it to make it more efficient and cleaner.



      ObjectPool.cs



      using System;
      using System.Collections.Generic;
      using UnityEngine;
      using System.Linq;

      public class ObjectPool : Singleton<ObjectPool> {

      private List<GameObject> objectPool;

      private void Awake() {
      objectPool = new List<GameObject>();
      }

      /// <summary>
      /// Add a gameobject to the object pool.
      /// </summary>
      /// <param name="objToPool">The gameobject to add to the object pool.</param>
      /// <param name="deactivateObj">Deactivate this gameobject upon storing into the object pool.</param>
      public void AddToPool(GameObject objToPool, bool deactivateObj = true) {
      objectPool.Add(objToPool);

      // If we need to deactivate this gameobject.
      if (deactivateObj) {
      // Set it to not active.
      objToPool.SetActive(false);
      }
      }

      /// <summary>
      /// Fetch a gameobject from the pool, with the gameobject containing the desired component.
      /// </summary>
      /// <typeparam name="T">The type of the component.</typeparam>
      /// <param name="removeFromPool">True if the fetched gameobject needs to be removed from the pool.</param>
      /// <returns>The respective gameobject. (Null if none is found)</returns>
      public GameObject FetchObjectByComponent<T>(bool removeFromPool = true) where T : MonoBehaviour {
      GameObject fetchedObject = null;

      // Foreach game object in the object pool
      foreach (var gameObj in objectPool) {
      // If this gameobject has the desired component.
      if (gameObj.GetComponent<T>() != null) {
      // Fetch this object.
      fetchedObject = gameObj;
      // End loop (an object with the desired component is found.)
      break;
      }
      }

      // If an object is fetched and we need to remove it from the pool.
      if (fetchedObject != null && removeFromPool) {
      // Remove the fetched object from the pool.
      objectPool.Remove(fetchedObject);
      }

      return fetchedObject;
      }

      /// <summary>
      /// Fetch an array of gameobjects that contains the desired component.
      /// </summary>
      /// <typeparam name="T">The type of the component the gameobject must contain.</typeparam>
      /// <param name="maxSize">The max size of the array returned. (Negative for limitless)</param>
      /// <param name="removeFromPool">True to remove the respective fetched gameobject from the object pool.</param>
      /// <returns>The respective fetched game objects.</returns>
      public GameObject FetchObjectsByComponent<T>(int maxSize = -1, bool removeFromPool = true) where T : MonoBehaviour {

      List<GameObject> temp = new List<GameObject>();

      // Loop through the object pool as long as the size limit it not reached.
      for (int i = 0; i < objectPool.Count && i < maxSize; ++i) {
      // If this current object contains the desired component.
      if (objectPool[i].GetComponent<T>() != null) {
      // Add to the temporary list
      temp.Add(objectPool[i]);
      }
      }

      var fetchedObjects = temp.ToArray();

      // If we need to remove the fetched objects from the object pool, remove.
      if (removeFromPool) {
      RemoveObjectsFromPool(fetchedObjects);
      }

      return fetchedObjects;
      }

      /// <summary>
      /// Fetch an array of gameobject based on the given condition.
      /// </summary>
      /// <param name="condition">The condition to check on when fetching gameobjects.</param>
      /// <param name="maxSize">The maximum size of the array returned. (Negative for limitless.)</param>
      /// <param name="removeFromPool">True to remove the respective fetched gameobject from the object pool.</param>
      /// <returns>The respective fetched game objects.</returns>
      public GameObject FetchObjectsByCondition(Func<GameObject, bool> condition, int maxSize = -1, bool removeFromPool = true) {
      // Fetch all the matching objects.
      var fetchedObjects = objectPool.Where(condition).ToArray();

      // If an array size limit is given.
      if (maxSize >= 1) {
      List<GameObject> temp = new List<GameObject>();

      // Loop through the fetched objects, adding to the list as long as the list stays in it's given size limit.
      for (int i = 0; i < fetchedObjects.Length && i < maxSize; ++i) {
      temp.Add(fetchedObjects[i]);
      }

      fetchedObjects = temp.ToArray();
      }

      // If we need to remove the fetched objects from the object pool
      if (removeFromPool) {
      RemoveObjectsFromPool(fetchedObjects);
      }

      return fetchedObjects;
      }

      #region Util

      private void RemoveObjectsFromPool(GameObject objectsToRemove) {
      // For each given object.
      foreach (var gameObject in objectsToRemove) {
      // Remove the given object from the object pool.
      objectPool.Remove(gameObject);
      }
      }

      #endregion
      }


      I am currently using Unity 2018.3.1f1, if that matters.









      share









      $endgroup$




      I am currently developing a Bullet Hell (shoot-em-up) game for my school project.
      I have implemented Object Pooling for my game to recycle mainly bullets. (Though I could use it to recycle enemies in the future if I do need to.)



      Currently I have tested this object pooling on bullet and it has worked.
      I am looking to receive feedback about my code in order to see if I can do anything about it to make it more efficient and cleaner.



      ObjectPool.cs



      using System;
      using System.Collections.Generic;
      using UnityEngine;
      using System.Linq;

      public class ObjectPool : Singleton<ObjectPool> {

      private List<GameObject> objectPool;

      private void Awake() {
      objectPool = new List<GameObject>();
      }

      /// <summary>
      /// Add a gameobject to the object pool.
      /// </summary>
      /// <param name="objToPool">The gameobject to add to the object pool.</param>
      /// <param name="deactivateObj">Deactivate this gameobject upon storing into the object pool.</param>
      public void AddToPool(GameObject objToPool, bool deactivateObj = true) {
      objectPool.Add(objToPool);

      // If we need to deactivate this gameobject.
      if (deactivateObj) {
      // Set it to not active.
      objToPool.SetActive(false);
      }
      }

      /// <summary>
      /// Fetch a gameobject from the pool, with the gameobject containing the desired component.
      /// </summary>
      /// <typeparam name="T">The type of the component.</typeparam>
      /// <param name="removeFromPool">True if the fetched gameobject needs to be removed from the pool.</param>
      /// <returns>The respective gameobject. (Null if none is found)</returns>
      public GameObject FetchObjectByComponent<T>(bool removeFromPool = true) where T : MonoBehaviour {
      GameObject fetchedObject = null;

      // Foreach game object in the object pool
      foreach (var gameObj in objectPool) {
      // If this gameobject has the desired component.
      if (gameObj.GetComponent<T>() != null) {
      // Fetch this object.
      fetchedObject = gameObj;
      // End loop (an object with the desired component is found.)
      break;
      }
      }

      // If an object is fetched and we need to remove it from the pool.
      if (fetchedObject != null && removeFromPool) {
      // Remove the fetched object from the pool.
      objectPool.Remove(fetchedObject);
      }

      return fetchedObject;
      }

      /// <summary>
      /// Fetch an array of gameobjects that contains the desired component.
      /// </summary>
      /// <typeparam name="T">The type of the component the gameobject must contain.</typeparam>
      /// <param name="maxSize">The max size of the array returned. (Negative for limitless)</param>
      /// <param name="removeFromPool">True to remove the respective fetched gameobject from the object pool.</param>
      /// <returns>The respective fetched game objects.</returns>
      public GameObject FetchObjectsByComponent<T>(int maxSize = -1, bool removeFromPool = true) where T : MonoBehaviour {

      List<GameObject> temp = new List<GameObject>();

      // Loop through the object pool as long as the size limit it not reached.
      for (int i = 0; i < objectPool.Count && i < maxSize; ++i) {
      // If this current object contains the desired component.
      if (objectPool[i].GetComponent<T>() != null) {
      // Add to the temporary list
      temp.Add(objectPool[i]);
      }
      }

      var fetchedObjects = temp.ToArray();

      // If we need to remove the fetched objects from the object pool, remove.
      if (removeFromPool) {
      RemoveObjectsFromPool(fetchedObjects);
      }

      return fetchedObjects;
      }

      /// <summary>
      /// Fetch an array of gameobject based on the given condition.
      /// </summary>
      /// <param name="condition">The condition to check on when fetching gameobjects.</param>
      /// <param name="maxSize">The maximum size of the array returned. (Negative for limitless.)</param>
      /// <param name="removeFromPool">True to remove the respective fetched gameobject from the object pool.</param>
      /// <returns>The respective fetched game objects.</returns>
      public GameObject FetchObjectsByCondition(Func<GameObject, bool> condition, int maxSize = -1, bool removeFromPool = true) {
      // Fetch all the matching objects.
      var fetchedObjects = objectPool.Where(condition).ToArray();

      // If an array size limit is given.
      if (maxSize >= 1) {
      List<GameObject> temp = new List<GameObject>();

      // Loop through the fetched objects, adding to the list as long as the list stays in it's given size limit.
      for (int i = 0; i < fetchedObjects.Length && i < maxSize; ++i) {
      temp.Add(fetchedObjects[i]);
      }

      fetchedObjects = temp.ToArray();
      }

      // If we need to remove the fetched objects from the object pool
      if (removeFromPool) {
      RemoveObjectsFromPool(fetchedObjects);
      }

      return fetchedObjects;
      }

      #region Util

      private void RemoveObjectsFromPool(GameObject objectsToRemove) {
      // For each given object.
      foreach (var gameObject in objectsToRemove) {
      // Remove the given object from the object pool.
      objectPool.Remove(gameObject);
      }
      }

      #endregion
      }


      I am currently using Unity 2018.3.1f1, if that matters.







      c# game unity3d





      share












      share










      share



      share










      asked 4 mins ago









      KaynnKaynn

      1136




      1136






















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211800%2fc-unity-object-pooling-for-a-shooter-game%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211800%2fc-unity-object-pooling-for-a-shooter-game%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Morgemoulin

          Scott Moir

          Souastre