Files
TEngine/Books/3-4-对象池模块.md
ALEXTANG d6dcd8851c 更新文档
更新文档
2023-08-18 17:49:28 +08:00

1.1 KiB

3-4.对象池模块 - ObjectModule

对象池较中量级,在客户端开发中是一个经常使用的技术,技术点我相信大家都懂,这里不过多讨论。

使用案例

/// <summary>
/// Actor对象。
/// </summary>
public class Actor : ObjectBase
{
    /// <summary>
    /// 释放对象。
    /// </summary>
    /// <param name="isShutdown">是否是关闭对象池时触发。</param>
    protected override void Release(bool isShutdown){}
}

/// <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>
/// <returns>Actor对象实例。</returns>
public Actor CreateActor()
{
    Actor ret = null;
    if (_actorPool.CanSpawn())
    {
        ret = _actorPool.Spawn();
    }
    else
    {
        ret = MemoryPool.Acquire<Actor>();
        _actorPool.Register(ret,true);
    }

    return ret;
}