Skip to content

Instantly share code, notes, and snippets.

@unity3dcollege
Created October 8, 2017 23:28
Show Gist options
  • Save unity3dcollege/ec1fe249b8b1a55078ea3d674bad938d to your computer and use it in GitHub Desktop.
Save unity3dcollege/ec1fe249b8b1a55078ea3d674bad938d to your computer and use it in GitHub Desktop.
using System;
using UnityEngine;
public class PooledMonoBehaviour : MonoBehaviour
{
[SerializeField]
private int initialPoolSize = 100;
public int InitialPoolSize {get {return initialPoolSize; } }
public event Action OnDestroyEvent;
protected virtual void OnDisable()
{
if (OnDestroyEvent != null)
OnDestroyEvent();
}
public T Get<T>(bool enable = true) where T : PooledMonoBehaviour
{
var pool = Pool.GetPool(this);
var pooledObject = pool.Get<T>();
if (enable)
{
pooledObject.gameObject.SetActive(true);
}
return pooledObject;
}
public T Get<T>(Transform parent, bool resetTransform = false) where T : PooledMonoBehaviour
{
var pooledObject = Get<T>(true);
pooledObject.transform.SetParent(parent);
if(resetTransform)
{
pooledObject.transform.localPosition = Vector3.zero;
pooledObject.transform.localRotation = Quaternion.identity;
}
return pooledObject;
}
public T Get<T>(Transform parent, Vector3 relativePosition, Quaternion relativeRotation) where T : PooledMonoBehaviour
{
var pooledObject = Get<T>(true);
pooledObject.transform.SetParent(parent);
pooledObject.transform.localPosition = relativePosition;
pooledObject.transform.localRotation = relativeRotation;
return pooledObject;
}
public T Get<T>(Vector3 relativePosition, Quaternion relativeRotation) where T : PooledMonoBehaviour
{
var pooledObject = Get<T>(true);
pooledObject.transform.localPosition = relativePosition;
pooledObject.transform.localRotation = relativeRotation;
return pooledObject;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment