Ejemplo n.º 1
0
    /// <summary>
    /// Fade out all the sounds that are currently being played.
    /// </summary>
    /// <param name="fncb"></param>
    public static void FadeOutAll(float inSecs, SoundManagerCallback cbfn)
    {
        // Finish all the fades
        foreach (FadeAudioSource fas in asFades)
        {
            fas.audioSrc.volume = fas.targetVolume;
        }

        asFades.Clear();

        FadeOutMusic(inSecs, null);

        FadeOutBackground(inSecs, null);

        for (int i = 0; i < CHANNELS; i++)
        {
            if (fx[i].isPlaying)
            {
                FadeOutSound(i, inSecs, null);
            }
        }

        fadingAllSounds           = true;
        fadeOutAllSoundsCallback += cbfn;
    }
Ejemplo n.º 2
0
    void Update()
    {
        if (soundList == null)
        {
            soundList = gameObject.GetComponent(typeof(SoundList)) as SoundList;
        }

        // Update the position of the music and the background main sound
        if (Camera.main != null)
        {
            music.transform.position      = Camera.main.transform.position;
            background.transform.position = Camera.main.transform.position;
        }

        // Change the volume of the faded sounds
        foreach (FadeAudioSource fas in asFades)
        {
            fas.accumTime = Time.realtimeSinceStartup - fas.initialTime;

            // Increase the volume
            float totalDelta  = fas.targetVolume - fas.initialVolume;
            float deltaVolume = totalDelta * (fas.accumTime / fas.fadeInSecs);

            fas.audioSrc.volume = fas.initialVolume + deltaVolume;

            //Debug.Log(String.Format("totalDelta: {0} deltaVol: {1} vol: {2}", totalDelta, deltaVolume, fas.audioSrc.volume));

            if (fas.accumTime >= fas.fadeInSecs)
            {
                fas.audioSrc.volume = fas.targetVolume;

                // Call the event
                if (fas.fnCb != null)
                {
                    fas.fnCb();
                }

                // Remove the fading
                asFades.Remove(fas);

                // Return on every remove
                return;
            }
        }

        if (fadingAllSounds)
        {
            if (asFades.Count == 0)
            {
                if (fadeOutAllSoundsCallback != null)
                {
                    fadeOutAllSoundsCallback();
                    fadeOutAllSoundsCallback = null;
                }
            }
        }
    }
Ejemplo n.º 3
0
    /// <summary>
    /// Fade out an specific sound.
    /// Do not call other function of the sound manager that requires a callback function before the first
    /// one finishes and call the callback.
    /// </summary>
    /// <param name="channelIdx"></param>
    /// <param name="fncb"></param>
    public static void FadeOutSound(int channelIdx, float inSecs, SoundManagerCallback cbfn)
    {
        FadeAudioSource fas = new FadeAudioSource();

        fas.initialTime   = Time.realtimeSinceStartup;
        fas.accumTime     = 0;
        fas.initialVolume = fx[channelIdx].volume;
        fas.targetVolume  = 0;
        fas.audioSrc      = fx[channelIdx];
        fas.fadeInSecs    = inSecs;
        fas.fnCb         += cbfn;

        asFades.Add(fas);
    }
Ejemplo n.º 4
0
        /// <summary>
        /// Fade out the specified channel
        /// </summary>
        public void FadeOutSound(int chnId, float inSecs, SoundManagerCallback cbFn = null)
        {
            var fas = new FadeAudioSource
            {
                initialTime   = Time.realtimeSinceStartup,
                accumTime     = 0,
                fadeInSecs    = inSecs,
                targetVolume  = 0,
                audioSrc      = _fxASList[chnId],
                initialVolume = _fxASList[chnId].volume,
                fnCb          = cbFn
            };

            _listFaders.Add(fas);
        }
