mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
[+] Books
[+] Books
This commit is contained in:
47
Books/3-4-对象池模块.md
Normal file
47
Books/3-4-对象池模块.md
Normal file
@@ -0,0 +1,47 @@
|
||||
## 3-4.对象池模块 - ObjectModule
|
||||
使用案例
|
||||
``` csharp
|
||||
/// <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;
|
||||
}
|
||||
|
||||
```
|
Reference in New Issue
Block a user