1
0
Fork 0
LaboratoryProtection/Assets/UnityTest/UI/Scripts/Message/UINextTip.cs

81 lines
2.4 KiB
C#
Raw Normal View History

2023-09-12 15:55:51 +08:00
using Cysharp.Threading.Tasks;
using DG.Tweening;
using MessagePipe;
using PMaker.DependencyInjection;
using PMaker.MessagePipe;
using System.Threading;
using TMPro;
using UniRx;
using UniRx.Triggers;
using UnityEngine;
using UnityEngine.UI;
public class UINextTip : NameMessageBehaviour<string>
{
public float duration = 1.5f;
[SerializeField]
private Transform _root;
[SerializeField]
private TextMeshProUGUI[] _text;
[SerializeField]
private Button _nextBtn;
private void Reset()
{
this._root = this.transform.Find("Root");
this._text = this.GetComponentsInChildren<TextMeshProUGUI>(true);
this._nextBtn = this.GetComponentInChildren<Button>(true);
}
private void Start()
{
var key = true;
#if UNITY_EDITOR
if (IoC.GetSingleton<TestMark>() == true)
{
var tokenSource = new CancellationTokenSource();
this.UpdateAsObservable()
.Where(_ => Input.GetKeyDown(KeyCode.F12))
.Subscribe(async _ => {
tokenSource?.Cancel();
await UniTask.WaitUntil(() => key == true);
tokenSource = new CancellationTokenSource();
tokenSource.AddTo(this);
await MessageKit.PublishAsync(this.name, "这是一条测试提示!", tokenSource.Token);
})
.AddTo(this);
}
#endif
this.asyncHandler =
async (string message, CancellationToken cancellationToken) => {
key = false;
_text[0].text = message;
var text = _text[1];
text.text = string.Empty;
this._root.gameObject.SetActive(true);
var result = await DOTween
.To(() => text.text, value => text.text = value, message, duration)
.SetEase(Ease.Linear)
.AwaitForComplete(TweenCancelBehaviour.KillWithCompleteCallbackAndCancelAwait, cancellationToken: cancellationToken)
.SuppressCancellationThrow();
Debug.Log(this.name + "IsCanceled: " + result);
// 箭头动画 TODO
if (result != true)
{
await _nextBtn.OnClickAsync(cancellationToken).SuppressCancellationThrow();
}
this._root.gameObject.SetActive(false);
key = true;
};
}
}