2019-05-19 23:14:47 +08:00
|
|
|
|
#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
|
|
|
|
|
|
2020-05-05 03:08:53 +08:00
|
|
|
|
using System.Threading;
|
2019-05-19 23:14:47 +08:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace UniRx.Async.Triggers
|
|
|
|
|
{
|
2020-05-05 03:08:53 +08:00
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-19 23:14:47 +08:00
|
|
|
|
[DisallowMultipleComponent]
|
|
|
|
|
public class AsyncStartTrigger : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
bool awakeCalled = false;
|
|
|
|
|
bool called = false;
|
2020-05-05 03:08:53 +08:00
|
|
|
|
TriggerEvent<AsyncUnit> triggerEvent;
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
|
|
|
|
void Awake()
|
|
|
|
|
{
|
|
|
|
|
awakeCalled = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Start()
|
|
|
|
|
{
|
|
|
|
|
called = true;
|
2020-05-05 03:08:53 +08:00
|
|
|
|
triggerEvent?.TrySetResult(AsyncUnit.Default);
|
|
|
|
|
triggerEvent = null;
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UniTask StartAsync()
|
|
|
|
|
{
|
|
|
|
|
if (called) return UniTask.CompletedTask;
|
2020-05-05 03:08:53 +08:00
|
|
|
|
|
2019-05-19 23:14:47 +08:00
|
|
|
|
if (!awakeCalled)
|
|
|
|
|
{
|
|
|
|
|
PlayerLoopHelper.AddAction(PlayerLoopTiming.Update, new AwakeMonitor(this));
|
|
|
|
|
}
|
2020-05-05 03:08:53 +08:00
|
|
|
|
|
|
|
|
|
if (triggerEvent == null)
|
|
|
|
|
{
|
|
|
|
|
triggerEvent = new TriggerEvent<AsyncUnit>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ((IAsyncOneShotTrigger)new AsyncTriggerHandler<AsyncUnit>(triggerEvent, true)).OneShotAsync();
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnDestroy()
|
|
|
|
|
{
|
2020-05-05 03:08:53 +08:00
|
|
|
|
triggerEvent?.TrySetCanceled(CancellationToken.None);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class AwakeMonitor : IPlayerLoopItem
|
|
|
|
|
{
|
|
|
|
|
readonly AsyncStartTrigger trigger;
|
|
|
|
|
|
|
|
|
|
public AwakeMonitor(AsyncStartTrigger trigger)
|
|
|
|
|
{
|
|
|
|
|
this.trigger = trigger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool MoveNext()
|
|
|
|
|
{
|
2020-05-05 03:08:53 +08:00
|
|
|
|
if (trigger.called) return false;
|
2019-05-19 23:14:47 +08:00
|
|
|
|
if (trigger == null)
|
|
|
|
|
{
|
|
|
|
|
trigger.OnDestroy();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-05 03:08:53 +08:00
|
|
|
|
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|