LaboratoryProtection/Assets/UnityTest/ZXL/Scripts/ETTaskHelper.cs

102 lines
3.0 KiB
C#
Raw Normal View History

2023-09-14 15:36:17 +08:00
using System;
2023-09-14 23:09:07 +08:00
using Cysharp.Threading.Tasks;
using DG.Tweening;
2023-09-14 15:36:17 +08:00
using DragonSoul.Shared;
using TMPro;
using UnityEngine;
2023-09-14 15:36:17 +08:00
namespace UnityTest.ZXL
{
public static class ETTaskHelper
{
public static async ETTask WaitFinish(Action action, ETCancellationToken token = null)
{
ETTask task = ETTask.Create();
await task;
}
/// <summary>
/// 等待完成(自动床奶奶)
/// </summary>
/// <param name="token"></param>
/// <returns></returns>
public static async ETTask<bool> WaitCompletion(ETCancellationToken token = null)
{
ETTask<bool> task = ETTask<bool>.Create();
token?.Add(Cancel);
void Cancel()
{
task.SetResult(false);
token = new ETCancellationToken();
}
var isTrue = await task;
token.Remove(Cancel);
return isTrue;
}
public static async ETTask<bool> WaitCompletion(ETTask<bool> task, ETCancellationToken token = null)
{
task = ETTask<bool>.Create();
token?.Add(Cancel);
void Cancel()
{
task.SetResult(false);
token = new ETCancellationToken();
}
var isTrue = await task;
token?.Remove(Cancel);
return isTrue;
}
2023-09-14 23:09:07 +08:00
public static async ETTask WaitTime(float time, ETCancellationToken token = null)
{
await UniTask.Delay(TimeSpan.FromSeconds(time));
}
public static async ETTask WaitDoTMPText(this TextMeshProUGUI text, string content, string audioName, ETCancellationToken token = null)
{
bool isEnd = false;
var t = 0.1f * content.Length;
DOTween.KillAll();
if (audioName != "")
{
t = Script.AudioManager.Instance.PlayOtherSound(false, $"6-4/{audioName}") + 0.1f;
}
await DOTween.To(() => string.Empty, value => text.text = value, content, t)
.SetEase(Ease.Linear).AwaitForComplete().SuppressCancellationThrow();
}
public static async ETTask WaitAnimatorPlayOver(this Animator animator, string clipName)
{
bool isOver = false;
AnimatorStateInfo animatorInfo;
animator.Play(clipName);
Debug.Log($"Start Play {clipName} Clip");
while (!isOver)
{
await UniTask.NextFrame();
animatorInfo = animator.GetCurrentAnimatorStateInfo(0); //必须放在update里
if ((animatorInfo.normalizedTime > 1.0f) && (animatorInfo.IsName(clipName))) //normalizedTime: 范围0 -- 1, 0是动作开始1是动作结束
{
animator.SetInteger(clipName, 0); //播放完成后回到待机动画
isOver = true;
}
}
Debug.Log($"End Play {clipName} Clip");
}
2023-09-14 15:36:17 +08:00
}
}