improve DOTween Extensions

master
neuecc 2020-06-05 12:32:16 +09:00
parent 0725bd1b30
commit b4468b4eba
3 changed files with 158 additions and 48 deletions

46
.gitignore vendored
View File

@ -159,3 +159,49 @@ src/UniTask/UniTask.Tests.Editor.csproj
src/UniTask/UniTask.*.unitypackage src/UniTask/UniTask.*.unitypackage
src/UniTask/UniTask.Linq.csproj src/UniTask/UniTask.Linq.csproj
src/UniTask/DOTween.Modules.csproj
src/UniTask/Unity.Addressables.csproj
src/UniTask/Unity.Addressables.Editor.csproj
src/UniTask/Unity.Analytics.DataPrivacy.csproj
src/UniTask/Unity.Recorder.csproj
src/UniTask/Unity.Recorder.Editor.csproj
src/UniTask/Unity.ResourceManager.csproj
src/UniTask/Unity.Rider.Editor.csproj
src/UniTask/Unity.ScriptableBuildPipeline.csproj
src/UniTask/Unity.ScriptableBuildPipeline.Editor.csproj
src/UniTask/Unity.TextMeshPro.csproj
src/UniTask/Unity.TextMeshPro.Editor.csproj
src/UniTask/Unity.Timeline.csproj
src/UniTask/Unity.Timeline.Editor.csproj
src/UniTask/Unity.VisualStudio.Editor.csproj
src/UniTask/Unity.VSCode.Editor.csproj
src/UniTask/UnityEditor.CacheServer.csproj
src/UniTask/UnityEditor.TestRunner.csproj
src/UniTask/UnityEditor.UI.csproj
src/UniTask/UnityEngine.Advertisements.csproj
src/UniTask/UnityEngine.Monetization.csproj
src/UniTask/UnityEngine.TestRunner.csproj
src/UniTask/UnityEngine.UI.csproj

View File

