上传task

This commit is contained in:
2025-06-15 22:06:57 +08:00
parent bc58a9d0d5
commit bcfa5ce1ec
358 changed files with 73507 additions and 7 deletions

View File

@@ -0,0 +1,32 @@
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
using System.Threading;
using UnityEngine;
namespace Cysharp.Threading.Tasks.Triggers
{
public static partial class AsyncTriggerExtensions
{
public static AsyncAwakeTrigger GetAsyncAwakeTrigger(this GameObject gameObject)
{
return GetOrAddComponent<AsyncAwakeTrigger>(gameObject);
}
public static AsyncAwakeTrigger GetAsyncAwakeTrigger(this Component component)
{
return component.gameObject.GetAsyncAwakeTrigger();
}
}
[DisallowMultipleComponent]
public sealed class AsyncAwakeTrigger : AsyncTriggerBase<AsyncUnit>
{
public UniTask AwakeAsync()
{
if (calledAwake) return UniTask.CompletedTask;
return ((IAsyncOneShotTrigger)new AsyncTriggerHandler<AsyncUnit>(this, true)).OneShotAsync();
}
}
}

View File

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

View File

@@ -0,0 +1,95 @@
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
using System.Threading;
using UnityEngine;
namespace Cysharp.Threading.Tasks.Triggers
{
public static partial class AsyncTriggerExtensions
{
public static AsyncDestroyTrigger GetAsyncDestroyTrigger(this GameObject gameObject)
{
return GetOrAddComponent<AsyncDestroyTrigger>(gameObject);
}
public static AsyncDestroyTrigger GetAsyncDestroyTrigger(this Component component)
{
return component.gameObject.GetAsyncDestroyTrigger();
}
}
[DisallowMultipleComponent]
public sealed class AsyncDestroyTrigger : MonoBehaviour
{
bool awakeCalled = false;
bool called = false;
CancellationTokenSource cancellationTokenSource;
public CancellationToken CancellationToken
{
get
{
if (cancellationTokenSource == null)
{
cancellationTokenSource = new CancellationTokenSource();
if (!awakeCalled)
{
PlayerLoopHelper.AddAction(PlayerLoopTiming.Update, new AwakeMonitor(this));
}
}
return cancellationTokenSource.Token;
}
}
void Awake()
{
awakeCalled = true;
}
void OnDestroy()
{
called = true;
cancellationTokenSource?.Cancel();
cancellationTokenSource?.Dispose();
}
public UniTask OnDestroyAsync()
{
if (called) return UniTask.CompletedTask;
var tcs = new UniTaskCompletionSource();
// OnDestroy = Called Cancel.
CancellationToken.RegisterWithoutCaptureExecutionContext(state =>
{
var tcs2 = (UniTaskCompletionSource)state;
tcs2.TrySetResult();
}, tcs);
return tcs.Task;
}
class AwakeMonitor : IPlayerLoopItem
{
readonly AsyncDestroyTrigger trigger;
public AwakeMonitor(AsyncDestroyTrigger trigger)
{
this.trigger = trigger;
}
public bool MoveNext()
{
if (trigger.called || trigger.awakeCalled) return false;
if (trigger == null)
{
trigger.OnDestroy();
return false;
}
return true;
}
}
}
}

View File

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

View File

@@ -0,0 +1,38 @@
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
using UnityEngine;
namespace Cysharp.Threading.Tasks.Triggers
{
public static partial class AsyncTriggerExtensions
{
public static AsyncStartTrigger GetAsyncStartTrigger(this GameObject gameObject)
{
return GetOrAddComponent<AsyncStartTrigger>(gameObject);
}
public static AsyncStartTrigger GetAsyncStartTrigger(this Component component)
{
return component.gameObject.GetAsyncStartTrigger();
}
}
[DisallowMultipleComponent]
public sealed class AsyncStartTrigger : AsyncTriggerBase<AsyncUnit>
{
bool called;
void Start()
{
called = true;
RaiseEvent(AsyncUnit.Default);
}
public UniTask StartAsync()
{
if (called) return UniTask.CompletedTask;
return ((IAsyncOneShotTrigger)new AsyncTriggerHandler<AsyncUnit>(this, true)).OneShotAsync();
}
}
}

