1. 程式人生 > >Unity之深入分析物件池

Unity之深入分析物件池

首先先分析一下何為物件池,什麼情況下使用:對於遊戲中經常使用而且需要及時銷燬的物件,比如子彈,特效,捕魚遊戲中的魚,甚至音效,這些物件如果使用平常的instantiate方法,是在不停的申請記憶體,而這些物件基本有沒有區別,這樣無疑會消耗我們大量效能。所以物件池為此而生。物件池就是在我們不需要的時候隱藏起來而不是直接銷燬,到需要的時候顯示出來,再賦予相應的transform。好了,現在這些東西說完了,估計原因和目的大家都很清楚,就是有可能對具體的實現方法有些疑惑,廢話少說,直接說思路,如果不想看思路的,可以直接拉到下面看程式碼。

思路:我們的目的是使N個放在Resources資料夾下的物件在需要的時候可以順利的例項化到場景中,也就是N個動態陣列,所以這裡用 字典明顯更合適,Key為每個物件的名字,為string型別,Value為各物件型別的List    所以把程式碼初步分為兩個類,一個儲存單個的名為name的List物件,一個類儲存所有的list的字典Dictionary<string,List> ,每個類裡又有出池  入池的方法。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

class SubPool
{
    GameObject poolItem;
    List<GameObject> poolList;
    public SubPool(GameObject poolItem)
    {
        this.poolItem = poolItem;
        poolList = new List<GameObject>();
    }

    public GameObject Spawn()
    {
        GameObject go = null;
        
        for (int i = 0; i < poolList.Count; i++)
        {
            
            if (!poolList[i].gameObject.activeSelf)
            {
                go = poolList[i].gameObject;
                go.SetActive(true);
                break;
            }
        }
        if (go == null)
        {
            go = GameObject.Instantiate(poolItem);
            poolList.Add(go);
        }
        go.SendMessage("OnSpawn", SendMessageOptions.DontRequireReceiver);
        return go;
    }

    public void UnSpawn(GameObject go)
    {
        if (poolList.Contains(go))
        {
            go.SetActive(false);
            go.SendMessage("OnUnspaw", SendMessageOptions.DontRequireReceiver);
        }
    }

    public void UnSpawn(GameObject go)
    {
        if (poolList.Contains(go))
        {
            go.SetActive(false);
            go.SendMessage("OnUnspaw", SendMessageOptions.DontRequireReceiver);
        }
    }

    public bool Contains(GameObject go)
    {
        if (poolList.Contains(go))
            return true;
        else
            return false;
    }
}

 

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

public class ObjectPool : MonoBehaviour
{
    public string ResourcesDic = "";
    public static ObjectPool instance;

    void Awake()
    {
        if (instance == null)
            instance = this;
    }
    private Dictionary<string, SubPool> poolDic = new Dictionary<string, SubPool>();

    public GameObject Spawn(string name)
    {
        if (!poolDic.ContainsKey(name))
            Regist(name);
        SubPool subPool = poolDic[name];
        return subPool.Spawn();
    }

    public void UnSpawn(GameObject go)
    {
        foreach (SubPool item in poolDic.Values)
        {
            if (item.Contains(go))
            {
                item.UnSpawn(go);
                break;
            }
        }
    }

    public void UnSpawnAll()
    {
        foreach (var item in poolDic.Values)
        {
            item.UnSpawnAll();
        }
    }

    void Regist(string name)
    {
        string path = "";
        if (string.IsNullOrEmpty(ResourcesDic.Trim()))
            path = name;
        else
            path = ResourcesDic.Trim() + "/" + name;
        GameObject go = Resources.Load<GameObject>(path);
        SubPool subPool = new SubPool(go);
        poolDic.Add(name, subPool);
    }
}
 

public interface IReusable
{
    /// <summary>
    /// 出池
    /// </summary>
    void OnSpawn();
    /// <summary>
    /// 入池
    /// </summary>
    void OnUnspaw();
}

 

這兩個類中ObjectPool放在場景中,主要用到ObjetPool.instance.Spawn()(出池),ObjetPool.instance.UnSpawn()(入池) Resouorces中的物件繼承 IReusable介面,OnSpawn()會在出池時被呼叫,OnUnspaw()會在入池時被呼叫。