1. 程式人生 > >Unity物件池

Unity物件池

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

/// <summary>
/// 例項化物件繼承此介面
/// </summary>
public interface IReusable {

    void OnSpawn();    //當取出時呼叫
    void OnUnspawn();  //當回收時呼叫

}







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

public class SubPool{

    //相對父物體
    private Transform m_parent;
    //預設
    private GameObject m_prefab;
    //物件的集合
    private List<GameObject> m_objects = new List<GameObject>();

    public SubPool(Transform parent,GameObject prefab)
    {
        this.m_parent = parent;
        this.m_prefab = prefab;
    }

    /// <summary>
    /// 取物件
    /// </summary>
    public GameObject Spawn()
    {
        GameObject go = null;
        foreach (GameObject obj in m_objects)
        {
            if (!obj.activeSelf)
            {
                go = obj;
                break;
            }
        }
        if(go == null)
        {
            go = GameObject.Instantiate<GameObject>(m_prefab);
            go.transform.SetParent(m_parent);
            m_objects.Add(go);
        }
        go.SetActive(true);
        go.SendMessage("OnSpawn", SendMessageOptions.DontRequireReceiver);
        return go;
    }

    /// <summary>
    /// 回收物件
    /// </summary>
    public void Unspawn(GameObject go)
    {
        if(m_objects.Contains(go))
        {
            go.SendMessage("OnUnspawn", SendMessageOptions.DontRequireReceiver);
            go.SetActive(false);
        }
    }

    /// <summary>
    /// 回收所有物件
    /// </summary>
    public void UnspawnAll()
    {
        foreach (GameObject go in m_objects)
        {
            if(go.activeSelf)
                Unspawn(go);
        }
    }

    /// <summary>
    /// 判斷是否包含物件
    /// </summary>
    public bool IsContains(GameObject go)
    {
        return m_objects.Contains(go);
    }
}








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

public class ObjectPool : MonoBehaviour {

    //單例
    private static ObjectPool m_instance;
    public static ObjectPool Instance
    {
        get { return m_instance; }
    }

    private void Awake()
    {
        m_instance = this;
    }

    private Dictionary<string, SubPool> m_pools = new Dictionary<string, SubPool>();

    /// <summary>
    /// 取物件
    /// </summary>
    public GameObject Spawn(string prefabPath)
    {
        if (!m_pools.ContainsKey(prefabPath))
            CreateSubPool(prefabPath);
        SubPool subPool = m_pools[prefabPath];
        return subPool.Spawn();
    }

    /// <summary>
    /// 回收物件
    /// </summary>
    public void Unspawn(GameObject go)
    {
        SubPool subPool = null;
        foreach (SubPool pool in m_pools.Values)
        {
            if(pool.IsContains(go))
            {
                subPool = pool;
                break;
            }
        }
        subPool.Unspawn(go);
    }

    /// <summary>
    /// 回收所有物件
    /// </summary>
    public void UnspawnAll()
    {
        foreach (SubPool subPool in m_pools.Values)
            subPool.UnspawnAll();
    }

    /// <summary>
    /// 建立子池子
    /// </summary>
    public void CreateSubPool(string prefabPath)
    {
        //載入預設
        GameObject prefab = Resources.Load<GameObject>(prefabPath);
        SubPool subPool = new SubPool(transform, prefab);
        m_pools.Add(prefabPath, subPool);
    }

}