0

在 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();
    }
}
4

1 回答 1

0

现在,您正在淡出场景中的每个音频源,但我认为您只想淡出正在播放钢琴噪音的音频源。如果这是正确的,那么您需要记住哪个音频源正在播放声音,然后只淡出那个!

在 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();
}

说明:
您想要跟踪实际播放的声音,并且只淡出那些歌曲!

于 2019-05-17T01:21:41.493 回答