版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jinsenianhua2012/article/details/72528536
游戏中为了避免频繁的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);
}
}