Ejemplo n.º 5
0
    public void FadeInMusic(int sndId, float inSecs, SoundManagerCallback cbfn)
    {
        if (!SoundConfigParams.useGameMusic)
        {
            return;
        }

        SoundProp sp = GetSoundProp(sndId);

        if (sp != null)
        {
            currentMusicSndProp = sp;

            // Set the position of the current camera in order to play the sound balanced
            if (Camera.main != null)
            {
                music.transform.position = Camera.main.transform.position;
            }

            music.clip   = sp.audioClip;
            music.loop   = sp.loop;
            music.volume = 0;
            music.Play();

            FadeAudioSource fas = new FadeAudioSource();

            fas.initialTime   = Time.realtimeSinceStartup;
            fas.accumTime     = 0;
            fas.initialVolume = 0;
            fas.targetVolume  = MusicVolume * MasterVolume * (sp.volume / 100.0f);
            fas.audioSrc      = music;
            fas.fadeInSecs    = inSecs;



            if (cbfn != null)
            {
                fas.fnCb += cbfn;
            }
            else
            {
                fas.fnCb = null;
            }

            asFades.Add(fas);
        }
    }
Ejemplo n.º 6
0
        /// <summary>
        /// Fade in sound
        /// </summary>
        public int FadeInSound(Vector3 pos, string sndId, float inSecs, SoundManagerCallback cbFn = null)
        {
            Debug.Assert(_soundManagerClips.ContainsKey(sndId));
            var smc = _soundManagerClips[sndId];

            var chnId = GetChannelIdx(smc);

            if (chnId != -1)
            {
                var audSrc = _fxASList[chnId];

                // Stop the audiosource, just in case
                audSrc.Stop();

                // Set info on audiosource
                SetInfoOnAudioSource(chnId, smc, pos);

                // Set the volume to zero
                audSrc.volume = 0f;

                // Start play with volume 0
                audSrc.Play();

                var fas = new FadeAudioSource
                {
                    targetVolume  = smc.volume,
                    initialVolume = 0f,
                    fadeInSecs    = inSecs,
                    initialTime   = Time.realtimeSinceStartup,
                    accumTime     = 0f,
                    audioSrc      = audSrc,
                    fnCb          = cbFn
                };

                // Add the fader to the list
                _listFaders.Add(fas);

                return(chnId);
            }
            else
            {
                Debug.LogWarningFormat("[SoundManager] All audiosource are busy. Cannot play sound {0}", smc.name);
            }

            return(-1);
        }
Ejemplo n.º 7
0
    public void FadeInSound(int sndId, float inSecs, SoundManagerCallback cbfn)
    {
        //if (!ConfigParams.useGameMusic) return;
        SoundProp sp = GetSoundProp(sndId);

        if (sp != null)
        {
            int channeldIdx = getChannelIdx(sp);

            if (channeldIdx != -1)
            {
                // Set the position of the current camera in order to play the sound balanced
                if (Camera.main != null)
                {
                    fx[channeldIdx].transform.position = Camera.main.transform.position;
                }

                fx[channeldIdx].clip   = sp.audioClip;
                fx[channeldIdx].loop   = sp.loop;
                fx[channeldIdx].volume = 0;
                fx[channeldIdx].Play();

                FadeAudioSource fas = new FadeAudioSource();

                fas.initialTime   = Time.realtimeSinceStartup;
                fas.accumTime     = 0;
                fas.initialVolume = 0;
                fas.targetVolume  = FxVolume * MasterVolume * (sp.volume / 100.0f);
                fas.audioSrc      = fx[channeldIdx];
                fas.fadeInSecs    = inSecs;

                if (cbfn != null)
                {
                    fas.fnCb += cbfn;
                }
                else
                {
                    fas.fnCb = null;
                }

                asFades.Add(fas);
            }
        }
    }
Ejemplo n.º 8
0
    public void FadeOutMusic(float inSecs, SoundManagerCallback cbfn)
    {
        FadeAudioSource fas = new FadeAudioSource();

        fas.initialTime = Time.realtimeSinceStartup;
        fas.accumTime   = 0;

        if (playingIntro)
        {
            fas.initialVolume = musicIntro.volume;
            fas.audioSrc      = musicIntro;
        }
        else
        {
            fas.initialVolume = music.volume;
            fas.audioSrc      = music;
        }

        fas.fadeInSecs   = inSecs;
        fas.targetVolume = 0;
        fas.fnCb        += cbfn;

        asFades.Add(fas);
    }
Ejemplo n.º 9
0
 /// <summary>
 /// Fade in sound
 /// </summary>
 public int FadeInSound(string sndId, float inSecs, SoundManagerCallback cbFn = null)
 {
     return(FadeInSound(Vector3.zero, sndId, inSecs, cbFn));
 }