Ejemplo n.º 1
0
        /// <summary>
        /// Play a sound by name (need to be in the Sound list)
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        public DGSoundClip PlaySoundByName(string Name, float volume = -1, bool unique = false, AudioSource useAudioSource = null)
        {
            foreach (DGSoundClip sound in Sounds)
            {
                if (sound.Name == Name)
                {
                    if (unique)
                    {
                        //check if the sound is already playing, if yes, we don't play it
                        if (efxSources.Where(p => p.clip == sound.Audio && p.isPlaying && p.time < 0.2f).ToList().Count > 0)
                        {
                            return(null);
                        }
                    }

                    //before
                    //PlaySound(sound);
                    //return sound;

                    //should create a copy i think
                    DGSoundClip newSound = new DGSoundClip();
                    newSound.Loop            = sound.Loop;
                    newSound.Name            = sound.Name;
                    newSound.Type            = sound.Type;
                    newSound.Volume          = volume != -1 && volume != 0 ? volume : sound.Volume;
                    newSound.Audio           = sound.Audio;
                    newSound.currentPosition = 0f;


                    PlaySound(newSound, useAudioSource);
                    return(newSound);
                }
            }
            return(null);
        }
Ejemplo n.º 2
0
        IEnumerator LoadSoundByResources(DGSoundClip sound, string path)
        {
            var request = Resources.LoadAsync(path);

            yield return(request);

            sound.Audio = request.asset as AudioClip;

            PlaySound(sound);
        }
Ejemplo n.º 3
0
        public DGSoundClip PlaySoundByResources(string path, float volume = 1f, DGSoundType type = DGSoundType.Sound_Fx, bool loop = false)
        {
            DGSoundClip sound = new DGSoundClip();

            sound.Volume = volume;
            sound.Type   = type;
            sound.Loop   = loop;
            StartCoroutine(LoadSoundByResources(sound, path));

            return(sound);
        }
Ejemplo n.º 4
0
        public IEnumerator PlaySoundByPath(string path, DGSoundType type)
        {
            WWW request = new WWW(path);

            // Wait for download to complete
            yield return(request);

            // use request.audio
            AudioClip loadedMp3 = request.GetAudioClip(false, false);

            DGSoundClip newSound = new DGSoundClip();

            newSound.Audio = loadedMp3;
            newSound.Type  = type;

            PlaySound(newSound);
        }
Ejemplo n.º 5
0
        public void StopSound(DGSoundClip clip)
        {
            if (clip == null || clip.CurrentSource == null)
            {
                return;
            }

            //if not same clip, mean we pass the audiosource to another sound and we shouldnt stop it
            if (clip.Audio != clip.CurrentSource.clip)
            {
                return;
            }

            clip.CurrentSource.Stop();
            clip.currentPosition = 0f;

            //remove from paused sound
            pausedSource.Remove(clip.CurrentSource);
        }
Ejemplo n.º 6
0
        public void PauseSound(DGSoundClip clip)
        {
            //little hack

            /*if (clip != null && clip.CurrentSource != null && !clip.CurrentSource.isPlaying)
             * {
             *  clip.currentPosition = -1f;
             *
             * }*/

            if (clip == null || clip.CurrentSource == null || clip.currentPosition == -1f)
            {
                return;
            }

            //add to paused sound
            pausedSource.Add(clip.CurrentSource);

            clip.currentPosition = clip.CurrentSource.time;
            clip.CurrentSource.Pause();
        }
Ejemplo n.º 7
0
        public void PlaySound(DGSoundClip clip, AudioSource useAudioSource = null)
        {
            if (clip.Type == DGSoundType.Sound_Fx)
            {
                AudioSource useSource = GetAvailableEFXAudioSource();

                if (useAudioSource != null)
                {
                    useSource = useAudioSource;
                }

                if (useSource != null)
                {
                    useSource.DOKill();
                    clip.CurrentSource = useSource;

                    useSource.time = 0;

                    //Set the clip of our efxSource audio source to the clip passed in as a parameter.
                    useSource.clip   = clip.Audio;
                    useSource.loop   = clip.Loop;
                    useSource.volume = clip.Volume * volume;
                    useSource.Play();
                }
                else
                {
                    //Debug.Log(" no more sound !! wtf!");
                }
            }
            else
            {
                currentMusic = clip;

                //Set the clip of our efxSource audio source to the clip passed in as a parameter.
                musicSource.clip   = clip.Audio;
                musicSource.loop   = clip.Loop;
                musicSource.volume = clip.Volume * musicVolume;
                musicSource.Play();
            }
        }
Ejemplo n.º 8
0
        public void ResumeSound(DGSoundClip clip)
        {
            //Debug.Log("RESUME SOUND");
            if (clip == null || clip.CurrentSource == null || clip.currentPosition == -1f)
            {
                return;
            }


            if (clip.Audio.length < clip.currentPosition)
            {
                Debug.Log("ERROR HERE WTF HOW POSSIBLE");
            }
            else
            {
                clip.CurrentSource.time = clip.currentPosition;
            }

            //remove from paused sound
            pausedSource.Remove(clip.CurrentSource);

            clip.CurrentSource.Play();
        }
Ejemplo n.º 9
0
 public void FadeOut(DGSoundClip clip, float duration = 0.5f)
 {
     clip.CurrentSource.DOKill();
     clip.CurrentSource.DOFade(0, duration).OnComplete(() => clip.CurrentSource.Stop());
 }