mirror of
https://github.com/Cysharp/UniTask.git
synced 2026-05-26 01:40:17 +00:00
Compare commits
1 Commits
b02876e1df
...
v2.5.11
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
936b224b2b |
12
README.md
12
README.md
@@ -33,7 +33,6 @@ For advanced tips, see blog post: [Extends UnityWebRequest via async decorator p
|
||||
- [AsyncEnumerable and Async LINQ](#asyncenumerable-and-async-linq)
|
||||
- [Awaitable Events](#awaitable-events)
|
||||
- [Channel](#channel)
|
||||
- [vs Awaitable](#vs-awaitable)
|
||||
- [For Unit Testing](#for-unit-testing)
|
||||
- [ThreadPool limitation](#threadpool-limitation)
|
||||
- [IEnumerator.ToUniTask limitation](#ienumeratortounitask-limitation)
|
||||
@@ -68,7 +67,6 @@ async UniTask<string> DemoAsync()
|
||||
await SceneManager.LoadSceneAsync("scene2");
|
||||
|
||||
// .WithCancellation enables Cancel, GetCancellationTokenOnDestroy synchornizes with lifetime of GameObject
|
||||
// after Unity 2022.2, you can use `destroyCancellationToken` in MonoBehaviour
|
||||
var asset2 = await Resources.LoadAsync<TextAsset>("bar").WithCancellation(this.GetCancellationTokenOnDestroy());
|
||||
|
||||
// .ToUniTask accepts progress callback(and all options), Progress.Create is a lightweight alternative of IProgress<T>
|
||||
@@ -295,8 +293,6 @@ public class MyBehaviour : MonoBehaviour
|
||||
}
|
||||
```
|
||||
|
||||
After Unity 2022.2, Unity adds CancellationToken in [MonoBehaviour.destroyCancellationToken](https://docs.unity3d.com/ScriptReference/MonoBehaviour-destroyCancellationToken.html) and [Application.exitCancellationToken](https://docs.unity3d.com/ScriptReference/Application-exitCancellationToken.html).
|
||||
|
||||
When cancellation is detected, all methods throw `OperationCanceledException` and propagate upstream. When exception(not limited to `OperationCanceledException`) is not handled in async method, it is propagated finally to `UniTaskScheduler.UnobservedTaskException`. The default behaviour of received unhandled exception is to write log as exception. Log level can be changed using `UniTaskScheduler.UnobservedExceptionWriteLogType`. If you want to use custom behaviour, set an action to `UniTaskScheduler.UnobservedTaskException.`
|
||||
|
||||
And also `OperationCanceledException` is a special exception, this is silently ignored at `UnobservedTaskException`.
|
||||
@@ -957,14 +953,6 @@ public class AsyncMessageBroker<T> : IDisposable
|
||||
}
|
||||
```
|
||||
|
||||
vs Awaitable
|
||||
---
|
||||
Unity 6 introduces the awaitable type, [Awaitable](https://docs.unity3d.com/6000.0/Documentation/ScriptReference/Awaitable.html). To put it simply, Awaitable can be considered a subset of UniTask, and in fact, Awaitable's design was influenced by UniTask. It should be able to handle PlayerLoop-based awaits, pooled Tasks, and support for cancellation with `CancellationToken` in a similar way. With its inclusion in the standard library, you may wonder whether to continue using UniTask or migrate to Awaitable. Here's a brief guide.
|
||||
|
||||
First, the functionality provided by Awaitable is equivalent to what coroutines offer. Instead of `yield return`, you use await; `await NextFrameAsync()` replaces `yield return null`; and there are equivalents for `WaitForSeconds` and `EndOfFrame`. However, that's the extent of it. Being coroutine-based in terms of functionality, it lacks Task-based features. In practical application development using async/await, operations like `WhenAll` are essential. Additionally, UniTask enables many frame-based operations (such as `DelayFrame`) and more flexible PlayerLoopTiming control, which are not available in Awaitable. Of course, there's no Tracker Window either.
|
||||
|
||||
Therefore, I recommend using UniTask for application development. UniTask is a superset of Awaitable and includes many essential features. For library development, where you want to avoid external dependencies, using Awaitable as a return type for methods would be appropriate. Awaitable can be converted to UniTask using `AsUniTask`, so there's no issue in handling Awaitable-based functionality within the UniTask library. Of course, if you don't need to worry about dependencies, using UniTask would be the best choice even for library development.
|
||||
|
||||
For Unit Testing
|
||||
---
|
||||
Unity's `[UnityTest]` attribute can test coroutine(IEnumerator) but can not test async. `UniTask.ToCoroutine` bridges async/await to coroutine so you can test async methods.
|
||||
|
||||
464
README_CN.md
464
README_CN.md
File diff suppressed because it is too large
Load Diff
@@ -135,8 +135,8 @@ namespace Cysharp.Threading.Tasks
|
||||
bool TryReturn()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
core.Reset();
|
||||
cancellationTokenRegistration.Dispose();
|
||||
core.Reset();
|
||||
cancellationTokenRegistration = default;
|
||||
parent.triggerEvent.Remove(this);
|
||||
parent = null;
|
||||
@@ -453,8 +453,8 @@ namespace Cysharp.Threading.Tasks
|
||||
bool TryReturn()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
core.Reset();
|
||||
cancellationTokenRegistration.Dispose();
|
||||
core.Reset();
|
||||
cancellationTokenRegistration = default;
|
||||
parent.triggerEvent.Remove(this);
|
||||
parent = null;
|
||||
|
||||
@@ -353,8 +353,8 @@ namespace Cysharp.Threading.Tasks
|
||||
bool TryReturn()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
core.Reset();
|
||||
cancellationRegistration.Dispose();
|
||||
core.Reset();
|
||||
|
||||
RestoreOriginalCallback();
|
||||
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
#if UNITASK_TEXTMESHPRO_SUPPORT
|
||||
|
||||
using System;
|
||||
using System.Threading;
|
||||
using TMPro;
|
||||
|
||||
namespace Cysharp.Threading.Tasks
|
||||
{
|
||||
public static partial class TextMeshProAsyncExtensions
|
||||
{
|
||||
public static IAsyncValueChangedEventHandler<int> GetAsyncValueChangedEventHandler(this TMP_Dropdown dropdown)
|
||||
{
|
||||
return new AsyncUnityEventHandler<int>(dropdown.onValueChanged, dropdown.GetCancellationTokenOnDestroy(), false);
|
||||
}
|
||||
|
||||
public static IAsyncValueChangedEventHandler<int> GetAsyncValueChangedEventHandler(this TMP_Dropdown dropdown, CancellationToken cancellationToken)
|
||||
{
|
||||
return new AsyncUnityEventHandler<int>(dropdown.onValueChanged, cancellationToken, false);
|
||||
}
|
||||
|
||||
public static UniTask<int> OnValueChangedAsync(this TMP_Dropdown dropdown)
|
||||
{
|
||||
return new AsyncUnityEventHandler<int>(dropdown.onValueChanged, dropdown.GetCancellationTokenOnDestroy(), true).OnInvokeAsync();
|
||||
}
|
||||
|
||||
public static UniTask<int> OnValueChangedAsync(this TMP_Dropdown dropdown, CancellationToken cancellationToken)
|
||||
{
|
||||
return new AsyncUnityEventHandler<int>(dropdown.onValueChanged, cancellationToken, true).OnInvokeAsync();
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<int> OnValueChangedAsAsyncEnumerable(this TMP_Dropdown dropdown)
|
||||
{
|
||||
return new UnityEventHandlerAsyncEnumerable<int>(dropdown.onValueChanged, dropdown.GetCancellationTokenOnDestroy());
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<int> OnValueChangedAsAsyncEnumerable(this TMP_Dropdown dropdown, CancellationToken cancellationToken)
|
||||
{
|
||||
return new UnityEventHandlerAsyncEnumerable<int>(dropdown.onValueChanged, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b858132b120c42b99cac55fd06dd32d3
|
||||
timeCreated: 1736165018
|
||||
@@ -255,7 +255,7 @@ namespace Cysharp.Threading.Tasks
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
|
||||
if (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
TryReturn();
|
||||
}
|
||||
@@ -289,6 +289,7 @@ namespace Cysharp.Threading.Tasks
|
||||
return false;
|
||||
}
|
||||
|
||||
cancellationTokenRegistration.Dispose();
|
||||
core.TrySetResult(null);
|
||||
return false;
|
||||
}
|
||||
@@ -296,9 +297,9 @@ namespace Cysharp.Threading.Tasks
|
||||
bool TryReturn()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
cancellationTokenRegistration.Dispose();
|
||||
core.Reset();
|
||||
cancellationToken = default;
|
||||
cancellationTokenRegistration.Dispose();
|
||||
cancelImmediately = default;
|
||||
return pool.TryPush(this);
|
||||
}
|
||||
@@ -366,7 +367,7 @@ namespace Cysharp.Threading.Tasks
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
|
||||
if (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
TryReturn();
|
||||
}
|
||||
@@ -412,9 +413,9 @@ namespace Cysharp.Threading.Tasks
|
||||
bool TryReturn()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
cancellationTokenRegistration.Dispose();
|
||||
core.Reset();
|
||||
cancellationToken = default;
|
||||
cancellationTokenRegistration.Dispose();
|
||||
return pool.TryPush(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,6 +42,7 @@ namespace Cysharp.Threading.Tasks
|
||||
return new UniTask(AsyncOperationConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, cancelImmediately, out var token), token);
|
||||
}
|
||||
|
||||
#if !UNITY_2023_1_OR_NEWER
|
||||
public struct AsyncOperationAwaiter : ICriticalNotifyCompletion
|
||||
{
|
||||
AsyncOperation asyncOperation;
|
||||
@@ -81,6 +82,7 @@ namespace Cysharp.Threading.Tasks
|
||||
asyncOperation.completed += continuationAction;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
sealed class AsyncOperationConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode<AsyncOperationConfiguredSource>
|
||||
{
|
||||
@@ -99,6 +101,7 @@ namespace Cysharp.Threading.Tasks
|
||||
CancellationTokenRegistration cancellationTokenRegistration;
|
||||
bool cancelImmediately;
|
||||
bool completed;
|
||||
bool allowReturn;
|
||||
|
||||
UniTaskCompletionSourceCore<AsyncUnit> core;
|
||||
|
||||
@@ -126,7 +129,8 @@ namespace Cysharp.Threading.Tasks
|
||||
result.cancellationToken = cancellationToken;
|
||||
result.cancelImmediately = cancelImmediately;
|
||||
result.completed = false;
|
||||
|
||||
result.allowReturn = false;
|
||||
|
||||
asyncOperation.completed += result.continuationAction;
|
||||
|
||||
if (cancelImmediately && cancellationToken.CanBeCanceled)
|
||||
@@ -154,9 +158,16 @@ namespace Cysharp.Threading.Tasks
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
|
||||
if (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
TryReturn();
|
||||
if (allowReturn)
|
||||
{
|
||||
TryReturn();
|
||||
}
|
||||
else
|
||||
{
|
||||
allowReturn = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -183,9 +194,15 @@ namespace Cysharp.Threading.Tasks
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
// Already completed
|
||||
if (completed || asyncOperation == null)
|
||||
if (allowReturn)
|
||||
{
|
||||
TryReturn();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (completed)
|
||||
{
|
||||
allowReturn = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -202,6 +219,8 @@ namespace Cysharp.Threading.Tasks
|
||||
|
||||
if (asyncOperation.isDone)
|
||||
{
|
||||
cancellationTokenRegistration.Dispose();
|
||||
allowReturn = true;
|
||||
core.TrySetResult(AsyncUnit.Default);
|
||||
return false;
|
||||
}
|
||||
@@ -212,29 +231,31 @@ namespace Cysharp.Threading.Tasks
|
||||
bool TryReturn()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
cancellationTokenRegistration.Dispose();
|
||||
core.Reset();
|
||||
asyncOperation.completed -= continuationAction;
|
||||
asyncOperation = default;
|
||||
progress = default;
|
||||
cancellationToken = default;
|
||||
cancellationTokenRegistration.Dispose();
|
||||
cancelImmediately = default;
|
||||
return pool.TryPush(this);
|
||||
}
|
||||
|
||||
void Continuation(AsyncOperation _)
|
||||
{
|
||||
if (completed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
completed = true;
|
||||
cancellationTokenRegistration.Dispose();
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
core.TrySetCanceled(cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (progress != null)
|
||||
{
|
||||
progress.Report(asyncOperation.progress);
|
||||
}
|
||||
|
||||
core.TrySetResult(AsyncUnit.Default);
|
||||
}
|
||||
}
|
||||
@@ -329,6 +350,7 @@ namespace Cysharp.Threading.Tasks
|
||||
CancellationTokenRegistration cancellationTokenRegistration;
|
||||
bool cancelImmediately;
|
||||
bool completed;
|
||||
bool allowReturn;
|
||||
|
||||
UniTaskCompletionSourceCore<UnityEngine.Object> core;
|
||||
|
||||
@@ -356,7 +378,8 @@ namespace Cysharp.Threading.Tasks
|
||||
result.cancellationToken = cancellationToken;
|
||||
result.cancelImmediately = cancelImmediately;
|
||||
result.completed = false;
|
||||
|
||||
result.allowReturn = false;
|
||||
|
||||
asyncOperation.completed += result.continuationAction;
|
||||
|
||||
if (cancelImmediately && cancellationToken.CanBeCanceled)
|
||||
@@ -384,9 +407,16 @@ namespace Cysharp.Threading.Tasks
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
|
||||
if (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
TryReturn();
|
||||
if (allowReturn)
|
||||
{
|
||||
TryReturn();
|
||||
}
|
||||
else
|
||||
{
|
||||
allowReturn = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -417,9 +447,15 @@ namespace Cysharp.Threading.Tasks
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
// Already completed
|
||||
if (completed || asyncOperation == null)
|
||||
if (allowReturn)
|
||||
{
|
||||
TryReturn();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (completed)
|
||||
{
|
||||
allowReturn = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -436,6 +472,8 @@ namespace Cysharp.Threading.Tasks
|
||||
|
||||
if (asyncOperation.isDone)
|
||||
{
|
||||
cancellationTokenRegistration.Dispose();
|
||||
allowReturn = true;
|
||||
core.TrySetResult(asyncOperation.asset);
|
||||
return false;
|
||||
}
|
||||
@@ -446,29 +484,31 @@ namespace Cysharp.Threading.Tasks
|
||||
bool TryReturn()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
cancellationTokenRegistration.Dispose();
|
||||
core.Reset();
|
||||
asyncOperation.completed -= continuationAction;
|
||||
asyncOperation = default;
|
||||
progress = default;
|
||||
cancellationToken = default;
|
||||
cancellationTokenRegistration.Dispose();
|
||||
cancelImmediately = default;
|
||||
return pool.TryPush(this);
|
||||
}
|
||||
|
||||
void Continuation(AsyncOperation _)
|
||||
{
|
||||
if (completed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
completed = true;
|
||||
cancellationTokenRegistration.Dispose();
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
core.TrySetCanceled(cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (progress != null)
|
||||
{
|
||||
progress.Report(asyncOperation.progress);
|
||||
}
|
||||
|
||||
core.TrySetResult(asyncOperation.asset);
|
||||
}
|
||||
}
|
||||
@@ -564,6 +604,7 @@ namespace Cysharp.Threading.Tasks
|
||||
CancellationTokenRegistration cancellationTokenRegistration;
|
||||
bool cancelImmediately;
|
||||
bool completed;
|
||||
bool allowReturn;
|
||||
|
||||
UniTaskCompletionSourceCore<UnityEngine.Object> core;
|
||||
|
||||
@@ -591,7 +632,8 @@ namespace Cysharp.Threading.Tasks
|
||||
result.cancellationToken = cancellationToken;
|
||||
result.cancelImmediately = cancelImmediately;
|
||||
result.completed = false;
|
||||
|
||||
result.allowReturn = false;
|
||||
|
||||
asyncOperation.completed += result.continuationAction;
|
||||
|
||||
if (cancelImmediately && cancellationToken.CanBeCanceled)
|
||||
@@ -619,9 +661,16 @@ namespace Cysharp.Threading.Tasks
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
|
||||
if (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
TryReturn();
|
||||
if (allowReturn)
|
||||
{
|
||||
TryReturn();
|
||||
}
|
||||
else
|
||||
{
|
||||
allowReturn = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -652,9 +701,15 @@ namespace Cysharp.Threading.Tasks
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
// Already completed
|
||||
if (completed || asyncOperation == null)
|
||||
if (allowReturn)
|
||||
{
|
||||
TryReturn();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (completed)
|
||||
{
|
||||
allowReturn = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -671,6 +726,8 @@ namespace Cysharp.Threading.Tasks
|
||||
|
||||
if (asyncOperation.isDone)
|
||||
{
|
||||
cancellationTokenRegistration.Dispose();
|
||||
allowReturn = true;
|
||||
core.TrySetResult(asyncOperation.asset);
|
||||
return false;
|
||||
}
|
||||
@@ -681,29 +738,31 @@ namespace Cysharp.Threading.Tasks
|
||||
bool TryReturn()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
cancellationTokenRegistration.Dispose();
|
||||
core.Reset();
|
||||
asyncOperation.completed -= continuationAction;
|
||||
asyncOperation = default;
|
||||
progress = default;
|
||||
cancellationToken = default;
|
||||
cancellationTokenRegistration.Dispose();
|
||||
cancelImmediately = default;
|
||||
return pool.TryPush(this);
|
||||
}
|
||||
|
||||
void Continuation(AsyncOperation _)
|
||||
{
|
||||
if (completed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
completed = true;
|
||||
cancellationTokenRegistration.Dispose();
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
core.TrySetCanceled(cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (progress != null)
|
||||
{
|
||||
progress.Report(asyncOperation.progress);
|
||||
}
|
||||
|
||||
core.TrySetResult(asyncOperation.asset);
|
||||
}
|
||||
}
|
||||
@@ -800,6 +859,7 @@ namespace Cysharp.Threading.Tasks
|
||||
CancellationTokenRegistration cancellationTokenRegistration;
|
||||
bool cancelImmediately;
|
||||
bool completed;
|
||||
bool allowReturn;
|
||||
|
||||
UniTaskCompletionSourceCore<AssetBundle> core;
|
||||
|
||||
@@ -827,7 +887,8 @@ namespace Cysharp.Threading.Tasks
|
||||
result.cancellationToken = cancellationToken;
|
||||
result.cancelImmediately = cancelImmediately;
|
||||
result.completed = false;
|
||||
|
||||
result.allowReturn = false;
|
||||
|
||||
asyncOperation.completed += result.continuationAction;
|
||||
|
||||
if (cancelImmediately && cancellationToken.CanBeCanceled)
|
||||
@@ -855,9 +916,16 @@ namespace Cysharp.Threading.Tasks
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
|
||||
if (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
TryReturn();
|
||||
if (allowReturn)
|
||||
{
|
||||
TryReturn();
|
||||
}
|
||||
else
|
||||
{
|
||||
allowReturn = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -888,9 +956,15 @@ namespace Cysharp.Threading.Tasks
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
// Already completed
|
||||
if (completed || asyncOperation == null)
|
||||
if (allowReturn)
|
||||
{
|
||||
TryReturn();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (completed)
|
||||
{
|
||||
allowReturn = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -907,6 +981,8 @@ namespace Cysharp.Threading.Tasks
|
||||
|
||||
if (asyncOperation.isDone)
|
||||
{
|
||||
cancellationTokenRegistration.Dispose();
|
||||
allowReturn = true;
|
||||
core.TrySetResult(asyncOperation.assetBundle);
|
||||
return false;
|
||||
}
|
||||
@@ -916,30 +992,32 @@ namespace Cysharp.Threading.Tasks
|
||||
|
||||
bool TryReturn()
|
||||
{
|
||||
cancellationTokenRegistration.Dispose();
|
||||
TaskTracker.RemoveTracking(this);
|
||||
core.Reset();
|
||||
asyncOperation.completed -= continuationAction;
|
||||
asyncOperation = default;
|
||||
progress = default;
|
||||
cancellationToken = default;
|
||||
cancellationTokenRegistration.Dispose();
|
||||
cancelImmediately = default;
|
||||
return pool.TryPush(this);
|
||||
}
|
||||
|
||||
void Continuation(AsyncOperation _)
|
||||
{
|
||||
if (completed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
completed = true;
|
||||
cancellationTokenRegistration.Dispose();
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
core.TrySetCanceled(cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (progress != null)
|
||||
{
|
||||
progress.Report(asyncOperation.progress);
|
||||
}
|
||||
|
||||
core.TrySetResult(asyncOperation.assetBundle);
|
||||
}
|
||||
}
|
||||
@@ -1051,6 +1129,7 @@ namespace Cysharp.Threading.Tasks
|
||||
CancellationTokenRegistration cancellationTokenRegistration;
|
||||
bool cancelImmediately;
|
||||
bool completed;
|
||||
bool allowReturn;
|
||||
|
||||
UniTaskCompletionSourceCore<UnityWebRequest> core;
|
||||
|
||||
@@ -1078,7 +1157,8 @@ namespace Cysharp.Threading.Tasks
|
||||
result.cancellationToken = cancellationToken;
|
||||
result.cancelImmediately = cancelImmediately;
|
||||
result.completed = false;
|
||||
|
||||
result.allowReturn = false;
|
||||
|
||||
asyncOperation.completed += result.continuationAction;
|
||||
|
||||
if (cancelImmediately && cancellationToken.CanBeCanceled)
|
||||
@@ -1107,9 +1187,16 @@ namespace Cysharp.Threading.Tasks
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
|
||||
if (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
TryReturn();
|
||||
if (allowReturn)
|
||||
{
|
||||
TryReturn();
|
||||
}
|
||||
else
|
||||
{
|
||||
allowReturn = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -1140,15 +1227,20 @@ namespace Cysharp.Threading.Tasks
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
// Already completed
|
||||
if (completed || asyncOperation == null)
|
||||
if (allowReturn)
|
||||
{
|
||||
TryReturn();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (completed)
|
||||
{
|
||||
allowReturn = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
asyncOperation.webRequest.Abort();
|
||||
core.TrySetCanceled(cancellationToken);
|
||||
return false;
|
||||
}
|
||||
@@ -1160,14 +1252,9 @@ namespace Cysharp.Threading.Tasks
|
||||
|
||||
if (asyncOperation.isDone)
|
||||
{
|
||||
if (asyncOperation.webRequest.IsError())
|
||||
{
|
||||
core.TrySetException(new UnityWebRequestException(asyncOperation.webRequest));
|
||||
}
|
||||
else
|
||||
{
|
||||
core.TrySetResult(asyncOperation.webRequest);
|
||||
}
|
||||
cancellationTokenRegistration.Dispose();
|
||||
allowReturn = true;
|
||||
core.TrySetResult(asyncOperation.webRequest);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1176,24 +1263,22 @@ namespace Cysharp.Threading.Tasks
|
||||
|
||||
bool TryReturn()
|
||||
{
|
||||
cancellationTokenRegistration.Dispose();
|
||||
TaskTracker.RemoveTracking(this);
|
||||
core.Reset();
|
||||
asyncOperation.completed -= continuationAction;
|
||||
asyncOperation = default;
|
||||
progress = default;
|
||||
cancellationToken = default;
|
||||
cancellationTokenRegistration.Dispose();
|
||||
cancelImmediately = default;
|
||||
return pool.TryPush(this);
|
||||
}
|
||||
|
||||
void Continuation(AsyncOperation _)
|
||||
{
|
||||
if (completed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
completed = true;
|
||||
cancellationTokenRegistration.Dispose();
|
||||
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
core.TrySetCanceled(cancellationToken);
|
||||
@@ -1204,6 +1289,11 @@ namespace Cysharp.Threading.Tasks
|
||||
}
|
||||
else
|
||||
{
|
||||
if (progress != null)
|
||||
{
|
||||
progress.Report(asyncOperation.progress);
|
||||
}
|
||||
|
||||
core.TrySetResult(asyncOperation.webRequest);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
"name": "com.cysharp.unitask",
|
||||
"displayName": "UniTask",
|
||||
"author": { "name": "Cysharp, Inc.", "url": "https://cysharp.co.jp/en/" },
|
||||
"version": "2.5.10",
|
||||
"version": "2.5.9",
|
||||
"unity": "2018.4",
|
||||
"description": "Provides an efficient async/await integration to Unity.",
|
||||
"keywords": [ "async/await", "async", "Task", "UniTask" ],
|
||||
|
||||
8
src/UniTask/Assets/Resources.meta
Normal file
8
src/UniTask/Assets/Resources.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b055f3837a55b4a44abf9bf4bcb3594f
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
1
src/UniTask/Assets/Resources/test2.txt
Normal file
1
src/UniTask/Assets/Resources/test2.txt
Normal file
@@ -0,0 +1 @@
|
||||
MyTEST
|
||||
7
src/UniTask/Assets/Resources/test2.txt.meta
Normal file
7
src/UniTask/Assets/Resources/test2.txt.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 49ccc900467d35543bb602a3de4d786a
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -21,7 +21,6 @@ using System.Linq.Expressions;
|
||||
using UnityEngine.Events;
|
||||
|
||||
|
||||
|
||||
// using DG.Tweening;
|
||||
|
||||
|
||||
@@ -180,7 +179,7 @@ public class SandboxMain : MonoBehaviour
|
||||
|
||||
|
||||
//UnityAction action;
|
||||
|
||||
|
||||
|
||||
return 10;
|
||||
}
|
||||
@@ -592,69 +591,78 @@ public class SandboxMain : MonoBehaviour
|
||||
|
||||
async UniTaskVoid Start()
|
||||
{
|
||||
await new WhenEachTest().Each();
|
||||
|
||||
|
||||
// UniTask.Delay(TimeSpan.FromSeconds(1)).TimeoutWithoutException
|
||||
|
||||
|
||||
var currentLoop = PlayerLoop.GetDefaultPlayerLoop();
|
||||
PlayerLoopHelper.Initialize(ref currentLoop, InjectPlayerLoopTimings.Minimum); // minimum is Update | FixedUpdate | LastPostLateUpdate
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// TestAsync(cts.Token).Forget();
|
||||
|
||||
okButton.onClick.AddListener(UniTask.UnityAction(async () =>
|
||||
var p = Progress.Create<float>(x =>
|
||||
{
|
||||
await UniTask.WaitForEndOfFrame(this);
|
||||
var texture = new Texture2D(Screen.width, Screen.height);
|
||||
texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
|
||||
texture.Apply();
|
||||
Debug.Log(x);
|
||||
});
|
||||
var foo = await Resources.LoadAsync("test2").ToUniTask(progress: p) as TextAsset;
|
||||
Debug.Log(foo.text);
|
||||
//var foo2 = await Resources.LoadAsync("test2").ToUniTask() as TextAsset;
|
||||
//Debug.Log(foo2.text); // onemore?
|
||||
|
||||
var jpg = texture.EncodeToJPG();
|
||||
File.WriteAllBytes("testscreencapture.jpg", jpg);
|
||||
Debug.Log("ok?");
|
||||
|
||||
//var texture = ScreenCapture.CaptureScreenshotAsTexture();
|
||||
//if (texture == null)
|
||||
//{
|
||||
// Debug.Log("fail");
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// var jpg = texture.EncodeToJPG();
|
||||
// File.WriteAllBytes("testscreencapture.jpg", jpg);
|
||||
// Debug.Log("ok?");
|
||||
//}
|
||||
}));
|
||||
|
||||
cancelButton.onClick.AddListener(UniTask.UnityAction(async () =>
|
||||
{
|
||||
//clickCancelSource.Cancel();
|
||||
//await new WhenEachTest().Each();
|
||||
|
||||
//RunCheck(PlayerLoopTiming.Initialization).Forget();
|
||||
//RunCheck(PlayerLoopTiming.LastInitialization).Forget();
|
||||
//RunCheck(PlayerLoopTiming.EarlyUpdate).Forget();
|
||||
//RunCheck(PlayerLoopTiming.LastEarlyUpdate).Forget();
|
||||
//RunCheck(PlayerLoopTiming.FixedUpdate).Forget();
|
||||
//RunCheck(PlayerLoopTiming.LastFixedUpdate).Forget();
|
||||
//RunCheck(PlayerLoopTiming.PreUpdate).Forget();
|
||||
//RunCheck(PlayerLoopTiming.LastPreUpdate).Forget();
|
||||
//RunCheck(PlayerLoopTiming.Update).Forget();
|
||||
//RunCheck(PlayerLoopTiming.LastUpdate).Forget();
|
||||
//RunCheck(PlayerLoopTiming.PreLateUpdate).Forget();
|
||||
//RunCheck(PlayerLoopTiming.LastPreLateUpdate).Forget();
|
||||
//RunCheck(PlayerLoopTiming.PostLateUpdate).Forget();
|
||||
//RunCheck(PlayerLoopTiming.LastPostLateUpdate).Forget();
|
||||
|
||||
await UniTask.Yield();
|
||||
}));
|
||||
//// UniTask.Delay(TimeSpan.FromSeconds(1)).TimeoutWithoutException
|
||||
|
||||
await UniTask.Yield();
|
||||
|
||||
//var currentLoop = PlayerLoop.GetDefaultPlayerLoop();
|
||||
//PlayerLoopHelper.Initialize(ref currentLoop, InjectPlayerLoopTimings.Minimum); // minimum is Update | FixedUpdate | LastPostLateUpdate
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//// TestAsync(cts.Token).Forget();
|
||||
|
||||
//okButton.onClick.AddListener(UniTask.UnityAction(async () =>
|
||||
//{
|
||||
// await UniTask.WaitForEndOfFrame(this);
|
||||
// var texture = new Texture2D(Screen.width, Screen.height);
|
||||
// texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
|
||||
// texture.Apply();
|
||||
|
||||
// var jpg = texture.EncodeToJPG();
|
||||
// File.WriteAllBytes("testscreencapture.jpg", jpg);
|
||||
// Debug.Log("ok?");
|
||||
|
||||
// //var texture = ScreenCapture.CaptureScreenshotAsTexture();
|
||||
// //if (texture == null)
|
||||
// //{
|
||||
// // Debug.Log("fail");
|
||||
// //}
|
||||
// //else
|
||||
// //{
|
||||
// // var jpg = texture.EncodeToJPG();
|
||||
// // File.WriteAllBytes("testscreencapture.jpg", jpg);
|
||||
// // Debug.Log("ok?");
|
||||
// //}
|
||||
//}));
|
||||
|
||||
//cancelButton.onClick.AddListener(UniTask.UnityAction(async () =>
|
||||
//{
|
||||
// //clickCancelSource.Cancel();
|
||||
|
||||
// //RunCheck(PlayerLoopTiming.Initialization).Forget();
|
||||
// //RunCheck(PlayerLoopTiming.LastInitialization).Forget();
|
||||
// //RunCheck(PlayerLoopTiming.EarlyUpdate).Forget();
|
||||
// //RunCheck(PlayerLoopTiming.LastEarlyUpdate).Forget();
|
||||
// //RunCheck(PlayerLoopTiming.FixedUpdate).Forget();
|
||||
// //RunCheck(PlayerLoopTiming.LastFixedUpdate).Forget();
|
||||
// //RunCheck(PlayerLoopTiming.PreUpdate).Forget();
|
||||
// //RunCheck(PlayerLoopTiming.LastPreUpdate).Forget();
|
||||
// //RunCheck(PlayerLoopTiming.Update).Forget();
|
||||
// //RunCheck(PlayerLoopTiming.LastUpdate).Forget();
|
||||
// //RunCheck(PlayerLoopTiming.PreLateUpdate).Forget();
|
||||
// //RunCheck(PlayerLoopTiming.LastPreLateUpdate).Forget();
|
||||
// //RunCheck(PlayerLoopTiming.PostLateUpdate).Forget();
|
||||
// //RunCheck(PlayerLoopTiming.LastPostLateUpdate).Forget();
|
||||
|
||||
// await UniTask.Yield();
|
||||
//}));
|
||||
}
|
||||
|
||||
async UniTaskVoid RunCheck(PlayerLoopTiming timing)
|
||||
|
||||
Reference in New Issue
Block a user