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

154 lines
4.9 KiB
C#

using Cysharp.Threading.Tasks;
using MessagePipe;
using PMaker.Await;
using PMaker.Await.UI;
using PMaker.MessagePipe;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using TMPro;
using UniRx;
using UnityEngine;
using UnityEngine.UI;
public class UIGlobal : AwaitBehaviour
{
[SerializeField]
private ToggleButton _muteBtn;
[SerializeField]
private Button[] _btns;
[SerializeField]
private TextMeshProUGUI _topText;
private void Reset()
{
var toggleBtns = this.GetComponentsInChildren<ToggleButton>(true);
this._muteBtn = toggleBtns.First(_ => _.name == "声音Btn");
var btns = this.GetComponentsInChildren<Button>(true);
var list = new List<Button>
{
btns.First(_ => _.name == "首页Btn"),
btns.First(_ => _.name == "类型Btn")
};
this._btns = list.ToArray();
_topText = this.GetComponentsInChildren<TextMeshProUGUI>(true).First(_ => _.name == "text_top");
}
private void Awake()
{
MessageKit.GetSubscriber<string, (string name, bool value)>()
.Subscribe(this.name, _ => {
this.transform.Find(_.name).gameObject.SetActive(_.value);
})
.AddTo(this);
}
private void Start()
{
var cancellationToken = this.GetCancellationTokenOnDestroy();
_muteBtn
.onValueChanged
.AsObservable()
.Subscribe(_ => {
if (_ == true)
{
Debug.Log("静音");
}
else
{
Debug.Log("取消静音");
}
MessageKit.Publish("Mute", _);
})
.AddTo(this);
// 首页
_btns[0]
.OnClickAsObservable()
.Subscribe(async _ => {
await this.ShowTip_2MiniAsync("返回将清空当前模块进度",
cancellationToken: cancellationToken,
onSelect: new Func<CancellationToken, UniTask>[]
{
async token => {
MessageKit
.GetPublisher<string>()
.Publish("返回首页");
await UniTask.Yield();
},
async token => {
await UniTask.Yield();
},
}
);
})
.AddTo(this);
// 类型
_btns[1]
.OnClickAsObservable()
.Subscribe(async _ => {
await this.ShowTip_2MiniAsync("返回将清空当前模块进度",
cancellationToken: cancellationToken,
onSelect: new Func<CancellationToken, UniTask>[]
{
async token => {
MessageKit
.GetPublisher<string>()
.Publish("返回类型");
await UniTask.Yield();
},
async token => {
await UniTask.Yield();
},
}
);
})
.AddTo(this);
// 顶部标题
MessageKit.GetSubscriber<string, string>()
.Subscribe("SetTopText", _ => {
this._topText.text = _;
})
.AddTo(this);
}
public override UniTask WaitAsync(CancellationToken cancellationToken = default)
{
throw new System.NotImplementedException();
}
public async UniTask ShowTip_2MiniAsync(string content, string title = "提 示", string btn1Name = "确定", string btn2Name = "取消", CancellationToken cancellationToken = default, params Func<CancellationToken, UniTask>[] onSelect)
{
var select = this.transform.Find("提示_2_mini").GetComponent<UISelect>();
var text = select.GetComponentsInChildren<TextMeshProUGUI>();
text[0].text = title;
text[1].text = content;
text[2].text = btn1Name;
text[3].text = btn2Name;
await select.WaitAsync(cancellationToken, onSelect);
}
public async UniTask ShowTip_2Async(string content, string title = "提 示", string btn1Name = "确定", string btn2Name = "取消", CancellationToken cancellationToken = default, params Func<CancellationToken, UniTask>[] onSelect)
{
var select = this.transform.Find("提示_2").GetComponent<UISelect>();
var text = select.GetComponentsInChildren<TextMeshProUGUI>();
text[0].text = title;
text[1].text = content;
text[2].text = btn1Name;
text[3].text = btn2Name;
await select.WaitAsync(cancellationToken, onSelect);
}
}