using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PoolData 
{
    public GameObject fatherObj;
    public List<GameObject> DataList = new List<GameObject>();


    public PoolData(string name, GameObject poolobj)
    {
        //创建子文件夹 并设置父物体
        fatherObj = new GameObject(name);
        fatherObj.transform.SetParent(poolobj.transform);

        DataList = new List<GameObject>() {};
    }
    public GameObject GetObj(string name) 
    {
        GameObject obj = DataList[0];

        DataList.RemoveAt(0);

        return obj;
    }

    public void PushObj(GameObject obj)
    {
        obj.transform.SetParent(fatherObj.transform);

        obj.SetActive(false);

        DataList.Add(obj);
    }

}
public class PoolMgr : BaseMgr<PoolMgr>
{
    private Dictionary<string, PoolData> Pooldic = new Dictionary<string, PoolData>();
    private GameObject Poolobj = null;

    public GameObject GetObj(string path) 
    {
        GameObject obj = null;

        if (Pooldic.ContainsKey(path))
        {
            Debug.Log(Pooldic[path].DataList.Count);
        }
        if (Pooldic.ContainsKey(path) &&Pooldic[path].DataList.Count>0)
        {
            obj = Pooldic[path].GetObj(path);
        }
        else
        {
            obj = GameObject.Instantiate(Resources.Load<GameObject>(path));
        }

        obj.SetActive(true);
        obj.transform.SetParent(null);
        obj.name = path;
        return obj;
    }
    public void PushObj(string name,GameObject obj) 
    {
        if (Poolobj==null)
        {
            Poolobj = new GameObject("Pool");
            GameObject.DontDestroyOnLoad(Poolobj);
        }
        
        if (!Pooldic.ContainsKey(name))
        {
            Pooldic.Add(name,new PoolData(name,Poolobj));
        }

        Pooldic[name].PushObj(obj);
    }
    public void Clear() 
    {
        Pooldic = new Dictionary<string, PoolData>();
        Poolobj = null;
    }

}
