对象池较中量级,在客户端开发中是一个经常使用的技术,技术点我相信大家都懂,这里不过多讨论。
使用案例
/// <summary>
/// Actor对象。
/// </summary>
public class Actor : ObjectBase
{
/// <summary>
/// 释放对象。
/// </summary>
/// <param name="isShutdown">是否是关闭对象池时触发。</param>
protected override void Release(bool isShutdown){}
/// <summary>
/// 创建Actor对象。
/// </summary>
/// <param name="actorName">对象名称。</param>
/// <param name="target">对象持有实例。</param>
/// <returns></returns>
public static Actor Create(string name, object target)
{
var actor = MemoryPool.Acquire<Actor>();
actor.Initialize(name, target);
return actor;
}
}
/// <summary>
/// Actor对象池。
/// </summary>
private IObjectPool<Actor> _actorPool;
void Start()
{
//创建允许单次获取的对象池。
_actorPool = GameModule.ObjectPool.CreateSingleSpawnObjectPool<Actor>(Utility.Text.Format("Actor Instance Pool ({0})", name));
}
/// <summary>
/// 创建Actor对象。
/// </summary>
/// <param name="actorName">对象名称。</param>
/// <param name="target">对象持有实例。</param>
/// <returns>Actor对象实例</returns>
public Actor CreateActor(string actorName, GameObject target)
{
Actor ret = null;
if (_actorPool.CanSpawn())
{
ret = _actorPool.Spawn();
}
else
{
ret = Actor.Create(actorName, target);
_actorPool.Register(ret,true);
}
return ret;
}