Unity点击一个物体触发声音具体代码应该怎么写?

2024-11-08 07:55:31
有1个网友回答
网友(1):

public AudioClip _AudioClip;
AudioSource m_AudioSource;
void Start () {
m_AudioSource= gameObject.AddComponent();
m_AudioSource.clip = _AudioClip;
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (UICamera.currentObject.CompareTag("Hit"))
{
Debug.Log("Onclick");
m_AudioSource.Play();
}
}
}