47 lines
1.4 KiB
C#
47 lines
1.4 KiB
C#
using Cysharp.Threading.Tasks;
|
|
|
|
using System;
|
|
using System.Threading;
|
|
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace PMaker.Await.UI
|
|
{
|
|
public class UISelect : AwaitBehaviour
|
|
{
|
|
public Func<CancellationToken, UniTask>[] onSelect;
|
|
|
|
[SerializeField]
|
|
private Button[] _btns;
|
|
|
|
private void Reset()
|
|
{
|
|
this._btns = GetComponentsInChildren<Button>(true);
|
|
}
|
|
|
|
public async override UniTask WaitAsync(CancellationToken cancellationToken)
|
|
{
|
|
this.gameObject.SetActive(true);
|
|
var (isCanceled, result) = await UniTask.WhenAny(_btns.Select(_ => _.OnClickAsync(cancellationToken))).SuppressCancellationThrow();
|
|
if (isCanceled != true)
|
|
{
|
|
await onSelect[result].Invoke(cancellationToken);
|
|
}
|
|
onSelect = null;
|
|
this.gameObject.SetActive(false);
|
|
}
|
|
|
|
public async UniTask WaitAsync(CancellationToken cancellationToken, params Func<CancellationToken, UniTask>[] onSelect)
|
|
{
|
|
this.gameObject.SetActive(true);
|
|
var (isCanceled, result) = await UniTask.WhenAny(_btns.Select(_ => _.OnClickAsync(cancellationToken))).SuppressCancellationThrow();
|
|
if (isCanceled != true)
|
|
{
|
|
await onSelect[result].Invoke(cancellationToken);
|
|
}
|
|
this.gameObject.SetActive(false);
|
|
}
|
|
}
|
|
}
|