Compare commits

...

9 Commits

Author SHA1 Message Date
neuecc
936b224b2b WIP reuse and cancellation 2024-10-25 11:51:55 +09:00
neuecc
9a3ec31533 Merge remote-tracking branch 'origin/master' 2024-10-03 09:41:47 +09:00
neuecc
37d8f4f48e C# 8 #624 2024-10-03 09:41:30 +09:00
github-actions[bot]
27a0c06ede feat: Update package.json to 2.5.9 2024-10-01 06:16:52 +00:00
neuecc
e4082ecd75 Merge remote-tracking branch 'origin/master' 2024-10-01 14:59:02 +09:00
neuecc
3b0fd784ff meta 2024-10-01 14:58:57 +09:00
neuecc
dc9ebfd765 Add AsyncInstantiateOperation.WithCancellation, ToUniTask for Unity 2022.3.20/Unity 2022.3 or newer 2024-10-01 14:50:13 +09:00
Yoshifumi Kawai
005c83fbd7 Merge pull request #623 from Cysharp/feature/ci
ci: remove Unity 2021 LTS from Build matrix
2024-10-01 10:44:30 +09:00
Ikiru Yoshizaki
eaa553dc83 ci: remove Unity 2021 LTS from Build matrix 2024-09-24 18:30:54 +09:00
14 changed files with 668 additions and 145 deletions

View File

@@ -24,7 +24,7 @@ jobs:
fail-fast: false
max-parallel: 2
matrix:
unity: ["2021.3.41f1", "2022.3.39f1", "6000.0.12f1"] # Test with LTS
unity: ["2022.3.39f1", "6000.0.12f1"] # Test with LTS
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
@@ -44,7 +44,7 @@ jobs:
# Execute scripts: Export Package
# /opt/Unity/Editor/Unity -quit -batchmode -nographics -silent-crashes -logFile -projectPath . -executeMethod PackageExporter.Export
- name: Build Unity (.unitypacakge)
if: ${{ startsWith(matrix.unity, '2021') }} # only execute once
if: ${{ startsWith(matrix.unity, '2022') }} # only execute once
uses: Cysharp/Actions/.github/actions/unity-builder@main
env:
UNITY_EMAIL: ${{ steps.op-load-secret.outputs.UNITY_EMAIL }}

View File

@@ -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;

View File

@@ -353,8 +353,8 @@ namespace Cysharp.Threading.Tasks
bool TryReturn()
{
TaskTracker.RemoveTracking(this);
core.Reset();
cancellationRegistration.Dispose();
core.Reset();
RestoreOriginalCallback();

View File

@@ -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);
}
}

View File

@@ -39,7 +39,7 @@ namespace Cysharp.Threading.Tasks
public WhenEachResult(Exception exception)
{
if (exception == null) throw new ArgumentNullException(nameof(exception));
this.Result = default!;
this.Result = default;
this.Exception = exception;
}
@@ -144,24 +144,24 @@ namespace Cysharp.Threading.Tasks
{
RunWhenEachTask(self, array[i], length).Forget();
}
}
static async UniTaskVoid RunWhenEachTask(Enumerator self, UniTask<T> task, int length)
static async UniTaskVoid RunWhenEachTask(Enumerator self, UniTask<T> task, int length)
{
try
{
try
{
var result = await task;
self.channel.Writer.TryWrite(new WhenEachResult<T>(result));
}
catch (Exception ex)
{
self.channel.Writer.TryWrite(new WhenEachResult<T>(ex));
}
var result = await task;
self.channel.Writer.TryWrite(new WhenEachResult<T>(result));
}
catch (Exception ex)
{
self.channel.Writer.TryWrite(new WhenEachResult<T>(ex));
}
if (Interlocked.Increment(ref self.completeCount) == length)
{
self.state = WhenEachState.Completed;
self.channel.Writer.TryComplete();
}
if (Interlocked.Increment(ref self.completeCount) == length)
{
self.state = WhenEachState.Completed;
self.channel.Writer.TryComplete();
}
}