View File

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

View File

@@ -0,0 +1,310 @@
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
using System;
using System.Threading;
using UnityEngine;
namespace Cysharp.Threading.Tasks.Triggers
{
public abstract class AsyncTriggerBase<T> : MonoBehaviour, IUniTaskAsyncEnumerable<T>
{
TriggerEvent<T> triggerEvent;
internal protected bool calledAwake;
internal protected bool calledDestroy;
void Awake()
{
calledAwake = true;
}
void OnDestroy()
{
if (calledDestroy) return;
calledDestroy = true;
triggerEvent.SetCompleted();
}
internal void AddHandler(ITriggerHandler<T> handler)
{
if (!calledAwake)
{
PlayerLoopHelper.AddAction(PlayerLoopTiming.Update, new AwakeMonitor(this));
}
triggerEvent.Add(handler);
}
internal void RemoveHandler(ITriggerHandler<T> handler)
{
if (!calledAwake)
{
PlayerLoopHelper.AddAction(PlayerLoopTiming.Update, new AwakeMonitor(this));
}
triggerEvent.Remove(handler);
}
protected void RaiseEvent(T value)
{
triggerEvent.SetResult(value);
}
public IUniTaskAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)
{
return new AsyncTriggerEnumerator(this, cancellationToken);
}
sealed class AsyncTriggerEnumerator : MoveNextSource, IUniTaskAsyncEnumerator<T>, ITriggerHandler<T>
{
static Action<object> cancellationCallback = CancellationCallback;
readonly AsyncTriggerBase<T> parent;
CancellationToken cancellationToken;
CancellationTokenRegistration registration;
bool called;
bool isDisposed;
public AsyncTriggerEnumerator(AsyncTriggerBase<T> parent, CancellationToken cancellationToken)
{
this.parent = parent;
this.cancellationToken = cancellationToken;
}
public void OnCanceled(CancellationToken cancellationToken = default)
{
completionSource.TrySetCanceled(cancellationToken);
}
public void OnNext(T value)
{
Current = value;
completionSource.TrySetResult(true);
}
public void OnCompleted()
{
completionSource.TrySetResult(false);
}
public void OnError(Exception ex)
{
completionSource.TrySetException(ex);
}
static void CancellationCallback(object state)
{
var self = (AsyncTriggerEnumerator)state;
self.DisposeAsync().Forget(); // sync
self.completionSource.TrySetCanceled(self.cancellationToken);
}
public T Current { get; private set; }
ITriggerHandler<T> ITriggerHandler<T>.Prev { get; set; }
ITriggerHandler<T> ITriggerHandler<T>.Next { get; set; }
public UniTask<bool> MoveNextAsync()
{
cancellationToken.ThrowIfCancellationRequested();
completionSource.Reset();
if (!called)
{
called = true;
TaskTracker.TrackActiveTask(this, 3);
parent.AddHandler(this);
if (cancellationToken.CanBeCanceled)
{
registration = cancellationToken.RegisterWithoutCaptureExecutionContext(cancellationCallback, this);
}
}
return new UniTask<bool>(this, completionSource.Version);
}
public UniTask DisposeAsync()
{
if (!isDisposed)
{
isDisposed = true;
TaskTracker.RemoveTracking(this);
registration.Dispose();
parent.RemoveHandler(this);
}
return default;
}
}
class AwakeMonitor : IPlayerLoopItem
{
readonly AsyncTriggerBase<T> trigger;
public AwakeMonitor(AsyncTriggerBase<T> trigger)
{
this.trigger = trigger;
}
public bool MoveNext()
{
if (trigger.calledAwake) return false;
if (trigger == null)
{
trigger.OnDestroy();
return false;
}
return true;
}
}
}
public interface IAsyncOneShotTrigger
{
UniTask OneShotAsync();
}
public partial class AsyncTriggerHandler<T> : IAsyncOneShotTrigger
{
UniTask IAsyncOneShotTrigger.OneShotAsync()
{
core.Reset();
return new UniTask((IUniTaskSource)this, core.Version);
}
}
public sealed partial class AsyncTriggerHandler<T> : IUniTaskSource<T>, ITriggerHandler<T>, IDisposable
{
static Action<object> cancellationCallback = CancellationCallback;
readonly AsyncTriggerBase<T> trigger;
CancellationToken cancellationToken;
CancellationTokenRegistration registration;
bool isDisposed;
bool callOnce;
UniTaskCompletionSourceCore<T> core;
internal CancellationToken CancellationToken => cancellationToken;
ITriggerHandler<T> ITriggerHandler<T>.Prev { get; set; }
ITriggerHandler<T> ITriggerHandler<T>.Next { get; set; }
internal AsyncTriggerHandler(AsyncTriggerBase<T> trigger, bool callOnce)
{
if (cancellationToken.IsCancellationRequested)
{
isDisposed = true;
return;
}
this.trigger = trigger;
this.cancellationToken = default;
this.registration = default;
this.callOnce = callOnce;
trigger.AddHandler(this);
TaskTracker.TrackActiveTask(this, 3);
}
internal AsyncTriggerHandler(AsyncTriggerBase<T> trigger, CancellationToken cancellationToken, bool callOnce)
{
if (cancellationToken.IsCancellationRequested)
{
isDisposed = true;
return;
}
this.trigger = trigger;
this.cancellationToken = cancellationToken;
this.callOnce = callOnce;
trigger.AddHandler(this);
if (cancellationToken.CanBeCanceled)
{
registration = cancellationToken.RegisterWithoutCaptureExecutionContext(cancellationCallback, this);
}
TaskTracker.TrackActiveTask(this, 3);
}
static void CancellationCallback(object state)
{
var self = (AsyncTriggerHandler<T>)state;
self.Dispose();
self.core.TrySetCanceled(self.cancellationToken);
}
public void Dispose()
{
if (!isDisposed)
{
isDisposed = true;
TaskTracker.RemoveTracking(this);
registration.Dispose();
trigger.RemoveHandler(this);
}
}
T IUniTaskSource<T>.GetResult(short token)
{
try
{
return core.GetResult(token);
}
finally
{
if (callOnce)
{
Dispose();
}
}
}
void ITriggerHandler<T>.OnNext(T value)
{
core.TrySetResult(value);
}
void ITriggerHandler<T>.OnCanceled(CancellationToken cancellationToken)
{
core.TrySetCanceled(cancellationToken);
}
void ITriggerHandler<T>.OnCompleted()
{
core.TrySetCanceled(CancellationToken.None);
}
void ITriggerHandler<T>.OnError(Exception ex)
{
core.TrySetException(ex);
}
void IUniTaskSource.GetResult(short token)
{
((IUniTaskSource<T>)this).GetResult(token);
}
UniTaskStatus IUniTaskSource.GetStatus(short token)
{
return core.GetStatus(token);
}
UniTaskStatus IUniTaskSource.UnsafeGetStatus()
{
return core.UnsafeGetStatus();
}
void IUniTaskSource.OnCompleted(Action<object> continuation, object state, short token)
{
core.OnCompleted(continuation, state, token);
}
}
}

