public class AudioPlayer : MonoBehaviour { public AudioSource source; private CancellationTokenSource cancellation; public async UniTask Play() { if (cancellation != null) { cancellation.Cancel(); cancellation.Dispose(); cancellation = null; } cancellation = new CancellationTokenSource(); source.Play(); Debug.Log("Play Start"); var canceled = await UniTask.WaitWhile(() => source.isPlaying || 0 < source.time, cancellationToken: cancellation.Token).SuppressCancellationThrow(); if (canceled) { Debug.Log("Play Canceld."); return; } cancellation.Dispose(); cancellation = null; Debug.Log("Play Finished."); } }