forked from zxl/LaboratoryProtection
81 lines
2.1 KiB
C#
81 lines
2.1 KiB
C#
using Cysharp.Threading.Tasks;
|
|
|
|
using Sirenix.OdinInspector;
|
|
|
|
using System.Threading;
|
|
|
|
using UnityEngine;
|
|
using UnityEngine.Playables;
|
|
|
|
namespace PMaker.Await
|
|
{
|
|
[RequireComponent(typeof(PlayableDirector))]
|
|
public class AwaitTimeline : AwaitBehaviour
|
|
{
|
|
[SerializeField]
|
|
private PlayableDirector _director;
|
|
|
|
private void Reset()
|
|
{
|
|
this._director = GetComponent<PlayableDirector>();
|
|
}
|
|
|
|
public void OnEnd()
|
|
{
|
|
this.SetTrue();
|
|
}
|
|
|
|
public override async UniTask WaitAsync(CancellationToken cancellationToken = default)
|
|
{
|
|
this.SetFalse();
|
|
_director.time = 0;
|
|
_director.Play();
|
|
await this.WaitTrue(cancellationToken);
|
|
if (cancellationToken.IsCancellationRequested)
|
|
{
|
|
_director.Stop();
|
|
}
|
|
}
|
|
|
|
public async UniTask WaitStep(CancellationToken cancellationToken = default)
|
|
{
|
|
if (_director.state != PlayState.Playing && (_director.time == 0 || _director.time == _director.duration))
|
|
{
|
|
_director.time = 0;
|
|
_director.Play();
|
|
}
|
|
else
|
|
{
|
|
_director.Resume();
|
|
}
|
|
this.SetFalse();
|
|
await this.WaitTrue(cancellationToken);
|
|
_director.Pause();
|
|
}
|
|
|
|
public async UniTask WaitStep(float time, CancellationToken cancellationToken = default)
|
|
{
|
|
if (_director.time == 0 || _director.state != PlayState.Paused)
|
|
{
|
|
_director.time = time;
|
|
_director.Play();
|
|
}
|
|
else
|
|
{
|
|
_director.time = time;
|
|
_director.Resume();
|
|
}
|
|
this.SetFalse();
|
|
await this.WaitTrue(cancellationToken);
|
|
_director.Pause();
|
|
}
|
|
|
|
[Button]
|
|
[HideInEditorMode]
|
|
public async UniTask WaitStepTest()
|
|
{
|
|
await this.WaitStep(this.GetCancellationTokenOnDestroy());
|
|
}
|
|
}
|
|
}
|