游戏设计 -- 资源缓存池

     游戏中为了避免频繁的IO操作,建立必要的缓冲池是必须的。需要的资源先在缓存池中提取,如果缓存中不存在希望的资源,则使用IO加载到新的资源。待资源使用完毕后归还给缓存池。

下面是笔者的特效级缓存设计

http://naotu.baidu.com/file/46eb35003cbc4b46911cca83945c47e1?token=5725518b92f00df2


缓存池接口

using UnityEngine;
using System.Collections;

// ========================================================
    // 描 述:对象缓存池接口
    // 作 者:ZhangShouYang
    // 创建时间:#CreateTime#
// 版 本:v 1.0
// ========================================================
namespace Hstj
{

    public interface IObjectPool<T>
    {
        /// <summary>
        /// 从缓存池中取出对象
        /// </summary>
        /// <returns></returns>
        T Take();

        /// <summary>
        /// 对象返回缓存池
        /// </summary>
        void Restor(T t);

        /// <summary>
        /// 向缓存池中添加对象
        /// </summary>
        /// <param name="t"></param>
        void AddObjec();

        /// <summary>
        /// 缓存中空闲数目
        /// </summary>
        /// <returns></returns>
        int IdleNum();

        /// <summary>
        /// 缓存中可用的对象数目
        /// </summary>
        /// <returns></returns>
        int ActiveNum();

        /// <summary>
        /// 清空缓存池
        /// </summary>
        void Clear();
    }
}
基本数据对象缓存
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;

// ========================================================
    // 描 述:基本的对象池
    // 作 者:ZhangShouYang
    // 创建时间:#CreateTime#
    // 版 本:v 1.0
// ========================================================

namespace Hstj
{
    public class ObjectPool<T> : IObjectPool<T>
    {
        private readonly List<T> _pool = new List<T>();
        private readonly IObjectFactory<T> _factory;

        /// <summary>
        /// 最大的空闲数目上限
        /// </summary>
        private int _maxIdleNum;

        /// <summary>
        /// 当前激活(使用到的数目)
        /// </summary>
        protected int _activeNum = 0;

        public ObjectPool(int maxIdleNum = 10)
        {
            this._factory = null;
            this._maxIdleNum = 10;
        }

        public ObjectPool(IObjectFactory<T> factory, int maxIdleNum = 10)
        {
            if (factory == null)
            {
                throw new ObjectPoolException("factory can not be null");
            }

            this._factory = factory;
            this._maxIdleNum = maxIdleNum;
        }

        public virtual T Take()
        {
            T t;

            if (_pool.Count <= 0)
            {
                t = _factory.CreateObject(true);
            }
            else
            {
                t = _pool[0];
                _pool.RemoveAt(0);
                _factory.ActiveObject(t);             
            }
            _activeNum++;
            return t;
        }

        public virtual void Restor(T t)
        {
            if (_pool.Count >= _maxIdleNum)
            {
                _factory.DestroyObject(t);
            }
            else
            {
                _factory.DisActiveObject(t);
                {
                    for (int i = 0; i < _pool.Count; ++i)
                    {
                        if ((object)t == (object)_pool[i])
                        {
                            PoolDebug.Instance.LogError("!!! Restor effor has this object value,point repeat " + t.ToString());
                            return;
                        }
                    }
                }
                _pool.Add(t);
            }
            _activeNum--;
        }

        public virtual void AddObjec()
        {
            if (_pool.Count >= _maxIdleNum)
                return;
            T t = _factory.CreateObject(false);
            _pool.Add(t);
        }

        public int IdleNum()
        {
            return _pool.Count;
        }

        public int ActiveNum()
        {
            return _activeNum;
        }

        public int PoolCount()
        {
            return _pool.Count;
        }
        
        public virtual void Clear()
        {
            int count = pool.Count;
            foreach (T t in _pool)
            {
                _factory.DestroyObject(t);
            }
            _pool.Clear();

            _activeNum -= count;
            if (_activeNum < 0)
                _activeNum = 0;
        }

        public int maxIdleNum
        {
            get { return _maxIdleNum; }
        }
        protected List<T> pool
        {
            get { return _pool; }
        }
        protected IObjectFactory<T> factory
        {
            get { return _factory; }
        }
    }
}

对象生成工厂

 
using UnityEngine;
using System.Collections;

// ========================================================
    // 描 述:对象工厂,用于创建、销毁、激活、反激活对象  
    // 作 者:ZhangShouYang
    // 创建时间:#CreateTime#
    // 版 本:v 1.0
// ========================================================

namespace Hstj
{
    public interface IObjectFactory<T>
    {
        /// <summary>
        /// 创建对象
        /// </summary>
        /// <param name="isActive"></param>
        T CreateObject(bool isActive);

        /// <summary>
        /// 销毁对象
        /// </summary>
        /// <param name="t"></param>
        void DestroyObject(T t);

        /// <summary>
        /// 激活对象
        /// </summary>
        /// <param name="t"></param>
        void ActiveObject(T t);

        /// <summary>
        /// 不激活对象
        /// </summary>
        /// <param name="t"></param>
        void DisActiveObject(T t);

        /// <summary>
        /// 克隆一份
        /// </summary>
        /// <param name="t"></param>
        T CloneObject(T t);
    }

}





  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值