View File

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

View File

@@ -0,0 +1,102 @@
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
using System.Threading;
using UnityEngine;
using Cysharp.Threading.Tasks.Triggers;
namespace Cysharp.Threading.Tasks
{
public static class UniTaskCancellationExtensions
{
#if UNITY_2022_2_OR_NEWER
/// <summary>This CancellationToken is canceled when the MonoBehaviour will be destroyed.</summary>
public static CancellationToken GetCancellationTokenOnDestroy(this MonoBehaviour monoBehaviour)
{
return monoBehaviour.destroyCancellationToken;
}
#endif
/// <summary>This CancellationToken is canceled when the MonoBehaviour will be destroyed.</summary>
public static CancellationToken GetCancellationTokenOnDestroy(this GameObject gameObject)
{
return gameObject.GetAsyncDestroyTrigger().CancellationToken;
}
/// <summary>This CancellationToken is canceled when the MonoBehaviour will be destroyed.</summary>
public static CancellationToken GetCancellationTokenOnDestroy(this Component component)
{
#if UNITY_2022_2_OR_NEWER
if (component is MonoBehaviour mb)
{
return mb.destroyCancellationToken;
}
#endif
return component.GetAsyncDestroyTrigger().CancellationToken;
}
}
}
namespace Cysharp.Threading.Tasks.Triggers
{
public static partial class AsyncTriggerExtensions
{
// Util.
static T GetOrAddComponent<T>(GameObject gameObject)
where T : Component
{
#if UNITY_2019_2_OR_NEWER
if (!gameObject.TryGetComponent<T>(out var component))
{
component = gameObject.AddComponent<T>();
}
#else
var component = gameObject.GetComponent<T>();
if (component == null)
{
component = gameObject.AddComponent<T>();
}
#endif
return component;
}
// Special for single operation.
/// <summary>This function is called when the MonoBehaviour will be destroyed.</summary>
public static UniTask OnDestroyAsync(this GameObject gameObject)
{
return gameObject.GetAsyncDestroyTrigger().OnDestroyAsync();
}
/// <summary>This function is called when the MonoBehaviour will be destroyed.</summary>
public static UniTask OnDestroyAsync(this Component component)
{
return component.GetAsyncDestroyTrigger().OnDestroyAsync();
}
public static UniTask StartAsync(this GameObject gameObject)
{
return gameObject.GetAsyncStartTrigger().StartAsync();
}
public static UniTask StartAsync(this Component component)
{
return component.GetAsyncStartTrigger().StartAsync();
}
public static UniTask AwakeAsync(this GameObject gameObject)
{
return gameObject.GetAsyncAwakeTrigger().AwakeAsync();
}
public static UniTask AwakeAsync(this Component component)
{
return component.GetAsyncAwakeTrigger().AwakeAsync();
}
}
}

