Created
May 25, 2017 16:16
-
-
Save unity3dcollege/a406b0c4c14e3a9c3ba8772ce2c970b2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment