[英]Adding a Button Click Sound Unity3D 5.4
我一直在尝试向Unity3D游戏中的按钮添加按钮点击声音。 但是,II尝试的所有操作(请参见下文)均无法产生所需的效果(单击时发出声音)。该按钮有效,但不产生声音。 以下链接包含我已经尝试过的内容,并且还附加了我最近在脚本编写中的尝试。 音频被设置为从检查器播放(通过最后一个链接),这就是为什么脚本本身中没有audioSource.play()
原因。 我希望在单击按钮时播放声音:
http://answers.unity3d.com/questions/26241/play-sound-when-gui-button-is-pressed.html http://answers.unity3d.com/questions/182754/play-sound-on-button -press.html http://answers.unity3d.com/questions/857810/play-audio-from-46-ui-button-on-click-script.html
private AudioSource audioSource;
private float audioTimer;
public void LoadLevel(string name)
{
StartCoroutine(AudioLength(audioTimer));
SceneManager.LoadScene(name);
}
public void Next(string name)
{
StartCoroutine(AudioLength(audioTimer));
SceneManager.LoadScene(name);
}
void Start()
{
audioSource = this.GetComponent<AudioSource>();
audioTimer = audioSource.clip.length;
}
void Update()
{
if(Input.GetKeyDown(KeyCode.Escape))
{
SceneManager.LoadScene("Start Menu");
}
else if(Input.GetKeyDown(KeyCode.Q))
{
Application.Quit();
}
}
IEnumerator AudioLength(float audioTimer)
{
yield return new WaitForSeconds(audioTimer);
}
我已经在community.gamedev.tv的两个人的帮助下找到了答案。
首先,问题是由音频播放之前的场景变化引起的。 其次,我必须将我的LoadScene
添加到我的协程中,以使其能够处理延迟,然后加载下一个场景。 我还必须通过协程将name
变量作为字符串传递。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.