1. 程式人生 > 其它 >Unity——物件池管理

Unity——物件池管理

Unity物件池管理

一、Demo展示

二.邏輯

在遊戲中會出現大量重複的物體需要頻繁的建立和銷燬;比如子彈,敵人,成就列表的格子等;

頻繁的建立刪除物體會造成很大的開銷,像這種大量建立重複且非持續性保持作用的物件我們會使用物件池將其管理起來,用空間換效率;

物件池的要對外提供建立銷燬物件的介面,已經新增物件池的介面;

對內要提供建立物件,根據路徑查詢預製的介面;

整體邏輯如下:

二.載入/新增物件池

新增資源路徑,分為Resources資料夾下和SteamingAssets檔案下資源;

Resources載入時路徑不需要字尾,SteamingAssets需要字尾,根據path路徑開頭的判斷載入方式,同時對路徑做處理;

SteamingAssets需要根據平臺新增路徑頭Application.streamingAssetsPath;

Resources需要去掉格式字尾,如.perfab;

這裡我測試只使用Resources路徑,正式專案自行處理;

存放預建立好的物件:

private Dictionary<string,List<GameObject>> pool;

存放預製體路徑:

private Dictionary<string, string> objPath;
public void InitPath()
{
    //測試用,正常肯定要做字串處理的,需要加字尾
    objPath.Add("cube","Cube");
    objPath.Add("sphere","Sphere");
}
public void Add(string key, int num)
{
    if (pool.ContainsKey(key))
    {
        for (int i = 0; i < num; ++i)
        {
            //AssetBundle ab = AssetBundle.LoadFromFile($"{path}/{objPath[key]}");
            //GameObject go = Instantiate(ab.LoadAsset<GameObject>(key));
            //由於我的測試資源就放在Resources下,正式專案需要根據路徑做個判斷;
            GameObject go = Instantiate(Resources.Load<GameObject>(objPath[key]));
            go.SetActive(false);
            pool[key].Add(go);
        }
    }
    else
    {
        List<GameObject> goList = new List<GameObject>();
        pool.Add(key, goList);
        
        for (int i = 0; i < num; ++i)
        {
            //AssetBundle ab = AssetBundle.LoadFromFile(objPath[key]);
            //GameObject go = Instantiate(ab.LoadAsset<GameObject>(key));
            GameObject go = Instantiate(Resources.Load<GameObject>(objPath[key]));
            go.SetActive(false);
            pool[key].Add(go);
        }
    }
}

三.管理物件池

物件池管理需要對外提供建立物件,銷燬物件,延遲銷燬和清空池的方法;

建立物件時,需要提供key,座標,旋轉;

現查詢是否有物件池,沒有則新增;

這裡提供了四元素和尤拉角的過載方法;

1.建立物件

public GameObject FindUsable(string key
{
    if(pool.ContainsKey(key))
    {   
        foreach (GameObject item in poo
        {
            if (!item.activeSelf)
                return item;
        }
        Debug.Log($"{key}的物件池數量不夠");
    }
    else
    {
        Debug.Log($"{key}未新增物件池");
    }
    return null;
}
/// <summary>建立一個遊戲物體到場景 </summary>
public GameObject CreateObject(string k
{
    GameObject tempGo = FindUsable(key)
    if (tempGo == null)
    {
        Debug.Log($"{key}的物件池數量不夠");
        Add(key, 10);
        tempGo = FindUsable(key);
    }
    tempGo.transform.position = positio
    tempGo.transform.rotation = quatern
    tempGo.SetActive(true);
    return tempGo;
}
public GameObject CreateObject(string k
{
    GameObject tempGo = FindUsable(key)
    if (tempGo == null)
    {
        Debug.Log($"{key}的物件池數量不夠");
        Add(key, 10);
        tempGo = FindUsable(key);
    }
    tempGo.transform.position = positio
    tempGo.transform.rotation = Quatern
    tempGo.SetActive(true);
    return tempGo;
}

2.銷燬物件

延時銷燬可使用回撥(invoke)或協程;

public void Destory(GameObject destoryGo)
{
    destoryGo.SetActive(false);
}

/// <summary>將物件歸入池中<summary>
public void Destory(GameObject tempGo, float delay)
{
    StartCoroutine(DelayDestory(tempGo,delay));
}
/// <summary>延遲銷燬</summary>
private IEnumerator DelayDestory(GameObject destoryGO, float delay)
{
    yield return new WaitForSeconds(delay);
    Destory(destoryGO);
}

3.清空池

/// <summary>清空某類遊戲物件</summary>
public void Clear(string key)
{
    pool.Remove(key);
}
/// <summary>清空池中所有遊戲物件</summary>
public void ClearAll()
{
    pool.Clear();
}

四、測試程式碼

兩個按鈕分別建立方塊和球,每次建立更改一次位置;滑鼠射線點選延遲1s銷燬;

public class Test : MonoBehaviour
{
    public Button btnCube;
    public Button btnSphere;
    void Start()
    {
        GameObjectPool.I.InitPath();
        GameObjectPool.I.Add("cube",10);
        GameObjectPool.I.Add("sphere",10);
        btnCube.onClick.AddListener(OnBtnCube);
        btnSphere.onClick.AddListener(OnBtnSphere);
    }

    private Vector3 deltaPos = new Vector3(1, 1, 1);
    private Vector3 pos =new Vector3(-10,0,0);
    private void OnBtnCube()
    {
        GameObject go = GameObjectPool.I.CreateObject("cube", pos, Vector3.zero);

        go.transform.SetParent(null);
        SceneManager.MoveGameObjectToScene(go, SceneManager.GetSceneByName("1"));
        go.transform.position += deltaPos;
        pos = go.transform.position;
    }

    private Vector3 pos1 =new Vector3(0,0,0);
    private void OnBtnSphere()
    {
        GameObject go = GameObjectPool.I.CreateObject("sphere", pos1, Vector3.zero);
        
        go.transform.SetParent(null);
        SceneManager.MoveGameObjectToScene(go, SceneManager.GetSceneByName("1"));
        go.transform.position += deltaPos;
        pos1 = go.transform.position;
    }

    private void Update()
    {
        if(Input.GetMouseButtonDown(0)){
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hitInfo;
            if(Physics.Raycast(ray,out hitInfo)){
                Debug.DrawLine(ray.origin,hitInfo.point);
                GameObject gameObj = hitInfo.collider.gameObject;
                Debug.Log("click object name is " + gameObj.name);
                GameObjectPool.I.Destory(gameObj,1);
            }
        }
    }
}