using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using DevelopEngine;
//单例对象池
public class GameObjectPool:MonoSingleton<GameObjectPool> {
//public static GameObjectPool instance;//单例
public GameObject[] prefabs;
private Dictionary<string, List<GameObject>> pool = new Dictionary<string, List<GameObject>>();//对象池字典
void Awake()
{
// instance = this;
}
//从池中获取
public GameObject GetOut(string GameObjectName, Vector3 vector3)
{
GameObject gameObject;
//对象池中有物体
if (pool.ContainsKey(GameObjectName) && pool[GameObjectName].Count > 0)
{
gameObject = pool[GameObjectName][0];
gameObject.SetActive(true);
gameObject.transform.position = vector3;
//取完后移除
pool[GameObjectName].RemoveAt(0);
}
else//池中没有需要的预设体
{
GameObject prefab = null;
//要生成的预设体
for (int i = 0; i < prefabs.Length; i++)
{
if (prefabs[i].name == GameObjectName)
{
prefab = prefabs[i];
}
}
//生成的预设体
gameObject = Instantiate(prefab) as GameObject;
}
return gameObject;
}
//存入对象池
public void SetIn(string GameobjectName, GameObject gameobject)
{
//池中没有
if (!pool.ContainsKey(GameobjectName))
{ //新建list
pool.Add(GameobjectName, new List<GameObject>());
}
//存入池
gameObject.SetActive(false);
pool[GameobjectName].Add(gameObject);
}
//销毁对象池
public void DestroyPool(string GameObjectName)
{
if (pool.ContainsKey(GameObjectName))
{//删除对象
for (int i = 0; i < pool[GameObjectName].Count; i++)
{
Destroy(pool[GameObjectName][i]);
}
//移除列表
pool.Remove(GameObjectName);
}
}
}