mirror of
https://github.com/Cysharp/UniTask.git
synced 2026-05-20 14:37:17 +00:00
complete implementation(triggers and etc...)
This commit is contained in:
@@ -1,32 +1,54 @@
|
||||
#if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6))
|
||||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
||||
|
||||
using System.Threading;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UniRx.Async.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 class AsyncAwakeTrigger : MonoBehaviour
|
||||
{
|
||||
bool called = false;
|
||||
UniTaskCompletionSource promise;
|
||||
TriggerEvent<AsyncUnit> triggerEvent;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
called = true;
|
||||
promise?.TrySetResult();
|
||||
triggerEvent?.TrySetResult(AsyncUnit.Default);
|
||||
triggerEvent = null;
|
||||
}
|
||||
|
||||
public UniTask AwakeAsync()
|
||||
{
|
||||
if (called) return UniTask.CompletedTask;
|
||||
|
||||
PlayerLoopHelper.AddAction(PlayerLoopTiming.Update, new AwakeMonitor(this));
|
||||
return new UniTask(promise ?? (promise = new UniTaskCompletionSource()));
|
||||
|
||||
if (triggerEvent == null)
|
||||
{
|
||||
triggerEvent = new TriggerEvent<AsyncUnit>();
|
||||
}
|
||||
|
||||
return ((IAsyncOneShotTrigger)new AsyncTriggerHandler<AsyncUnit>(triggerEvent, true)).OneShotAsync();
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
promise?.TrySetCanceled();
|
||||
triggerEvent?.TrySetCanceled(CancellationToken.None);
|
||||
}
|
||||
|
||||
class AwakeMonitor : IPlayerLoopItem
|
||||
|
||||
Reference in New Issue
Block a user