Unity对象池

using System.Collections.Generic;
using UnityEngine;

/// 
/// 对象池管理类
/// 
public class GameObjectManager
{
    #region singleInstance
    private static GameObjectManager instance = null;
    public static GameObjectManager sInstance
    {
        get
        {
            if (instance == null)
            {
                instance = new GameObjectManager();
            }
            return instance;
        }
    }
    #endregion

    #region Config
    //池子最大容量
    public const int MAX = 50;
    #endregion

    /// 取出来对象池
    /// 
    public GameObject GetObj(string path)
    {
        GameObject ret = null;
        if (!string.IsNullOrEmpty(path))
        {
            ret = GetObj(Resources.Load(path));
        }
        return ret;
    }

    /// 取出来对象池
    /// 
    public GameObject GetObj(Object obj)
    {
        GameObject ret = null;
        do
        {
            if (obj == null) break;

            ret = PopPool(obj);
            if (ret == null)//对象池中没有,实例化出来      
            {
                ret = GameObject.Instantiate(obj) as GameObject;
                ret.name = obj.name;
            }

            PushActivePool(ret, obj);
        } while (false);

        return ret;
    }

    /// 放回对象池
    /// 
    public void Recycle(GameObject o)
    {
        Recycle(o, false);
    }

    /// 放回对象池
    /// 
    public void Recycle(GameObject o, bool isDetory)
    {
        do
        {
            if (o == null) break;
            CreateRoot();
            Object prefab = PopActivePool(o);
            if (prefab == null || isDetory)//立即销毁的 或 不是从对象池中取出来的
            {
                MyDestroy(o);
                break;
            }

            PushPool(o, prefab);

            Check();
        } while (false);
    }

    /// 消除所有对象
    public void DestroyAll()
    {
        inactiveDic.Clear();
        activeDic.Clear();
        enterInfoLs.Clear();
        if (poolRoot != null)
        {
            GameObject.Destroy(poolRoot.gameObject);
        }
    }

    //****************************私有方法************************
    //

    //池子根节点
    private Transform poolRoot;

    //对象池缓存的对象
    private Dictionary> inactiveDic = new Dictionary>();

    //对象池激活的对象
    private Dictionary activeDic = new Dictionary();

    //记录进入池的循序
    private List enterInfoLs = new List();

    /// 压入缓存池
    /// 
    /// 
    private void PushPool(GameObject o, Object prefab)
    {
        if (!inactiveDic.ContainsKey(prefab)) inactiveDic.Add(prefab, new List());
        if (!inactiveDic[prefab].Contains(o))
        {
            inactiveDic[prefab].Add(o);

            o.SetActive(false);
            o.transform.SetParent(poolRoot);

            RecordEntry(o, prefab);
        }
    }

    /// 压出缓存池
    /// 
    /// 
    private GameObject PopPool(Object prefab)
    {
        GameObject ret = null;
        if (prefab != null && inactiveDic.ContainsKey(prefab) && inactiveDic[prefab].Count > 0)
        {
            do
            {
                ret = inactiveDic[prefab][0];
                inactiveDic[prefab].RemoveAt(0);
            } while (ret == null && inactiveDic[prefab].Count > 0);//防止之前界面存在引用,删掉了缓存池的东西

            if (ret != null)
            {
                ret.gameObject.SetActive(true);
                ret.transform.SetParent(null);

                DeleteRecordEntry(ret);
            }
        }
        return ret;
    }

    /// 压入激活缓存池
    /// 
    /// 
    private void PushActivePool(GameObject o, Object prefab)
    {
        if (!activeDic.ContainsKey(o))
        {
            activeDic.Add(o, prefab);
        }
    }

    /// 压出激活缓存池
    /// 
    /// 
    private Object PopActivePool(GameObject prefab)
    {
        Object ret = null;
        if (activeDic.ContainsKey(prefab))
        {
            ret = activeDic[prefab];
            activeDic.Remove(prefab);
        }
        return ret;
    }

    /// 创建根节点
    private void CreateRoot()
    {
        if (poolRoot == null)
        {
            poolRoot = new GameObject().transform;
            poolRoot.name = "PoolRoot";
        }
    }

    /// 
    /// 检查是否需要释放
    /// 
    private void Check()
    {
        while (enterInfoLs.Count > MAX && enterInfoLs.Count > 0)
        {
            MyDestroy(PopPool(enterInfoLs[0].prefab));
            enterInfoLs.RemoveAt(0);
        }
    }

    /// 记录
    /// 
    private void RecordEntry(GameObject o, Object prefab)
    {
        enterInfoLs.Add(new GameObjectInfo(o, prefab));
    }

    /// 删除记录
    /// 
    private void DeleteRecordEntry(GameObject o)
    {
        for (int i = enterInfoLs.Count - 1; i >= 0; i--)
        {
            if (enterInfoLs[i].gameobject == o)
            {
                enterInfoLs.RemoveAt(i);
                break;
            }
            if (enterInfoLs[i].gameobject == null)
            {
                enterInfoLs.RemoveAt(i);
            }
        }
    }

    /// 删除资源
    /// 
    private void MyDestroy(GameObject obj)
    {
        GameObject.Destroy(obj);
    }
}

public class GameObjectInfo
{
    //实例化物体
    public GameObject gameobject;

    //预设
    public Object prefab;

    public GameObjectInfo(GameObject gameobject, Object prefab)
    {
        this.gameobject = gameobject;
        this.prefab = prefab;
    }
}

你可能感兴趣的:(Unity对象池)