View File

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

View File

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

View File

@@ -0,0 +1,208 @@
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
<#
var empty = new (string, string)[0];
var triggers = new (string triggerName, string methodName, string returnType, string handlerInterface, (string argType, string argName)[] arguments)[]
{
("AnimatorIK", "OnAnimatorIK", "int", null, new []{ ("int", "layerIndex") }),
("AnimatorMove", "OnAnimatorMove", "AsyncUnit", null, empty),
("OnCanvasGroupChanged", "OnCanvasGroupChanged", "AsyncUnit", null, empty ),
("CollisionEnter2D", "OnCollisionEnter2D", "Collision2D", null, new []{ ("Collision2D", "coll") }),
("CollisionExit2D", "OnCollisionExit2D", "Collision2D", null, new []{ ("Collision2D", "coll") }),
("CollisionStay2D", "OnCollisionStay2D", "Collision2D", null, new []{ ("Collision2D", "coll") }),
("CollisionEnter", "OnCollisionEnter", "Collision", null, new []{ ("Collision", "coll") }),
("CollisionExit", "OnCollisionExit", "Collision", null, new []{ ("Collision", "coll") }),
("CollisionStay", "OnCollisionStay", "Collision", null, new []{ ("Collision", "coll") }),
("Enable", "OnEnable", "AsyncUnit", null, empty),
("Disable", "OnDisable", "AsyncUnit", null, empty),
("JointBreak", "OnJointBreak", "float", null, new []{ ("float", "breakForce") }),
("JointBreak2D", "OnJointBreak2D", "Joint2D", null, new []{ ("Joint2D", "brokenJoint") }),
("Update", "Update", "AsyncUnit", null, empty),
("FixedUpdate", "FixedUpdate", "AsyncUnit", null, empty),
("LateUpdate", "LateUpdate", "AsyncUnit", null, empty),
("ParticleCollision", "OnParticleCollision", "GameObject", null, new []{ ("GameObject", "other") }),
("RectTransformDimensionsChange", "OnRectTransformDimensionsChange", "AsyncUnit", null, empty),
("RectTransformRemoved", "OnRectTransformRemoved", "AsyncUnit", null, empty),
("BeforeTransformParentChanged", "OnBeforeTransformParentChanged", "AsyncUnit", null, empty),
("TransformParentChanged", "OnTransformParentChanged", "AsyncUnit", null, empty),
("TransformChildrenChanged", "OnTransformChildrenChanged", "AsyncUnit", null, empty),
("TriggerEnter2D", "OnTriggerEnter2D", "Collider2D", null, new []{ ("Collider2D", "other") }),
("TriggerExit2D", "OnTriggerExit2D", "Collider2D", null, new []{ ("Collider2D", "other") }),
("TriggerStay2D", "OnTriggerStay2D", "Collider2D", null, new []{ ("Collider2D", "other") }),
("TriggerEnter", "OnTriggerEnter", "Collider", null, new []{ ("Collider", "other") }),
("TriggerExit", "OnTriggerExit", "Collider", null, new []{ ("Collider", "other") }),
("TriggerStay", "OnTriggerStay", "Collider", null, new []{ ("Collider", "other") }),
("BecameInvisible", "OnBecameInvisible", "AsyncUnit", null, empty),
("BecameVisible", "OnBecameVisible", "AsyncUnit", null, empty),
// Mouse... #if !(UNITY_IPHONE || UNITY_ANDROID || UNITY_METRO)
("MouseDown", "OnMouseDown", "AsyncUnit", null, empty),
("MouseDrag", "OnMouseDrag", "AsyncUnit", null, empty),
("MouseEnter", "OnMouseEnter", "AsyncUnit", null, empty),
("MouseExit", "OnMouseExit", "AsyncUnit", null, empty),
("MouseOver", "OnMouseOver", "AsyncUnit", null, empty),
("MouseUp", "OnMouseUp", "AsyncUnit", null, empty),
("MouseUpAsButton", "OnMouseUpAsButton", "AsyncUnit", null, empty),
// new in v2
("ApplicationFocus", "OnApplicationFocus", "bool", null, new []{("bool", "hasFocus") }),
("ApplicationPause", "OnApplicationPause", "bool", null, new []{("bool", "pauseStatus") }),
("ApplicationQuit", "OnApplicationQuit", "AsyncUnit", null, empty),
("AudioFilterRead", "OnAudioFilterRead", "(float[] data, int channels)", null, new[]{("float[]", "data"), ("int", "channels")}),
("ControllerColliderHit", "OnControllerColliderHit", "ControllerColliderHit", null, new[]{("ControllerColliderHit", "hit")}),
("DrawGizmos", "OnDrawGizmos", "AsyncUnit", null, empty),
("DrawGizmosSelected", "OnDrawGizmosSelected", "AsyncUnit", null, empty),
("GUI", "OnGUI", "AsyncUnit", null, empty),
("ParticleSystemStopped", "OnParticleSystemStopped", "AsyncUnit", null, empty),
("ParticleTrigger", "OnParticleTrigger", "AsyncUnit", null, empty),
("PostRender", "OnPostRender", "AsyncUnit", null, empty),
("PreCull", "OnPreCull", "AsyncUnit", null, empty),
("PreRender", "OnPreRender", "AsyncUnit", null, empty),
("RenderImage", "OnRenderImage", "(RenderTexture source, RenderTexture destination)", null, new[]{("RenderTexture", "source"), ("RenderTexture", "destination")}),
("RenderObject", "OnRenderObject", "AsyncUnit", null, empty),
("ServerInitialized", "OnServerInitialized", "AsyncUnit", null, empty),
("Validate", "OnValidate", "AsyncUnit", null, empty),
("WillRenderObject", "OnWillRenderObject", "AsyncUnit", null, empty),
("Reset", "Reset", "AsyncUnit", null, empty),
// uGUI
("BeginDrag", "OnBeginDrag", "PointerEventData", "IBeginDragHandler", new []{ ("PointerEventData", "eventData") }),
("Cancel", "OnCancel", "BaseEventData", "ICancelHandler", new []{ ("BaseEventData", "eventData") }),
("Deselect", "OnDeselect", "BaseEventData", "IDeselectHandler", new []{ ("BaseEventData", "eventData") }),
("Drag", "OnDrag", "PointerEventData", "IDragHandler", new []{ ("PointerEventData", "eventData") }),
("Drop", "OnDrop", "PointerEventData", "IDropHandler", new []{ ("PointerEventData", "eventData") }),
("EndDrag", "OnEndDrag", "PointerEventData", "IEndDragHandler", new []{ ("PointerEventData", "eventData") }),
("InitializePotentialDrag", "OnInitializePotentialDrag", "PointerEventData", "IInitializePotentialDragHandler", new []{ ("PointerEventData", "eventData") }),
("Move", "OnMove", "AxisEventData", "IMoveHandler", new []{ ("AxisEventData", "eventData") }),
("PointerClick", "OnPointerClick", "PointerEventData", "IPointerClickHandler", new []{ ("PointerEventData", "eventData") }),
("PointerDown", "OnPointerDown", "PointerEventData", "IPointerDownHandler", new []{ ("PointerEventData", "eventData") }),
("PointerEnter", "OnPointerEnter", "PointerEventData", "IPointerEnterHandler", new []{ ("PointerEventData", "eventData") }),
("PointerExit", "OnPointerExit", "PointerEventData", "IPointerExitHandler", new []{ ("PointerEventData", "eventData") }),
("PointerUp", "OnPointerUp", "PointerEventData", "IPointerUpHandler", new []{ ("PointerEventData", "eventData") }),
("Scroll", "OnScroll", "PointerEventData", "IScrollHandler", new []{ ("PointerEventData", "eventData") }),
("Select", "OnSelect", "BaseEventData", "ISelectHandler", new []{ ("BaseEventData", "eventData") }),
("Submit", "OnSubmit", "BaseEventData", "ISubmitHandler", new []{ ("BaseEventData", "eventData") }),
("UpdateSelected", "OnUpdateSelected", "BaseEventData", "IUpdateSelectedHandler", new []{ ("BaseEventData", "eventData") }),
// 2019.3
("ParticleUpdateJobScheduled", "OnParticleUpdateJobScheduled", "UnityEngine.ParticleSystemJobs.ParticleSystemJobData", null, new[]{("UnityEngine.ParticleSystemJobs.ParticleSystemJobData", "particles")}),
// Oneshot
// Awake, Start, Destroy
};
triggers = triggers.OrderBy(x => x.handlerInterface != null).ThenBy(x => x.handlerInterface != null ? x.handlerInterface : x.methodName).ToArray();
Func<string, string> ToInterfaceName = x => $"IAsync{x}Handler";
Func<string, string> ToUniTaskName = x => x == "AsyncUnit" ? "UniTask" : $"UniTask<{x}>";
Func<string, string> ToCastUniTasSourceType = x => x == "AsyncUnit" ? "IUniTaskSource" : $"IUniTaskSource<{x}>";
Func<string, string> OnInvokeSuffix = x => x == "AsyncUnit" ? ".AsUniTask()" : $"";
Func<(string argType, string argName)[], string> BuildMethodArgument = x => string.Join(", ", x.Select(y => y.argType + " " + y.argName));
Func<(string argType, string argName)[], string> BuildResultParameter = x => x.Length == 0 ? "AsyncUnit.Default" : "(" + string.Join(", ", x.Select(y => y.argName)) + ")";
Func<string, bool> IsParticleSystem = x => x == "ParticleUpdateJobScheduled";
Func<string, bool> IsMouseTrigger = x => x.StartsWith("Mouse");
Func<string, string> RequirePhysicsModule = x => (x.StartsWith("Collision") || x.StartsWith("Collider") || x.StartsWith("ControllerCollider") || x.StartsWith("Joint") || x.StartsWith("Trigger"))
? (x.Contains("2D") ? "UNITASK_PHYSICS2D_SUPPORT" : "UNITASK_PHYSICS_SUPPORT")
: null;
Func<string, bool> IsUguiSystem = x => x != null;
#>
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
using System.Threading;
using UnityEngine;
#if !UNITY_2019_1_OR_NEWER || UNITASK_UGUI_SUPPORT
using UnityEngine.EventSystems;
#endif
namespace Cysharp.Threading.Tasks.Triggers
{
<# foreach(var t in triggers) { #>
#region <#= t.triggerName #>
<# if(IsUguiSystem(t.handlerInterface)) { #>
#if !UNITY_2019_1_OR_NEWER || UNITASK_UGUI_SUPPORT
<# } #>
<# if(IsParticleSystem(t.triggerName)) { #>
#if UNITY_2019_3_OR_NEWER && (!UNITY_2019_1_OR_NEWER || UNITASK_PARTICLESYSTEM_SUPPORT)
<# } #>
<# if(IsMouseTrigger(t.triggerName)) { #>
#if !(UNITY_IPHONE || UNITY_ANDROID || UNITY_METRO)
<# } #>
<# if(RequirePhysicsModule(t.triggerName) != null) { #>
#if !UNITY_2019_1_OR_NEWER || <#= RequirePhysicsModule(t.triggerName) #>
<# } #>
public interface <#= ToInterfaceName(t.methodName) #>
{
<#= ToUniTaskName(t.returnType) #> <#= t.methodName #>Async();
}
public partial class AsyncTriggerHandler<T> : <#= ToInterfaceName(t.methodName) #>
{
<#= ToUniTaskName(t.returnType) #> <#= ToInterfaceName(t.methodName) #>.<#= t.methodName #>Async()
{
core.Reset();
return new <#= ToUniTaskName(t.returnType) #>((<#= ToCastUniTasSourceType(t.returnType) #>)(object)this, core.Version);
}
}
public static partial class AsyncTriggerExtensions
{
public static Async<#= t.triggerName #>Trigger GetAsync<#= t.triggerName #>Trigger(this GameObject gameObject)
{
return GetOrAddComponent<Async<#= t.triggerName #>Trigger>(gameObject);
}
public static Async<#= t.triggerName #>Trigger GetAsync<#= t.triggerName #>Trigger(this Component component)
{
return component.gameObject.GetAsync<#= t.triggerName #>Trigger();
}
}
[DisallowMultipleComponent]
public sealed class Async<#= t.triggerName #>Trigger : AsyncTriggerBase<<#= t.returnType #>><#= (t.handlerInterface == null) ? "" : $", {t.handlerInterface}" #>
{
void <#= (t.handlerInterface == null) ? "" : $"{t.handlerInterface}." #><#= t.methodName #>(<#= BuildMethodArgument(t.arguments) #>)
{
RaiseEvent(<#= BuildResultParameter(t.arguments) #>);
}
public <#= ToInterfaceName(t.methodName) #> Get<#= t.methodName #>AsyncHandler()
{
return new AsyncTriggerHandler<<#= t.returnType #>>(this, false);
}
public <#= ToInterfaceName(t.methodName) #> Get<#= t.methodName #>AsyncHandler(CancellationToken cancellationToken)
{
return new AsyncTriggerHandler<<#= t.returnType #>>(this, cancellationToken, false);
}
public <#= ToUniTaskName(t.returnType) #> <#= t.methodName #>Async()
{
return ((<#= ToInterfaceName(t.methodName) #>)new AsyncTriggerHandler<<#= t.returnType #>>(this, true)).<#= t.methodName #>Async();
}
public <#= ToUniTaskName(t.returnType) #> <#= t.methodName #>Async(CancellationToken cancellationToken)
{
return ((<#= ToInterfaceName(t.methodName) #>)new AsyncTriggerHandler<<#= t.returnType #>>(this, cancellationToken, true)).<#= t.methodName #>Async();
}
}
<# if(IsUguiSystem(t.handlerInterface)) { #>
#endif
<# } #>
<# if(RequirePhysicsModule(t.triggerName) != null) { #>
#endif
<# } #>
<# if(IsParticleSystem(t.triggerName) || IsMouseTrigger(t.triggerName)) { #>
#endif
<# } #>
#endregion
<# } #>
}

View File

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