在统一的音乐应用程序中,当您放置淡入淡出时,创建的声音在停止时就不会单击,所有的音乐都在后台中也进入消退。我希望能够在不干扰背景音乐的情况下进行淡出,但我不知道如何。这是我的代码:
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));
}
}
激活声音时如何保存声音,以便我可以在不影响其他所有内容的情况下进行淡出?我已经陷入困境了几天,我找不到该怎么做。我会非常感谢任何帮助。谢谢你
编辑。是的,对不起,我的钢琴魔术师代码是:
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();
}
}
现在,您正在淡出场景中的每一个AudioSource,但我认为您只想淡入播放钢琴噪音的AudioSource。如果这是正确的,那么您需要记住哪个AudioSource在播放声音,然后淡出那个声音!
在钢琴魔术师中添加/更改此信息:
public List<PianoSound> soundsToFadeOut = new List<PianoSound>();
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();
soundsToFadeOut.Add(s);
}
并更改您的其他功能:
void OnTouchDown()
{
if (instrument == "Piano")
{
PianoAudioManager playable = FindObjectOfType<PianoAudioManager>();
playable.Play("PianoGmaj1G");
}
void OnTouchUp()
{
PianoAudioManager playable = FindObjectOfType<PianoAudioManager>();
foreach (PianoSound s in playable.soundsToFadeout)
{
StartCoroutine(AudioFadeOut.FadeOut(s.source, 0.02f));
}
playable.soundsToFadeout.Clear();
}
解释:
您想跟踪实际播放的声音,只会淡出这些歌曲!