1
0
Fork 0
LaboratoryProtection/Assets/UnityTest/ZXL/Scripts/ETTaskHelper.cs

102 lines
3.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using Cysharp.Threading.Tasks;
using DG.Tweening;
using DragonSoul.Shared;
using TMPro;
using UnityEngine;
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;
}
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");
}
}
}