forked from zxl/LaboratoryProtection
53 lines
1.2 KiB
C#
53 lines
1.2 KiB
C#
using Cysharp.Threading.Tasks;
|
|
|
|
using PMaker.Await;
|
|
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
using UniRx;
|
|
using UniRx.Triggers;
|
|
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public partial class UIButton2AwaitLogic : BaseBehaviour
|
|
{
|
|
[SerializeField]
|
|
private Button[] _btns;
|
|
[SerializeField]
|
|
private AwaitBehaviour[] _behaviours;
|
|
|
|
private CancellationTokenSource _tempSource;
|
|
|
|
private async void OnEnable()
|
|
{
|
|
_tempSource = new CancellationTokenSource();
|
|
var token = _tempSource.Token;
|
|
while (token.IsCancellationRequested != true)
|
|
{
|
|
var (IsCanceled, Result) = await UniTask.WhenAny(this._btns.Select(btn => btn.OnClickAsync(token))).SuppressCancellationThrow();
|
|
if (IsCanceled != true)
|
|
{
|
|
await _behaviours[Result].WaitAsync(token);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
_tempSource?.Cancel();
|
|
}
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
public partial class UIButton2AwaitLogic
|
|
{
|
|
private void Reset()
|
|
{
|
|
this._btns = this.transform.Find("root_btn").GetComponentsInChildren<Button>(true);
|
|
this._behaviours = this.transform.Find("root_logic").GetComponentsInChildren<AwaitBehaviour>(true);
|
|
}
|
|
}
|
|
#endif
|