// ObjectPool<T>.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ObjectPool<T> where T : PoolableObject
{
private int allocateCount;
public delegate T Initializer ();
private Initializer initializer;
private Stack<T> objStack;
public List<T> objList;
public ObjectPool ()
{
// default constructor
}
public ObjectPool (int ac, Initializer fn)
{
this.allocateCount = ac;
this.initializer = fn;
this.objStack = new Stack<T>();
this.objList = new List<T>();
}
public void Allocate ()
{
for (int index = 0; index < this.allocateCount; ++index)
{
this.objStack.Push(this.initializer());
}
}
public T PopObject ()
{
if (this.objStack.Count <= 0)
{
Allocate();
}
T obj = this.objStack.Pop();
this.objList.Add(obj);
obj.gameObject.SetActive(true);
return obj;
}
public void PushObject (T obj)
{
obj.gameObject.SetActive(false);
this.objList.Remove(obj);
this.objStack.Push(obj);
}
public void Dispose ()
{
if (this.objStack == null || this.objList == null)
return;
this.objList.ForEach(obj => this.objStack.Push(obj));
while (this.objStack.Count > 0)
{
GameObject.Destroy(this.objStack.Pop());
}
this.objList.Clear();
this.objStack.Clear();
}
}
// PoolableObject.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PoolableObject : MonoBehaviour
{
protected ObjectPool<PoolableObject> pPool;
public virtual void Create (ObjectPool<PoolableObject> pool)
{
pPool = pool;
gameObject.SetActive(false);
}
public virtual void Dispose ()
{
pPool.PushObject(this);
}
public virtual void _OnEnableContents ()
{
// to do ...
}
public virtual void _OnDisableContents ()
{
// to do ...
}
}
// ObjectPoolManager.cs
using UnityEngine;
using System.Collections;
public class ObjectPoolManager : MonoBehaviour
{
private static ObjectPoolManager singleton;
public static ObjectPoolManager GetInstance () { return singleton; }
public ObjectPool<PoolableObject> bulletPool = new ObjectPool<PoolableObject>();
public Bullet bulletPrefab;
void Awake ()
{
if (singleton != null && singleton != this)
{
Destroy(gameObject);
}
else
{
singleton = this;
}
}
void Start ()
{
bulletPool = new ObjectPool<PoolableObject>(5, () =>
{
Bullet bullet = Instantiate(bulletPrefab);
bullet.Create(bulletPool);
return bullet;
});
bulletPool.Allocate();
}
void OnDestroy ()
{
bulletPool.Dispose();
singleton = null;
}
}
// Gun.cs
using UnityEngine;
using System.Collections;
public class Gun : MonoBehaviour
{
[HideInInspector] public Transform tm;
private Vector3 bulletSpawnPoint;
void Awake ()
{
tm = gameObject.GetComponent<Transform>();
bulletSpawnPoint = transform.FindChild("bulletSpawnPoint").transform.position;
}
void Update ()
{
if (Input.GetMouseButtonDown(0))
{
Bullet bullet = ObjectPoolManager.GetInstance().bulletPool.PopObject() as Bullet;
bullet.Fire(bulletSpawnPoint);
}
}
}
// Bullet.cs
using UnityEngine;
using System.Collections;
public class Bullet : PoolableObject
{
[HideInInspector] public Transform tm;
[HideInInspector] public Rigidbody2D rb2D;
private float force;
public override void Create (ObjectPool<PoolableObject> pool)
{
base.Create (pool);
}
public override void Dispose ()
{
base.Dispose ();
}
public override void _OnEnableContents ()
{
base._OnEnableContents ();
rb2D.Sleep();
}
public override void _OnDisableContents ()
{
base._OnDisableContents ();
}
void Awake ()
{
tm = gameObject.GetComponent<Transform>();
rb2D = gameObject.GetComponent<Rigidbody2D>();
force = 800.0f;
}
void OnEnable ()
{
_OnEnableContents();
}
void OnDisable ()
{
_OnDisableContents();
}
void Update ()
{
if (tm.position.x > 10.0f)
{
Dispose();
}
}
public void Fire (Vector3 spawnPoint)
{
tm.position = spawnPoint;
rb2D.AddForce(Vector3.right * force);
}
}