Compare commits

...

4 Commits

Author SHA1 Message Date
QuocHieuNguyen f1f99e3819
Merge ea28212b98 into 4c3d6938ed 2023-09-01 09:35:06 +09:00
hadashiA 4c3d6938ed
Merge pull request #484 from Cysharp/hadashiA/fix-async-enumerable
Fix problem with finally in UniTaskAsyncEnumerable.Create not being executed
2023-08-31 19:17:31 +09:00
hadashiA b4486802f2 Fix problem with part of await foreach not executing on break 2023-08-31 12:42:53 +09:00
Nguyen Quoc Hieu ea28212b98 Update docs in External Assets 2023-04-16 19:27:52 +07:00
3 changed files with 76 additions and 1 deletions

View File

@ -656,6 +656,47 @@ External Assets
---
By default, UniTask supports TextMeshPro(`BindTo(TMP_Text)` and `TMP_InputField` event extensions like standard uGUI `InputField`), DOTween(`Tween` as awaitable) and Addressables(`AsyncOperationHandle` and `AsyncOperationHandle<T>` as awaitable).
You can react to the change of the value and update it using TextMeshPro by binding the field to the `AsyncReactiveProperty`. For more infomation, please read [Awaitable Events](https://github.com/QuocHieuNguyen/UniTask#awaitable-events).
```csharp
// BindTo(TMP_Text)
public class ExampleUniTaskTMPro : MonoBehaviour
{
public TextMeshPro textMesh;
// Start is called before the first frame update
async void Start()
{
var exampleValue = new AsyncReactiveProperty<string>("Hello World");
exampleValue.ForEachAsync(x =>
{
Debug.Log(x); // print each time the property is changed
}, this.GetCancellationTokenOnDestroy()).Forget();
exampleValue.BindTo(this.textMesh); // bind to the TextMeshPro field
exampleValue.Value = "Luck"; // the value of the text field of the TextMeshPro is now "Luck"
}
}
```
You can add event to the `TMP_InputField` by using `OnSelectAsync` or `OnEndEditAsync`
```csharp
public class ExampleUniTaskInputField : MonoBehaviour
{
[SerializeField] private TMP_InputField inputField;
async UniTask Start()
{
inputField.text = "Please select here";
await inputField.OnSelectAsync();
inputField.text = ""; // the input field is empty after selecting
var value = await inputField.OnEndEditAsync();
var text = $"You just type {value}";
inputField.text = text; // the value of the input field after editing
}
}
```
There are defined in separated asmdefs like `UniTask.TextMeshPro`, `UniTask.DOTween`, `UniTask.Addressables`.
TextMeshPro and Addressables support are automatically enabled when importing their packages from package manager. However for DOTween support, it is required to import `com.demigiant.dotween` from [OpenUPM](https://openupm.com/packages/com.demigiant.dotween/) or to define `UNITASK_DOTWEEN_SUPPORT` to enable it.

View File

@ -159,6 +159,30 @@ namespace NetCoreTests.Linq
list.Should().Equal(100, 200, 300, 400);
}
[Fact]
public async Task AwaitForeachBreak()
{
var finallyCalled = false;
var enumerable = UniTaskAsyncEnumerable.Create<int>(async (writer, _) =>
{
try
{
await writer.YieldAsync(1);
}
finally
{
finallyCalled = true;
}
});
await foreach (var x in enumerable)
{
x.Should().Be(1);
break;
}
finallyCalled.Should().BeTrue();
}
async IAsyncEnumerable<int> Range(int from, int count)
{
for (int i = 0; i < count; i++)

View File

@ -52,6 +52,7 @@ namespace Cysharp.Threading.Tasks.Linq
public UniTask DisposeAsync()
{
TaskTracker.RemoveTracking(this);
writer.Dispose();
return default;
}
@ -127,7 +128,7 @@ namespace Cysharp.Threading.Tasks.Linq
}
}
sealed class AsyncWriter : IUniTaskSource, IAsyncWriter<T>
sealed class AsyncWriter : IUniTaskSource, IAsyncWriter<T>, IDisposable
{
readonly _Create enumerator;
@ -137,6 +138,15 @@ namespace Cysharp.Threading.Tasks.Linq
{
this.enumerator = enumerator;
}
public void Dispose()
{
var status = core.GetStatus(core.Version);
if (status == UniTaskStatus.Pending)
{
core.TrySetCanceled();
}
}
public void GetResult(short token)
{