using UnityEngine ;
using UnityEngine ;
namespace UOP1.Pool
{
/// <typeparam name="T">Specifies the component to pool.</typeparam>
public abstract class ComponentPoolSO < T > : PoolSO < T > where T : Component
{
private GameObject _poolRootObject ;
private GameObject PoolRootObject
private Transform _poolRoot ;
private Transform PoolRoot
if ( ! Application . isPlaying )
{
return null ;
}
if ( _poolRootObject = = null )
if ( _poolRoot = = null )
_poolRootObject = new GameObject ( name ) ;
//DontDestroyOnLoad(_poolRootObject);
_poolRoot = new GameObject ( name ) . transform ;
_poolRoot . SetParent ( _parent ) ;
return _poolRootObject ;
return _poolRoot ;
private Transform _parent ;
/// <summary>
/// Parents the pool root transform to <paramref name="t"/>.
/// </summary>
/// <param name="t">The Transform to which this pool should become a child.</param>
/// <remarks>NOTE: Setting the parent to an object marked DontDestroyOnLoad will effectively make this pool DontDestroyOnLoad.<br/>
/// This can only be circumvented by manually destroying the object or its parent or by setting the parent to an object not marked DontDestroyOnLoad.</remarks>
public void SetParent ( Transform t )
{
_parent = t ;
PoolRoot . SetParent ( _parent ) ;
}
public override T Request ( )
{
T member = base . Request ( ) ;
public override void Return ( T member )
{
member . transform . SetParent ( PoolRootObject . transform ) ;
member . transform . SetParent ( PoolRoot . transform ) ;
member . gameObject . SetActive ( false ) ;
base . Return ( member ) ;
}
T newMember = base . Create ( ) ;
newMember . transform . SetParent ( PoolRootObject . transform ) ;
newMember . transform . SetParent ( PoolRoot . transform ) ;
newMember . gameObject . SetActive ( false ) ;
return newMember ;
}
base . OnDisable ( ) ;
if ( _poolRoot ! = null )
{
DestroyImmediate ( PoolRootObject ) ;
DestroyImmediate ( _poolRoot . gameObject ) ;
Destroy ( PoolRootObject ) ;
Destroy ( _poolRoot . gameObject ) ;
}
}
}
}