Unity对象池

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/ff_0528/article/details/83410331
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);
    }

}

 

展开阅读全文

没有更多推荐了,返回首页