写的一个非常好用的对象池

2019-12-20  本文已影响0人  小蜻蜓队长

在游戏中总是有很多资源时可以复用的,当需要的时候从不活跃的池中取放入活跃池,当不需要的时候从活跃池转移到非活跃池.

using System;
using System.Collections.Generic;

public class ClassPool<T> where T : class
{
    protected List<T> m_ActionPool = new List<T>(); //活跃的池
    protected List<T> m_DealPool = new List<T>(); //不活跃的池

    //创建对象 或者从不活跃的池里pop,加入活跃的尺,如果不活跃的尺子里没有就创建一个
    public T Spaw(params object[] param)
    {
        T cls;
        if (m_DealPool.Count > 0)
        {
            cls = m_DealPool[0];
            m_DealPool.RemoveAt(0);
            if (cls == null)
            {
                cls = (T)Activator.CreateInstance(typeof(T), param);
            }
        }
        else
        {
            cls = (T)Activator.CreateInstance(typeof(T), param);
        }
        m_ActionPool.Add(cls);
        return cls;
    }

    //只需要pop如果不活跃的池子里没有就返回空
    public T SpawNotCreate()
    {
        T cls = null;
        if (m_DealPool.Count > 0)
        {
            cls = m_DealPool[0];
            m_DealPool.RemoveAt(0);
        }
        return cls;
    }

    //放入不活跃的池子
    public bool Recycle(T obj)
    {

        if (obj == null)
            return false;
        if (m_ActionPool.Contains(obj))
        {
            m_ActionPool.Remove(obj);
            m_DealPool.Add(obj);
            return true;
        }
        return false;
    }

    //遍历活动列表
    public void ActionTraverse(Action<T> action)
    {
        if(action == null)
            return;
        for (int i = m_ActionPool.Count-1; i >=0; i--)
        {
            if(m_ActionPool[i] != null)
                action(m_ActionPool[i]);
        }
    }

    //遍历非活动列表
    public void DealTraverse(Action<T> action)
    {
        if (action == null)
            return;
        for (int i = m_DealPool.Count-1; i >=0; i--)
        {
            if (m_DealPool[i] != null)
                action(m_DealPool[i]);
        }
    }

    //所有活跃全部转移到非活跃列表
    public void AllActionToDeal()
    {
        ActionTraverse((x) => { Recycle(x); });
    }

    //遍历所有列表完成你需要的操作
    public void AllTraverse(Action<T> action)
    {
        ActionTraverse(action);
        DealTraverse(action);
    }
}

以下是使用方式

using System;

namespace 对象池
{
    class Monster
    {
        public int data;
        private object monsterBt;

        public void OnDestroy()
        {
            data = 0;
        }

        public void Print()
        {
            Console.WriteLine("执行");
        }

    }

    class Program
    {
        static ClassPool<Monster> m_monsterPool = new ClassPool<Monster>();
        static void Main(string[] args)
        {
            //创建或者推出一个 对象
            Monster mster = m_monsterPool.Spaw();
            mster.data = 100;

            //遍历活跃表中的对象
            m_monsterPool.ActionTraverse((x) =>
            {
                x.Print();
            });

            //放入非活跃列表
            mster.OnDestroy();
            m_monsterPool.Recycle(mster);

            //遍历非活跃列表
            m_monsterPool.DealTraverse((x) => {
                x.Print();
            });

            //将对象池全部活跃列表转入非活跃列表
            m_monsterPool.AllActionToDeal();

        }
    }
}

上一篇下一篇

猜你喜欢

热点阅读