Your Cart
Loading
object pooling unity 2d tutorial unity object pooling tutorial

Object Pooling Unity 2D Tutorial | Unity Tutorial

Object Pooling Unity 2D Tutorial | Unity Tutorial


In this Unity 2D tutorial, learn how to implement object pooling to optimize your game’s performance. We'll guide you through creating and managing a pool of reusable objects, such as bullets, to avoid the overhead of frequent instantiation and destruction. By the end, you'll understand how to set up efficient shooting mechanics and enhance your game's efficiency. Perfect for improving performance and mastering game development basics!



WATCH FULL TUTORIAL ON YOUTUBE



CHECK OUR PROJECTS


SUBSCRIBE FOR LATEST OFFERS



=========================================================


Unity Projects - ON SALE!!!


1. Water Sort Puzzle 9000 Levels

https://payhip.com/b/cjDb8


2. Ball Sort Puzzle 9000 Levels

https://payhip.com/b/IbtoD


3. Sling Shot

https://payhip.com/b/PjLDH



=========================================================



Scripts:


Bullet.cs


using UnityEngine;


public class Bullet : MonoBehaviour

{

  public float lifetime = 2f; // Time before the bullet is deactivated


  void OnEnable()

  {

    Invoke("Deactivate", lifetime);

  }


  void OnDisable()

  {

    CancelInvoke(); // Cancel the deactivation if the bullet is returned to the pool before the lifetime expires

  }


  void Deactivate()

  {

    // Return the bullet to the pool

    ObjectPool pool = FindObjectOfType<ObjectPool>(); // Make sure this works for your setup

    if (pool != null)

    {

      pool.ReturnObject(gameObject);

    }

  }

}



---------------------------------------------------------------------------------------------------------------------------------------


ObjectPool.cs


using System.Collections.Generic;

using UnityEngine;


public class ObjectPool : MonoBehaviour

{

  public GameObject prefab; // The prefab to pool

  public int poolSize = 10; // Number of objects in the pool


  private Queue<GameObject> pool;


  void Awake()

  {

    pool = new Queue<GameObject>();


    for (int i = 0; i < poolSize; i++)

    {

      GameObject obj = Instantiate(prefab);

      obj.SetActive(false);

      pool.Enqueue(obj);

    }

  }


  public GameObject GetObject()

  {

    if (pool.Count > 0)

    {

      GameObject obj = pool.Dequeue();

      obj.SetActive(true);

      return obj;

    }

    else

    {

      // Optionally: create new objects if pool is empty

      GameObject obj = Instantiate(prefab);

      return obj;

    }

  }


  public void ReturnObject(GameObject obj)

  {

    obj.SetActive(false);

    pool.Enqueue(obj);

  }

}



-----------------------------------------------------------------------------------------------------------------------------------------


BulletShooter.cs


using UnityEngine;


public class BulletShooter : MonoBehaviour

{

  public ObjectPool bulletPool; // Assign in Inspector

  public Transform firePoint; // The point from where bullets are fired

  public float bulletSpeed = 10f; // Speed of the bullets

  public KeyCode shootKey = KeyCode.Space; // Key to shoot

  public float fireRate = 0.1f; // Time between shots


  private float nextFireTime = 0f;


  void Update()

  {

    if (Input.GetKey(shootKey) && Time.time >= nextFireTime)

    {

      Shoot();

      nextFireTime = Time.time + fireRate;

    }

  }


  void Shoot()

  {

    GameObject bullet = bulletPool.GetObject();

    bullet.transform.position = firePoint.position;

    bullet.transform.rotation = firePoint.rotation;


    Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();

    if (rb != null)

    {

      rb.velocity = firePoint.right * bulletSpeed; // Ensure this aligns with your desired direction

    }

  }

}





DOWNLOAD CODESTER FILES