1
0
Fork 0
LaboratoryProtection/Assets/UnityTest/UI/Scripts/Await/UIMoudleIndexGetter.cs

58 lines
1.5 KiB
C#

using Cysharp.Threading.Tasks;
using MessagePipe;
using PMaker.MessagePipe;
using System.Threading;
using UniRx;
using UnityEngine;
using UnityEngine.UI;
public class UIMoudleIndexGetter : MonoBehaviour
{
[SerializeField]
private Button[] _btns;
private void Reset()
{
this._btns = this.GetComponentsInChildren<Button>(true);
}
void Awake()
{
MessageKit.GetAsyncSubscriber<string, int>()
.Subscribe(this.name, async (_, token) => {
await _btns[_].OnClickAsync(token);
})
.AddTo(this);
MessageKit.GetFuncAsyncSubscriber<string, int>()
.Subscribe(this.name + "Any", async (_, token) => {
var (IsCanceled, Result) = await UniTask.WhenAny(this._btns.Select(btn => btn.OnClickAsync(token))).SuppressCancellationThrow();
if (IsCanceled != true)
{
_.value = Result;
}
else
{
_.value = -1;
}
})
.AddTo(this);
}
public async UniTask<int> WaitIndexAsync(CancellationToken token)
{
var (IsCanceled, Result) = await UniTask.WhenAny(this._btns.Select(btn => btn.OnClickAsync(token))).SuppressCancellationThrow();
if (IsCanceled != true)
{
return Result;
}
return -1;
}
}