LaboratoryProtection/Assets/UnityTest/UI/Scripts/Await/UIBook.cs

92 lines
2.8 KiB
C#
Raw Normal View History

2023-09-12 15:55:51 +08:00
using Cysharp.Threading.Tasks;
using Cysharp.Threading.Tasks.Linq;
using Cysharp.Threading.Tasks.Triggers;
using PMaker.Await;
using PMaker.Extension;
using System.Linq;
using System.Threading;
using Script;
using UnityEngine;
using UnityEngine.UI;
public class UIBook : AwaitBehaviour
{
[SerializeField] private GameObject[] _pages;
[SerializeField] private IndexController _indexController;
[SerializeField] private AsyncReactiveProperty<int> _index;
[SerializeField] private Button _finishBtn;
private void Reset()
{
//Debug.Log(this.transform.Find("学习完成Btn"));
//this.transform.GetChild(2).gameObject.GetComponent<Button>();
this.transform.GetComponentsInChildren<Button>().First(_ => _.name == "学习完成Btn");
this._pages = this
.transform
.Find("root")
.Children()
//.Where(_ => _.GetComponent<BaseBehaviour>() == null)
//.Where(_ => _.GetComponent<Selectable>() == null)
.Select(_ => _.gameObject)
.ToArray();
this._indexController = this.GetComponentInChildren<IndexController>(true);
}
private void Start()
{
var token = this.GetCancellationTokenOnDestroy();
_index = new AsyncReactiveProperty<int>(0);
var max = _pages.Length - 1;
_index
.Where(_ => _ >= 0 && _ <= max)
.Subscribe(_ =>
{
AudioManager.Instance.PlayMainSounds($"6-1/{StringConst.Model6_1Audio[_]}");
if (_ == 0)
{
_indexController.SetActive(0, false);
}
else
{
_indexController.SetActive(0, true);
}
if (_ == max)
{
_indexController.SetActive(1, false);
this._finishBtn.gameObject.SetActive(true);
}
else
{
this._finishBtn.gameObject.SetActive(false);
_indexController.SetActive(1, true);
}
foreach (var item in _pages)
{
item.SetActive(false);
}
_pages[_].SetActive(true);
})
.AddTo(token);
_indexController.Bind(this._index, 0, max + 1, token);
}
public override async UniTask WaitAsync(CancellationToken cancellationToken = default)
{
this.gameObject.SetActive(true);
await this.StartAsync();
await this._finishBtn.OnClickAsync(cancellationToken);
AudioManager.Instance.StopAllAudioSource();
//var max = this._pages.Length - 1;
//await this._index.FirstAsync(_ => _ >= max, cancellationToken);
this.gameObject.SetActive(false);
}
}