Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AudioKit 游戏暂停时 callback 无法回调 #138

Open
Hocchi01 opened this issue Sep 11, 2024 · 1 comment
Open

AudioKit 游戏暂停时 callback 无法回调 #138

Hocchi01 opened this issue Sep 11, 2024 · 1 comment

Comments

@Hocchi01
Copy link

我正在使用 AudioKit 中的

public static AudioPlayer PlaySound(AudioClip clip, bool loop = false, Action<AudioPlayer> callBack = null, float volume = 1.0f)

播放某个 UI 音效,此时游戏时暂停的(Time.timeScale = 0),传入的 callback 事件在音效播放完毕后未执行,而是在游戏恢复时才一起执行。

QF version: 1.0141

@Hocchi01
Copy link
Author

Hocchi01 commented Sep 12, 2024

我了解到 Unity 原生的 AudioSource 没有提供结束事件接口,网上大致通过两种协程方式判断:1)等待 AudioChip.length 时间长度;2)根据 AudioSource.IsPlaying 状态判断。我目前采用第二种方法,并封装了一个自己的 AudioSource:

public void Pause()
{
    _isPaused = true;
    _audioSource?.Pause();
}

public void Resume()
{
    _audioSource?.UnPause();
    _isPaused = false;
}

public void Stop()
{
    _audioSource?.Stop();
}

private IEnumerator FinishCoroutine(Action<MyAudioSource> onFinish)
{
    yield return new WaitUntil(() => !_audioSource.isPlaying && !_isPaused);
    onFinish?.Invoke(this);
}

上述逻辑对我是有效的,它不会受到 Time.timeScale 的影响(因为音频的播放也不会受到该影响,所以我认为这样是更合理的),当 AudioSource 正常播放完毕或者被 Stop 则会调用 onFinish,此外上述逻辑同样适用于 loop=true 的 AudioSource,而使用等待时间来监听是否完成处理 loop 的音频需要额外的逻辑处理。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant