Skip to content

Commit be03a80

Browse files
author
k-okawa
committed
update readme
1 parent 0dd3a31 commit be03a80

1 file changed

Lines changed: 112 additions & 1 deletion

File tree

README.md

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,112 @@
1-
# UniTaskObjectPool
1+
# UniTaskObjectPool
2+
3+
ObjectPool for UniTask.
4+
5+
AsyncObjectPool in this package conform to [UnityEngine.Pool.ObjectPool<T>](https://docs.unity3d.com/ScriptReference/Pool.ObjectPool_1.html)
6+
7+
Differences is bellow
8+
9+
- Create, OnGet and OnRelease return value is UniTask
10+
- Create, OnGet and OnRelease argument includes CancellationToken
11+
12+
## Installation
13+
### Dependency
14+
Using UniTask in the package, UniTask must be added in the project.
15+
16+
[UniTask](https://github.com/Cysharp/UniTask)
17+
18+
### PackageManager
19+
20+
#### Install via git url
21+
22+
Open Window/Package Manager, and add package from git URL...
23+
24+
```
25+
https://github.com/k-okawa/UniTaskObjectPool.git?path=Assets/Bg/UniTaskObjectPool
26+
```
27+
28+
#### Install via OpenUPM
29+
30+
```
31+
openupm add com.bg.unitaskobjectpool
32+
```
33+
34+
### UnityPackage
35+
36+
You can download unity package in [release page](https://github.com/k-okawa/UniTaskObjectPool/releases).
37+
38+
## Sample Code
39+
40+
```csharp
41+
public class SamplePoolObj : IDisposable
42+
{
43+
public static int globalIndex = 0;
44+
public readonly int id;
45+
46+
public SamplePoolObj()
47+
{
48+
id = ++globalIndex;
49+
}
50+
51+
public async UniTask GetLog(CancellationToken ct)
52+
{
53+
await UniTask.Delay(TimeSpan.FromSeconds(0.02f), cancellationToken: ct);
54+
Debug.Log($"Get from pool id:{id}");
55+
}
56+
57+
public async UniTask ReleaseLog(CancellationToken ct)
58+
{
59+
await UniTask.Delay(TimeSpan.FromSeconds(0.02f), cancellationToken: ct);
60+
Debug.Log($"Release id:{id}");
61+
}
62+
63+
public void Dispose()
64+
{
65+
Debug.Log($"Disposed id:{id}");
66+
}
67+
}
68+
69+
public static async UniTask<SamplePoolObj> CreateFunc(CancellationToken ct)
70+
{
71+
await UniTask.Delay(TimeSpan.FromSeconds(0.02f), cancellationToken: ct);
72+
return new SamplePoolObj();
73+
}
74+
75+
public static UniTask OnGet(SamplePoolObj element, CancellationToken ct)
76+
{
77+
return element.GetLog(ct);
78+
}
79+
80+
public static UniTask OnRelease(SamplePoolObj element, CancellationToken ct)
81+
{
82+
return element.ReleaseLog(ct);
83+
}
84+
85+
public static void Destroy(SamplePoolObj element)
86+
{
87+
element.Dispose();
88+
}
89+
```
90+
91+
92+
```csharp
93+
public async UniTask Sample(CancellationToken ct)
94+
{
95+
var objectPool = new AsyncObjectPool<SamplePoolObj>(
96+
CreateFunc,
97+
OnGet,
98+
OnRelease,
99+
Destroy,
100+
true, 10, 100
101+
);
102+
103+
var element = await objectPool.Get(ct);
104+
await objectPool.Release(element, ct);
105+
106+
var pooledObject = await objectPool.GetPooledObject(ct);
107+
// call element function sample
108+
await pooledObject.Get().GetLog(ct);
109+
// return to pool
110+
await pooledObject.DisposeAsync(ct);
111+
}
112+
```

0 commit comments

Comments
 (0)