首页 > 解决方案 > 每次按下带有 Button 组件的 GameObject 时,如何播放声音?

问题描述

我试过用这个,但声音不播放。

public class audioManager : MonoBehaviour {

public EventSystem EventSystemManager;

public AudioClip buttonSound;
AudioSource audioSource;
GameObject game_object;

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        game_object = EventSystemManager.currentSelectedGameObject;
        if (game_object.GetComponent<Button>())
            PlaySound(buttonSound);
    }

}

void PlaySound(AudioClip sound)
{
    audioSource = game_object.AddComponent<AudioSource>();
    audioSource.clip = sound;
    audioSource.Play();
    Destroy(audioSource);
}

我怎样才能得到这个工作?我尝试了其他一些技术,但仍然没有结果。

标签: user-interfacebuttonaudio

解决方案


推荐阅读