1
0
Fork 0
LaboratoryProtection/Assets/UnityTest/UI/Scripts/Other/UIButton2AwaitLogic.cs

53 lines
1.2 KiB
C#
Raw Normal View History

2023-09-12 15:55:51 +08:00
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