Skip to content

Instantly share code, notes, and snippets.

@llamacademy
Last active April 19, 2023 18:15
Show Gist options
  • Save llamacademy/e3ed9e647b2c703d698a809d00a112a8 to your computer and use it in GitHub Desktop.
Save llamacademy/e3ed9e647b2c703d698a809d00a112a8 to your computer and use it in GitHub Desktop.
Scripts from Object Pooling video: https://www.youtube.com/watch?v=fsDE_mO4RZM. If you get value from LlamAcademy, consider becoming a Patreon supporter at https://www.patreon.com/llamacademy
public class AutoDestroyPoolableObject : PoolableObject
{
public float AutoDestroyTime = 5f;
private const string DisableMethodName = "Disable";
public virtual void OnEnable()
{
CancelInvoke(DisableMethodName);
Invoke(DisableMethodName, AutoDestroyTime);
}
public virtual void Disable()
{
gameObject.SetActive(false);
}
}
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
public class Bullet : AutoDestroyPoolableObject
{
public Rigidbody2D RigidBody;
public Vector2 Speed = new Vector2(20, -1);
private void Awake()
{
RigidBody = GetComponent<Rigidbody2D>();
}
public override void OnEnable()
{
base.OnEnable();
RigidBody.velocity = Speed;
}
public override void OnDisable()
{
base.OnDisable();
RigidBody.velocity = Vector2.zero;
}
}
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
public class BulletNotPooled : MonoBehaviour
{
[HideInInspector]
public Rigidbody2D RigidBody;
public float DestroyTime = 1f;
public Vector2 Speed = new Vector2(200, 0);
private const string DestroyMethodName = "Destroy";
private void Awake()
{
RigidBody = GetComponent<Rigidbody2D>();
}
public void OnEnable()
{
RigidBody.velocity = Speed;
Invoke(DestroyMethodName, DestroyTime);
}
public void Destroy()
{
GameObject.Destroy(this.gameObject);
}
}
using System.Collections.Generic;
using UnityEngine;
public class ObjectPool
{
private PoolableObject Prefab;
private int Size;
private List<PoolableObject> AvailableObjectsPool;
private ObjectPool(PoolableObject Prefab, int Size)
{
this.Prefab = Prefab;
this.Size = Size;
AvailableObjectsPool = new List<PoolableObject>(Size);
}
public static ObjectPool CreateInstance(PoolableObject Prefab, int Size)
{
ObjectPool pool = new ObjectPool(Prefab, Size);
GameObject poolGameObject = new GameObject(Prefab + " Pool");
pool.CreateObjects(poolGameObject);
return pool;
}
private void CreateObjects(GameObject parent)
{
for (int i = 0; i < Size; i++)
{
PoolableObject poolableObject = GameObject.Instantiate(Prefab, Vector3.zero, Quaternion.identity, parent.transform);
poolableObject.Parent = this;
poolableObject.gameObject.SetActive(false); // PoolableObject handles re-adding the object to the AvailableObjects
}
}
public PoolableObject GetObject()
{
PoolableObject instance = AvailableObjectsPool[0];
AvailableObjectsPool.RemoveAt(0);
instance.gameObject.SetActive(true);
return instance;
}
public void ReturnObjectToPool(PoolableObject Object)
{
AvailableObjectsPool.Add(Object);
}
}
using System.Collections;
using UnityEngine;
public class PlayerShoot : MonoBehaviour
{
public Bullet BulletPrefab;
public int RateOfFire = 5;
private ObjectPool BulletPool;
private void Awake()
{
BulletPool = ObjectPool.CreateObjectPool(BulletPrefab, 100);
}
private void Start()
{
StartCoroutine(Fire());
}
private IEnumerator Fire()
{
WaitForSeconds Wait = new WaitForSeconds(1f / RateOfFire);
while(true)
{
PoolableObject instance = BulletPool.GetObject();
if (instance != null)
{
instance.transform.SetParent(transform, false);
instance.transform.localPosition = Vector2.zero;
instance.gameObject.SetActive(true);
}
yield return Wait;
}
}
}
using UnityEngine;
public class PoolableObject : MonoBehaviour
{
public ObjectPool Parent;
public virtual void OnDisable()
{
Parent.ReturnObjectToPool(this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment