Unity 性能優(yōu)化之寫個(gè)簡(jiǎn)單的緩存池

在公司看源碼的時(shí)候,發(fā)現(xiàn)一個(gè)投籃的場(chǎng)景中,玩家把籃球不斷投出去,然后不斷的創(chuàng)建和銷毀對(duì)象。于是試著寫個(gè)緩存池來(lái)管理。

有個(gè)叫PoolManager的工具類插件可以很方便的實(shí)現(xiàn),可以看這里的介紹Unity3D研究院之初探PoolManager插件。

但是其實(shí)對(duì)象池就是預(yù)先創(chuàng)建一點(diǎn)對(duì)象,當(dāng)我們需要用的時(shí)候,去拿就行了。如果沒(méi)有,再創(chuàng)建。使用完畢后也并不銷毀,方便下次使用。有點(diǎn)類似于Android中的listview的holder??雌饋?lái)并不難,花了點(diǎn)時(shí)間寫了個(gè)。

BufferPoolList.cs

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

public class BufferPoolList{

    private List<GameObject> pool;
    private GameObject prefab;
    private Transform prefabParent;

    
    public BufferPoolList(GameObject obj, Transform parent, int count)
    {
        prefab = obj;

        pool = new List<GameObject>(count);
        prefabParent = parent;

        for (int i = 0; i < count; i++)
        {
            GameObject objClone = GameObject.Instantiate(prefab) as GameObject;
            objClone.transform.parent = prefabParent;//為克隆出來(lái)的子彈指定父物體
            objClone.name = "Clone0" + i.ToString();
            objClone.SetActive(false);
            pool.Add(objClone); 
        }
    }


    public GameObject GetObject()
    {
        //遍歷緩存池 找空閑的物體
        foreach (GameObject iter in pool)
        {
            if (iter.activeSelf == false)
            {
                iter.transform.SetParent(prefabParent);
                iter.SetActive(true);
                return iter;
            }

        }
        GameObject newPrefab = GameObject.Instantiate(prefab) as GameObject;
        newPrefab.transform.SetParent(prefabParent);
        newPrefab.name = "Clone0" + pool.Count.ToString();
        newPrefab.SetActive(true);
        pool.Add(newPrefab);
        return newPrefab;
    }
}

在Player上的腳本或者控制類腳本上初始化之后,需要使用我們初始化的物體時(shí),只需要GetObject()即可,用完將物體設(shè)置不可見(jiàn)即可??梢栽趯?shí)例化的物體上判斷物體是否超出屏幕邊界,超出 則 .SetActive(false);設(shè)置物體不可見(jiàn)。

這樣寫雖然不用每次都創(chuàng)建對(duì)象,緩存池的目的算是達(dá)到了,但是每次獲取可用對(duì)象時(shí)都去做個(gè)循環(huán),總感覺(jué)怪怪的。所以換種寫法,不用list,改用 Queue 隊(duì)列來(lái)寫。代碼如下:

BufferPool.cs

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class BufferPool
{
    private Queue<GameObject> pool;
    private GameObject prefab;
    private Transform prefabParent; 

    //使用構(gòu)造函數(shù)構(gòu)造對(duì)象池
    public BufferPool(GameObject obj,Transform parent,int count)
    {
        prefab = obj;
        
        pool = new Queue<GameObject>(count);
        prefabParent = parent;

        for (int i = 0; i < count; i++)
        {
            GameObject objClone = GameObject.Instantiate(prefab) as GameObject;
            objClone.transform.parent = prefabParent;//為克隆出來(lái)的子彈指定父物體
            objClone.name = "Clone0" + i.ToString();
            objClone.SetActive(false);
            pool.Enqueue(objClone);  
        }
    }

    
    public GameObject GetObject()
    {
        GameObject obj = null;

        if (pool.Count > 0)
        {
            obj = pool.Dequeue();  //Dequeue()方法 移除并返回位于 Queue 開(kāi)始處的對(duì)象
            obj.transform.position = prefabParent.position;
        }
        else
        {
            obj = GameObject.Instantiate(prefab) as GameObject;
            obj.transform.SetParent(prefabParent);
           
        }
        
        obj.SetActive(true);
        return obj;
    }

    //回收對(duì)象
    public void Recycle(GameObject obj)
    {
        obj.SetActive(false);
        pool.Enqueue(obj);//加入隊(duì)列
    }
}

這樣獲取對(duì)象時(shí),就不用做循環(huán)了。每次使用時(shí),出列。實(shí)例化的物體不再使用時(shí),再讓他加入隊(duì)列。這樣也有個(gè)不方便的地方,需要在游戲物體的腳本上,拿到 Player上的 BufferPool.cs 腳本 ,來(lái)回收對(duì)象,因?yàn)楂@取對(duì)象時(shí),已經(jīng)把該實(shí)例給移出隊(duì)列了,所以當(dāng)不再使用時(shí),必須調(diào)用Recycle方法來(lái)將其加入到隊(duì)列中去。

雖然一個(gè)用的List ,一個(gè)用的 Queue,但是道理是一樣的。目的也都是為了解決Unity實(shí)例化對(duì)象慢的問(wèn)題。

鏈接:http://539go.com/2016/11/10/Unity-%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96%E4%B9%8B%E5%86%99%E4%B8%AA%E7%AE%80%E5%8D%95%E7%9A%84%E7%BC%93%E5%AD%98%E6%B1%A0/

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容