You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.4 KiB

using System.Collections.Generic;
using UnityEngine;
namespace XWFramework.ObjectTool
{
public class SubPool
{
List<GameObject> pool;
GameObject m_prefab;
Transform m_parent;
public SubPool(GameObject prefab, Transform parent)
{
pool = new List<GameObject>();
m_prefab = prefab;
m_parent = parent;
}
//取出
public GameObject Spawn()
{
foreach (GameObject obj in pool)
{
if (!obj.activeInHierarchy)
{
obj.SetActive(true);
return obj;
}
}
GameObject gb = GameObject.Instantiate(m_prefab);
pool.Add(gb);
gb.transform.SetParent(m_parent);
return gb;
}
//回收
public void UnSpawn(GameObject obj)
{
if (!pool.Contains(obj))
{
pool.Add(obj);
}
obj.SetActive(false);
}
//回收所有
public void UnSpawnAll()
{
foreach (GameObject obj in pool)
{
if (obj.activeInHierarchy)
{
obj.SetActive(false);
}
}
}
//判断物体是否属于对象池
public bool Contains(GameObject obj)
{
return pool.Contains(obj);
}
}
}