@ -93,28 +93,30 @@ namespace Cysharp.Threading.Tasks
TaskPool.RegisterSizeGetter(typeof(TweenConfiguredSource), () => pool.Size); TaskPool.RegisterSizeGetter(typeof(TweenConfiguredSource), () => pool.Size);
} }
static readonly Action<object> CancellationCallbackDelegate = CancellationCallback;
static readonly TweenCallback EmptyTweenCallback = () => { }; static readonly TweenCallback EmptyTweenCallback = () => { };
readonly TweenCallback onKillDelegate; readonly TweenCallback onKillDelegate;
readonly TweenCallback onUpdateDelegate;
Tween tween; Tween tween;
TweenCancelBehaviour cancelBehaviour; TweenCancelBehaviour cancelBehaviour;
CancellationToken cancellationToken; CancellationToken cancellationToken;
bool canceled; bool canceled;
CancellationTokenRegistration cancellationTokenRegistration; TweenCallback originalUpdateAction;
UniTaskCompletionSourceCore<AsyncUnit> core; UniTaskCompletionSourceCore<AsyncUnit> core;
TweenConfiguredSource() TweenConfiguredSource()
{ {
onKillDelegate = OnKill; onKillDelegate = OnKill;
onUpdateDelegate = OnUpdate;
} }
public static IUniTaskSource Create(Tween tween, TweenCancelBehaviour cancelBehaviour, CancellationToken cancellationToken, out short token) public static IUniTaskSource Create(Tween tween, TweenCancelBehaviour cancelBehaviour, CancellationToken cancellationToken, out short token)
{ {
if (cancellationToken.IsCancellationRequested) if (cancellationToken.IsCancellationRequested)
{ {
DoCancelBeforeCreate(tween, cancelBehaviour);
return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
} }
@ -127,27 +129,25 @@ namespace Cysharp.Threading.Tasks
result.cancelBehaviour = cancelBehaviour; result.cancelBehaviour = cancelBehaviour;
result.cancellationToken = cancellationToken; result.cancellationToken = cancellationToken;
TaskTracker.TrackActiveTask(result, 3); result.originalUpdateAction = tween.onUpdate;
result.canceled = false;
result.RegisterEvent(); if (result.originalUpdateAction == result.onUpdateDelegate)
{
result.originalUpdateAction = null;
}
tween.onUpdate = result.onUpdateDelegate;
tween.onKill = result.onKillDelegate;
TaskTracker.TrackActiveTask(result, 3);
token = result.core.Version; token = result.core.Version;
return result; return result;
} }
void RegisterEvent()
{
if (cancellationToken.CanBeCanceled)
{
cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(CancellationCallbackDelegate, this);
}
tween.OnKill(onKillDelegate);
}
void OnKill() void OnKill()
{ {
cancellationTokenRegistration.Dispose();
if (canceled) if (canceled)
{ {
core.TrySetCanceled(cancellationToken); core.TrySetCanceled(cancellationToken);
@ -158,44 +158,84 @@ namespace Cysharp.Threading.Tasks
} }
} }
static void CancellationCallback(object state) void OnUpdate()
{ {
var self = (TweenConfiguredSource)state; originalUpdateAction?.Invoke();
switch (self.cancelBehaviour) if (!cancellationToken.IsCancellationRequested)
{
return;
}
switch (this.cancelBehaviour)
{ {
case TweenCancelBehaviour.Kill: case TweenCancelBehaviour.Kill:
default: default:
self.tween.Kill(false); this.tween.Kill(false);
break; break;
case TweenCancelBehaviour.KillAndCancelAwait: case TweenCancelBehaviour.KillAndCancelAwait:
self.canceled = true; this.canceled = true;
self.tween.Kill(false); this.tween.Kill(false);
break; break;
case TweenCancelBehaviour.KillWithCompleteCallback: case TweenCancelBehaviour.KillWithCompleteCallback:
self.tween.Kill(true); this.tween.Kill(true);
break; break;
case TweenCancelBehaviour.KillWithCompleteCallbackAndCancelAwait: case TweenCancelBehaviour.KillWithCompleteCallbackAndCancelAwait:
self.canceled = true; this.canceled = true;
self.tween.Kill(true); this.tween.Kill(true);
break; break;
case TweenCancelBehaviour.Complete: case TweenCancelBehaviour.Complete:
self.tween.Complete(false); this.tween.Complete(false);
break; break;
case TweenCancelBehaviour.CompleteAndCancelAwait: case TweenCancelBehaviour.CompleteAndCancelAwait:
self.canceled = true; this.canceled = true;
self.tween.Complete(false); this.tween.Complete(false);
break; break;
case TweenCancelBehaviour.CompleteWithSeqeunceCallback: case TweenCancelBehaviour.CompleteWithSeqeunceCallback:
self.tween.Complete(true); this.tween.Complete(true);
break; break;
case TweenCancelBehaviour.CompleteWithSeqeunceCallbackAndCancelAwait: case TweenCancelBehaviour.CompleteWithSeqeunceCallbackAndCancelAwait:
self.canceled = true; this.canceled = true;
self.tween.Complete(true); this.tween.Complete(true);
break;
case TweenCancelBehaviour.CancelAwait:
this.tween.onKill = EmptyTweenCallback; // replace to empty(avoid callback after Canceled(instance is returned to pool.)
this.core.TrySetCanceled(this.cancellationToken);
break;
}
}
static void DoCancelBeforeCreate(Tween tween, TweenCancelBehaviour tweenCancelBehaviour)
{
switch (tweenCancelBehaviour)
{
case TweenCancelBehaviour.Kill:
default:
tween.Kill(false);
break;
case TweenCancelBehaviour.KillAndCancelAwait:
tween.Kill(false);
break;
case TweenCancelBehaviour.KillWithCompleteCallback:
tween.Kill(true);
break;
case TweenCancelBehaviour.KillWithCompleteCallbackAndCancelAwait:
tween.Kill(true);
break;
case TweenCancelBehaviour.Complete:
tween.Complete(false);
break;
case TweenCancelBehaviour.CompleteAndCancelAwait:
tween.Complete(false);
break;
case TweenCancelBehaviour.CompleteWithSeqeunceCallback:
tween.Complete(true);
break;
case TweenCancelBehaviour.CompleteWithSeqeunceCallbackAndCancelAwait:
tween.Complete(true);
break; break;
case TweenCancelBehaviour.CancelAwait: case TweenCancelBehaviour.CancelAwait:
self.tween.onKill = EmptyTweenCallback; // replace to empty(avoid callback after Caceled(instance is returned to pool.)
self.core.TrySetCanceled(self.cancellationToken);
break; break;
} }
} }
@ -212,7 +252,6 @@ namespace Cysharp.Threading.Tasks
} }
} }
public UniTaskStatus GetStatus(short token) public UniTaskStatus GetStatus(short token)
{ {
return core.GetStatus(token); return core.GetStatus(token);
@ -232,8 +271,11 @@ namespace Cysharp.Threading.Tasks
{ {
TaskTracker.RemoveTracking(this); TaskTracker.RemoveTracking(this);
core.Reset(); core.Reset();
tween.onUpdate = originalUpdateAction;
tween.onKill = null;
tween = default; tween = default;
cancellationToken = default; cancellationToken = default;
originalUpdateAction = default;
return pool.TryPush(this); return pool.TryPush(this);
} }

View File

@ -15,6 +15,7 @@ using UnityEngine.LowLevel;
using UnityEngine.Networking; using UnityEngine.Networking;
using UnityEngine.UI; using UnityEngine.UI;
// using DG.Tweening; // using DG.Tweening;
public struct MyJob : IJob public struct MyJob : IJob
@ -302,9 +303,17 @@ public class SandboxMain : MonoBehaviour
} }
async UniTaskVoid Start() void Start()
{ {
await UniTask.SwitchToMainThread(); //_ = UniTask.Run(async () =>
//{
// var watch = System.Diagnostics.Stopwatch.StartNew();
// await UniTask.Delay(new TimeSpan(0, 0, seconds: 10));
// Debug.Log(watch.Elapsed);
//});
//return;
//await UniTask.SwitchToMainThread();
//UniTaskAsyncEnumerable.EveryValueChanged(mcc, x => x.MyProperty) //UniTaskAsyncEnumerable.EveryValueChanged(mcc, x => x.MyProperty)
// .Do(_ => { }, () => Debug.Log("COMPLETED")) // .Do(_ => { }, () => Debug.Log("COMPLETED"))
@ -315,10 +324,29 @@ public class SandboxMain : MonoBehaviour
// .Forget(); // .Forget();
//_ = Test1(); //_ = Test1();
Test2().Forget(); //Test2().Forget();
//StartCoroutine(Test3("https://bing.com/")); //StartCoroutine(Test3("https://bing.com/"));
//bool flip = false;
//var rect = cancelButton.GetComponent<RectTransform>();
//var cts = new CancellationTokenSource();
//var ct = cts.Token;
//okButton.onClick.AddListener(UniTask.UnityAction(async () =>
//{
// await rect.DOMoveX(10f * (flip ? -1 : 1), 3).OnUpdate(() => { Debug.Log("UPDATE YEAH"); }).WithCancellation(ct);
// flip = !flip;
// // ok.
//}));
//cancelButton.onClick.AddListener(() =>
//{
// cts.Cancel();
//});
// DG.Tweening.Core.TweenerCore<int> // DG.Tweening.Core.TweenerCore<int>
//Debug.Log("GO MOVEX"); //Debug.Log("GO MOVEX");
//await okButton.GetComponent<RectTransform>().DOMoveX(-10.2f, 3).WithCancellation(CancellationToken.None); //await okButton.GetComponent<RectTransform>().DOMoveX(-10.2f, 3).WithCancellation(CancellationToken.None);
@ -329,7 +357,7 @@ public class SandboxMain : MonoBehaviour
//await okButton.GetComponent<RectTransform>().DOMoveY(10.2f, 3).WithCancellation(CancellationToken.None); //await okButton.GetComponent<RectTransform>().DOMoveY(10.2f, 3).WithCancellation(CancellationToken.None);
//Debug.Log("AGAIN END MOVE"); //Debug.Log("AGAIN END MOVE");
Debug.Log(Test().GetType().FullName); //Debug.Log(Test().GetType().FullName);
@ -383,16 +411,10 @@ public class SandboxMain : MonoBehaviour
//{ //{
okButton.onClick.AddListener(UniTask.UnityAction(async () => //foreach (var (type, size) in TaskPool.GetCacheSizeInfo())
{ //{
await UniTask.Yield(); // Debug.Log(type + ":" + size);
Debug.Log("Yeha"); //}
}));
foreach (var (type, size) in TaskPool.GetCacheSizeInfo())
{
Debug.Log(type + ":" + size);
}
//}).Forget(); //}).Forget();
@ -410,7 +432,7 @@ public class SandboxMain : MonoBehaviour
//await UniTask.Delay(TimeSpan.FromSeconds(1)); //await UniTask.Delay(TimeSpan.FromSeconds(1));
_ = ReturnToMainThreadTest(); // _ = ReturnToMainThreadTest();
//GameObject.Destroy(this.gameObject); //GameObject.Destroy(this.gameObject);