【问题标题】:Adding a Button Click Sound Unity3D 5.4添加按钮单击声音 Unity3D 5.4
【发布时间】:2016-10-25 15:53:11
【问题描述】:

我一直在尝试向我的 Unity3D 游戏中的按钮添加按钮单击声音。然而,我尝试过的一切(见下文)都没有产生预期的效果(点击时发出声音。)按钮工作但不产生声音。以下链接包含我尝试过的内容,并且还附上了我在脚本方面的最新尝试。音频设置为从检查器播放(根据最后一个链接),这就是脚本本身没有 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);
}

【问题讨论】:

    标签: button audio unity3d onclick


    【解决方案1】:

    我在 community.gamedev.tv 的两个人的帮助下找到了答案。

    首先,问题是由于在音频有机会播放之前场景发生了变化。其次,我必须将我的LoadScene添加到我的协程中,以便让它处理延迟,然后加载下一个场景。我还必须通过我的协程将 namevariable 作为字符串传递。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-17
      • 1970-01-01
      • 2017-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-17
      • 1970-01-01
      相关资源
      最近更新 更多