using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
public abstract class SimplePool<T> : UniRx.Toolkit.ObjectPool<T> where T : UnityEngine.Component
{

    protected T Prefab { get; set; }
    protected string PoolObjectName { get; set; }
    public SimplePool(T prefab, string poolObjectName = "pool")
    {
        this.Prefab = prefab;
        PoolObjectName = poolObjectName;
    }
    protected override void OnBeforeReturn(T instance)
    {
        base.OnBeforeReturn(instance);
        instance.transform.parent = GetParentObject.transform;

    }
    protected override void OnBeforeRent(T instance)
    {
        base.OnBeforeRent(instance);
        instance.transform.parent = null;
    }
    GameObject GetParentObject
    {
        get
        {
            var pool = GameObject.Find(PoolObjectName);
            if (pool == null)
            {
                pool = new GameObject(PoolObjectName);
                //  GameObject.DontDestroyOnLoad(pool);
            }
            return pool;
        }
    }

    protected override T CreateInstance()
    {
        return GameObject.Instantiate(Prefab);
    }

}
