如何在运行时在 Unity c# 中管理 AudioSources
How to manage AudioSources at runtime in Unity c#
在 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();
}
}
现在,您正在淡出场景中的每个 AudioSource,但我认为您只想淡出播放钢琴噪音的音频源。如果这是正确的,那么您需要记住哪个音频源正在播放声音,然后淡出那个!
Add/change 这在 PianoAudioManager 中:
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();
}
解释:
您想跟踪实际播放的声音,只淡出那些歌曲!
在 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();
}
}
现在,您正在淡出场景中的每个 AudioSource,但我认为您只想淡出播放钢琴噪音的音频源。如果这是正确的,那么您需要记住哪个音频源正在播放声音,然后淡出那个!
Add/change 这在 PianoAudioManager 中:
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();
}
解释:
您想跟踪实际播放的声音,只淡出那些歌曲!