View File

@@ -1,2 +1,11 @@
fileFormatVersion: 2
guid: 7cac24fdda5112047a1cd3dd66b542c4
guid: 7cac24fdda5112047a1cd3dd66b542c4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,386 @@
// AsyncInstantiateOperation was added since Unity 2022.3.20 / 2023.3.0b7
#if UNITY_2022_3 && !(UNITY_2022_3_0 || UNITY_2022_3_1 || UNITY_2022_3_2 || UNITY_2022_3_3 || UNITY_2022_3_4 || UNITY_2022_3_5 || UNITY_2022_3_6 || UNITY_2022_3_7 || UNITY_2022_3_8 || UNITY_2022_3_9 || UNITY_2022_3_10 || UNITY_2022_3_11 || UNITY_2022_3_12 || UNITY_2022_3_13 || UNITY_2022_3_14 || UNITY_2022_3_15 || UNITY_2022_3_16 || UNITY_2022_3_17 || UNITY_2022_3_18 || UNITY_2022_3_19)
#define UNITY_2022_SUPPORT
#endif
#if UNITY_2022_SUPPORT || UNITY_2023_3_OR_NEWER
using Cysharp.Threading.Tasks.Internal;
using System;
using System.Threading;
using UnityEngine;
namespace Cysharp.Threading.Tasks
{
public static class AsyncInstantiateOperationExtensions
{
// AsyncInstantiateOperation<T> has GetAwaiter so no need to impl
// public static UniTask<T[]>.Awaiter GetAwaiter<T>(this AsyncInstantiateOperation<T> operation) where T : Object
public static UniTask<UnityEngine.Object[]> WithCancellation<T>(this AsyncInstantiateOperation asyncOperation, CancellationToken cancellationToken)
{
return ToUniTask(asyncOperation, cancellationToken: cancellationToken);
}
public static UniTask<UnityEngine.Object[]> WithCancellation<T>(this AsyncInstantiateOperation asyncOperation, CancellationToken cancellationToken, bool cancelImmediately)
{
return ToUniTask(asyncOperation, cancellationToken: cancellationToken, cancelImmediately: cancelImmediately);
}
public static UniTask<UnityEngine.Object[]> ToUniTask(this AsyncInstantiateOperation asyncOperation, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false)
{
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled<UnityEngine.Object[]>(cancellationToken);
if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.Result);
return new UniTask<UnityEngine.Object[]>(AsyncInstantiateOperationConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, cancelImmediately, out var token), token);
}
public static UniTask<T[]> WithCancellation<T>(this AsyncInstantiateOperation<T> asyncOperation, CancellationToken cancellationToken)
where T : UnityEngine.Object
{
return ToUniTask(asyncOperation, cancellationToken: cancellationToken);
}
public static UniTask<T[]> WithCancellation<T>(this AsyncInstantiateOperation<T> asyncOperation, CancellationToken cancellationToken, bool cancelImmediately)
where T : UnityEngine.Object
{
return ToUniTask(asyncOperation, cancellationToken: cancellationToken, cancelImmediately: cancelImmediately);
}
public static UniTask<T[]> ToUniTask<T>(this AsyncInstantiateOperation<T> asyncOperation, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false)
where T : UnityEngine.Object
{
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled<T[]>(cancellationToken);
if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.Result);
return new UniTask<T[]>(AsyncInstantiateOperationConfiguredSource<T>.Create(asyncOperation, timing, progress, cancellationToken, cancelImmediately, out var token), token);
}
sealed class AsyncInstantiateOperationConfiguredSource : IUniTaskSource<UnityEngine.Object[]>, IPlayerLoopItem, ITaskPoolNode<AsyncInstantiateOperationConfiguredSource>
{
static TaskPool<AsyncInstantiateOperationConfiguredSource> pool;
AsyncInstantiateOperationConfiguredSource nextNode;
public ref AsyncInstantiateOperationConfiguredSource NextNode => ref nextNode;
static AsyncInstantiateOperationConfiguredSource()
{
TaskPool.RegisterSizeGetter(typeof(AsyncInstantiateOperationConfiguredSource), () => pool.Size);
}
AsyncInstantiateOperation asyncOperation;
IProgress<float> progress;
CancellationToken cancellationToken;
CancellationTokenRegistration cancellationTokenRegistration;
bool cancelImmediately;
bool completed;
UniTaskCompletionSourceCore<UnityEngine.Object[]> core;
Action<AsyncOperation> continuationAction;
AsyncInstantiateOperationConfiguredSource()
{
continuationAction = Continuation;
}
public static IUniTaskSource<UnityEngine.Object[]> Create(AsyncInstantiateOperation asyncOperation, PlayerLoopTiming timing, IProgress<float> progress, CancellationToken cancellationToken, bool cancelImmediately, out short token)
{
if (cancellationToken.IsCancellationRequested)
{
return AutoResetUniTaskCompletionSource<UnityEngine.Object[]>.CreateFromCanceled(cancellationToken, out token);
}
if (!pool.TryPop(out var result))
{
result = new AsyncInstantiateOperationConfiguredSource();
}
result.asyncOperation = asyncOperation;
result.progress = progress;
result.cancellationToken = cancellationToken;
result.cancelImmediately = cancelImmediately;
result.completed = false;
asyncOperation.completed += result.continuationAction;
if (cancelImmediately && cancellationToken.CanBeCanceled)
{
result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state =>
{
var source = (AsyncInstantiateOperationConfiguredSource)state;
source.core.TrySetCanceled(source.cancellationToken);
}, result);
}
TaskTracker.TrackActiveTask(result, 3);
PlayerLoopHelper.AddAction(timing, result);
token = result.core.Version;
return result;
}
public UnityEngine.Object[] GetResult(short token)
{
try
{
return core.GetResult(token);
}
finally
{
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
{
TryReturn();
}
else
{
TaskTracker.RemoveTracking(this);
}
}
}
void IUniTaskSource.GetResult(short token)
{
GetResult(token);
}
public UniTaskStatus GetStatus(short token)
{
return core.GetStatus(token);
}
public UniTaskStatus UnsafeGetStatus()
{
return core.UnsafeGetStatus();
}
public void OnCompleted(Action<object> continuation, object state, short token)
{
core.OnCompleted(continuation, state, token);
}
public bool MoveNext()
{
// Already completed
if (completed || asyncOperation == null)
{
return false;
}
if (cancellationToken.IsCancellationRequested)
{
core.TrySetCanceled(cancellationToken);
return false;
}
if (progress != null)
{
progress.Report(asyncOperation.progress);
}
if (asyncOperation.isDone)
{
core.TrySetResult(asyncOperation.Result);
return false;
}
return true;
}
bool TryReturn()
{
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;
if (cancellationToken.IsCancellationRequested)
{
core.TrySetCanceled(cancellationToken);
}
else
{
core.TrySetResult(asyncOperation.Result);
}
}
}
sealed class AsyncInstantiateOperationConfiguredSource<T> : IUniTaskSource<T[]>, IPlayerLoopItem, ITaskPoolNode<AsyncInstantiateOperationConfiguredSource<T>>
where T : UnityEngine.Object
{
static TaskPool<AsyncInstantiateOperationConfiguredSource<T>> pool;
AsyncInstantiateOperationConfiguredSource<T> nextNode;
public ref AsyncInstantiateOperationConfiguredSource<T> NextNode => ref nextNode;
static AsyncInstantiateOperationConfiguredSource()
{
TaskPool.RegisterSizeGetter(typeof(AsyncInstantiateOperationConfiguredSource<T>), () => pool.Size);
}
AsyncInstantiateOperation<T> asyncOperation;
IProgress<float> progress;
CancellationToken cancellationToken;
CancellationTokenRegistration cancellationTokenRegistration;
bool cancelImmediately;
bool completed;
UniTaskCompletionSourceCore<T[]> core;
Action<AsyncOperation> continuationAction;
AsyncInstantiateOperationConfiguredSource()
{
continuationAction = Continuation;
}
public static IUniTaskSource<T[]> Create(AsyncInstantiateOperation<T> asyncOperation, PlayerLoopTiming timing, IProgress<float> progress, CancellationToken cancellationToken, bool cancelImmediately, out short token)
{
if (cancellationToken.IsCancellationRequested)
{
return AutoResetUniTaskCompletionSource<T[]>.CreateFromCanceled(cancellationToken, out token);
}
if (!pool.TryPop(out var result))
{
result = new AsyncInstantiateOperationConfiguredSource<T>();
}
result.asyncOperation = asyncOperation;
result.progress = progress;
result.cancellationToken = cancellationToken;
result.cancelImmediately = cancelImmediately;
result.completed = false;
asyncOperation.completed += result.continuationAction;
if (cancelImmediately && cancellationToken.CanBeCanceled)
{
result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state =>
{
var source = (AsyncInstantiateOperationConfiguredSource<T>)state;
source.core.TrySetCanceled(source.cancellationToken);
}, result);
}
TaskTracker.TrackActiveTask(result, 3);
PlayerLoopHelper.AddAction(timing, result);
token = result.core.Version;
return result;
}
public T[] GetResult(short token)
{
try
{
return core.GetResult(token);
}
finally
{
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
{
TryReturn();
}
else
{
TaskTracker.RemoveTracking(this);
}
}
}
void IUniTaskSource.GetResult(short token)
{
GetResult(token);
}
public UniTaskStatus GetStatus(short token)
{
return core.GetStatus(token);
}
public UniTaskStatus UnsafeGetStatus()
{
return core.UnsafeGetStatus();
}
public void OnCompleted(Action<object> continuation, object state, short token)
{
core.OnCompleted(continuation, state, token);
}
public bool MoveNext()
{
// Already completed
if (completed || asyncOperation == null)
{
return false;
}
if (cancellationToken.IsCancellationRequested)
{
core.TrySetCanceled(cancellationToken);
return false;
}
if (progress != null)
{
progress.Report(asyncOperation.progress);
}
if (asyncOperation.isDone)
{
core.TrySetResult(asyncOperation.Result);
return false;
}
return true;
}
bool TryReturn()
{
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;
if (cancellationToken.IsCancellationRequested)
{
core.TrySetCanceled(cancellationToken);
}
else
{
core.TrySetResult(asyncOperation.Result);
}
}
}
}
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8321f4244edfdcd4798b4fcc92a736c9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -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);
}
}

View File

@@ -2,7 +2,7 @@
"name": "com.cysharp.unitask",
"displayName": "UniTask",
"author": { "name": "Cysharp, Inc.", "url": "https://cysharp.co.jp/en/" },
"version": "2.5.8",
"version": "2.5.9",
"unity": "2018.4",
"description": "Provides an efficient async/await integration to Unity.",
"keywords": [ "async/await", "async", "Task", "UniTask" ],

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: b055f3837a55b4a44abf9bf4bcb3594f
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1 @@
MyTEST

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 49ccc900467d35543bb602a3de4d786a
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -18,7 +18,7 @@ using UnityEngine.SceneManagement;
using UnityEngine.Rendering;
using System.IO;
using System.Linq.Expressions;
using UnityEngine.Events;
// using DG.Tweening;
@@ -172,12 +172,13 @@ public class SandboxMain : MonoBehaviour
// var foo = InstantiateAsync<SandboxMain>(this).ToUniTask();
// var tako = await foo;
//UnityAction action;
return 10;
@@ -590,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)