在 Unity 的音乐应用程序中,当您设置淡出以使正在创建的声音在停止时没有点击时,背景中的所有音乐也会进入淡出。我希望能够在不干扰背景音乐的情况下进行淡出,但我不知道如何。这是我的代码:
private AudioSource[] sound;
void OnTouchDown()
{
if (instrument == "Piano")
{
FindObjectOfType<PianoAudioManager>().Play("PianoGmaj1G");
}
void OnTouchUp()
{
sound = FindObjectsOfType(typeof(AudioSource)) as AudioSource[];
foreach (AudioSource audioS in sound)
{
StartCoroutine(AudioFadeOut.FadeOut(audioS, 0.02f));
}
}
当我激活它时如何保存声音,这样我就可以在不影响其他一切的情况下进行淡出?我已经坚持了好几天了,我找不到该怎么做。如果有任何帮助,我将不胜感激。谢谢
编辑。是的,对不起,我的 PianoAudioManager 代码是:
public class PianoAudioManager : MonoBehaviour{
public PianoSound[] PianoSounds;
public static PianoAudioManager instance;
public AudioMixerGroup PianoMixer;
void Awake()
{
if (instance == null)
instance = this;
else
{
Destroy(gameObject);
return;
}
DontDestroyOnLoad(gameObject);
foreach(PianoSound s in PianoSounds)
{
s.source = gameObject.AddComponent<AudioSource>();
s.source.outputAudioMixerGroup = PianoMixer;
s.source.clip = s.clip;
s.source.volume = s.volume;
s.source.pitch = s.pitch;
}
}
public void Play (string name)
{
PianoSound s = Array.Find(PianoSounds, sound => sound.name == name);
if (s == null)
{
Debug.LogWarning("Sound: " + name + " not found!");
return;
}
s.source.Play();
}
}