我无法找出通过单击按钮来播放和暂停音频源的正确代码。
这是我的代码
ButtonAction.GetComponent().onClick.AddListener(delegate
{
if (soundTarget.isPlaying)
{
soundTarget.Pause();
}
else if(!soundTarget.isPlaying)
{
soundTarget.Play();
playSound("sounds/English");
}
});
注意:默认情况下它不会播放,因为如果我默认播放它,它只会循环播放。它以某种方式工作,但我需要在播放或暂停之前多次单击该按钮。
帮助我。
最佳答案
很简单:
你的 Unity 场景应该是这样的:
对于我称为“UnityUI”的脚本,如下所示:
public AudioSource MyAudioSource;
public Button PlayBtn;
public PlayButtonText;
void Start()
{
PlayBtn.onClick.AddListener(() =>
{
if (MyAudioSource.isPlaying)
{
MyAudioSource.Stop();
PlayButtonText.text = "Play me!";
}
else
{
MyAudioSource.Play();
PlayButtonText.text = "Stop me!";
}
});
}
希望这有帮助!
祝你编码愉快!
关于c# - Unity 播放暂停音频效果不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51923678/