WXMC/proj/unity/Assets/WishSceneSoundManager.cs

57 lines
1.9 KiB
C#
Raw Permalink Normal View History

2024-12-04 16:18:46 +08:00
using Sirenix.Utilities;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
public class WishSceneSoundManager : MonoBehaviour
{
public GameObject wishCanvasGroup;
public GameObject attackCanvas;
public AudioClip buttonClick;
public AudioClip attack;
public AudioClip bellRing;
// Start is called before the first frame update
void Start()
{
var buttons = wishCanvasGroup.GetComponentsInChildren<Button>(true);
buttons = buttons.Concat(attackCanvas.GetComponentsInChildren<Button>(true)).ToArray();
var buttonClickAudioSource = AddAudioSource(buttonClick);
var attackAudioSource = AddAudioSource(attack);
var bellRingAudioSource = AddAudioSource(bellRing);
foreach (var button in buttons)
{
//Debug.Log(button.gameObject.name);
//var buttonSound = button.gameObject.AddComponent<AudioSource>();
//buttonSound.playOnAwake = false;
switch (button.gameObject.tag)
{
case "Attack":
button.onClick.AddListener(() => attackAudioSource.Play());
//buttonSound.clip = attack;
break;
case "Bell":
button.onClick.AddListener(() => bellRingAudioSource.Play());
//buttonSound.clip = bellRing;
break;
default:
button.onClick.AddListener(() => buttonClickAudioSource.Play());
//buttonSound.clip = buttonClick;
break;
}
//button.onClick.AddListener(() => buttonSound.Play());
}
}
AudioSource AddAudioSource(AudioClip clip)
{
var audioSource = gameObject.AddComponent<AudioSource>();
audioSource.clip = clip;
audioSource.playOnAwake = false;
return audioSource;
}
}