unity中对象池的创建

unity创建一个对象池


//这里MonoSingleton是一个单例的类,为了以后在使用中只能存在一个类。也可以
public class GameObjectPool : MonoSingleton
{
//1 创建池:
private Dictionary<string, List> cache=
new Dictionary<string, List>();

//2 创建使用池的元素【游戏对象】一个对象并使用对象:
//池中有从池中返回;池中没有加载,放入池中再返回   
public GameObject CreateObject(string key, GameObject go,
    Vector3 positon, Quaternion quaternion)
{
    //1)查找池中有无可用游戏对象
    GameObject temoGo = FindUsable(key);
    //2)池中有从池中返回;
    if (temoGo != null)
    {
        temoGo.transform.position = positon;
        temoGo.transform.rotation = quaternion;
        temoGo.SetActive(true);//表示当前这个游戏对象在使用中
    }
    else//3)池中没有加载,放入池中再返回
    {
        temoGo = Instantiate(go, positon, quaternion) as GameObject; 
        //放入池中
        Add(key, temoGo);
    }
    //作为池物体的子物体
    temoGo.transform.parent = this.transform;
    return temoGo;
}
private GameObject FindUsable(string key)
{
    if (cache.ContainsKey(key))
    {
        //清楚列表中的空位置
        cache[key].RemoveAll(p=>p==null);
        //找列表 中找出 未激活【未活动】的游戏物体
        //p.activeSelf表示激活返回true,取反就是查找没有被激活的
        return cache[key].Find(p => !p.activeSelf);
    }
    return null;
}
private void Add(string key, GameObject go)
{
    //先检查池中是否 有需要的key,没有,需要向创建key和对应的列表。
    if (!cache.ContainsKey(key))
    {
        cache.Add(key, new List<GameObject>());
    }
    //把游戏对象添加到 池中
    cache[key].Add(go);
}

//3 释放资源:从池中删除对象!
//3.1释放部分:按Key释放 
public void Clear(string key)
{
    if (cache.ContainsKey(key))
    { 
        for (int i = 0; i < cache[key].Count; i++)
        {
            //释放场景中 的游戏物体
            Destroy(cache[key][i]);
        }
        //移除了对象的地址
        cache.Remove(key);
    }
}
//3.2释放全部 循环调用Clear(string key)
public void ClearAll()
{
    //这两种写法的效果一样
    //string[] keys = cache.Keys;
    List<string> keys = new List<string>(cache.Keys);
    for (int i = 0; i < keys.Count; i++)
    {
        Clear(keys[i]);
    }
}
//4 回收对象:使用完对象返回池中【从画面中消失】
//4.1即时回收对象
public void CollectObject(GameObject go)
{
    go.SetActive(false);//本质:画面小时 设置属性
}
//4.2延时回收对象 等待一定的时间 (协程)
public void CollectObject(GameObject go, float delay)
{
    //启动协程(调用方法)
    StartCoroutine(CollectDelay(go, delay));
}
private IEnumerator CollectDelay(GameObject go, float delay)
{
    //返回协程的语句必须使用 yield return
    yield return new WaitForSeconds(delay);
    CollectObject(go);
}

}

猜你喜欢

转载自blog.csdn.net/weixin_43333566/article/details/94760976