예제 #1
0
 /// <summary>
 /// Plays audio clip with given name
 /// </summary>
 /// <param name="name">name of sound to play</param>
 public void Play(AudioClipName name)
 {
     if (audioSource != null)
     {
         audioSource.PlayOneShot(audioClips[name]);
     }
 }
예제 #2
0
 private static void AddClip(AudioClipName audioClipName, string audioResourceName)
 {
     if (!audioClips.ContainsKey(audioClipName))
     {
         audioClips.Add(audioClipName, Resources.Load <AudioClip>(audioResourceName));
     }
 }
예제 #3
0
    public void CreateReverseAudioSource(AudioClipName name)
    {
        GameObject ras = Instantiate <GameObject>(reverseAudioSourcePrefab);

        ras.GetComponent <ReverseAudioSource>().Initialize(
            AudioManager.GetAudioClip(name));
    }
예제 #4
0
 /// <summary>
 /// Stops audio clip with given name
 /// </summary>
 /// <param name="name">name of sound to play</param>
 public void StopSource(AudioClipName name)
 {
     if (audioSource != null)
     {
         audioSource.Stop();
     }
 }
예제 #5
0
 /// <summary>
 /// Plays the audio clip with the given name
 /// </summary>
 /// <param name="name">name of the audio clip to play</param>
 public static void Play(AudioClipName name)
 {
     if (!GameManagerSys.isGamePaused)
     {
         audioSource.PlayOneShot(audioClips[name]);
     }
 }
예제 #6
0
 /// <summary>
 /// Plays the audio clip with the given name
 /// </summary>
 /// <param name="name">name of the audio clip to play</param>
 public static void Play(AudioClipName name)
 {
     if (audioSource.enabled)
     {
         audioSource.PlayOneShot(audioClips[name]);
     }
 }
예제 #7
0
    void OnCollisionEnter2D(Collision2D coll)
    {
        GameObject cobj = coll.gameObject;

        if (cobj.tag == "Bullet")
        {
            Destroy(cobj);
            AudioClipName mas = AudioClipName.AsteroidHit;
            AudioManager.Play(mas);

            Vector3 vector3 = new Vector3();
            vector3 = transform.localScale;
            if (vector3.x > 0.5)
            {
                vector3.x               /= 2;
                vector3.y               /= 2;
                transform.localScale     = vector3;
                circleCollider2D         = GetComponent <CircleCollider2D>();
                circleCollider2D.radius /= 2;
                GameObject a1 = Instantiate(gameObject, transform.position, Quaternion.identity);
                a1.GetComponent <Asteroid>().StartMoving();
                GameObject a2 = Instantiate(gameObject, transform.position, Quaternion.identity);
                a2.GetComponent <Asteroid>().StartMoving();
            }
            Destroy(gameObject);
        }
    }
예제 #8
0
    /// <summary>
    /// Plays the audio clip with the given name
    /// </summary>
    /// <param name="name">Name.</param>
    public void PlayOneShot(AudioClipName name, float volumeScale = 1.0f)
    {
        GameState currentGameState = GetGameState();

        if (currentGameState != null && !currentGameState.DisableSoundEffects)
        {
            audioSource.PlayOneShot(audioClips[name], volumeScale);
        }
    }
예제 #9
0
 public static void PlayBackground(AudioClipName name)
 {
     if (!audioSourceBackground.isPlaying || audioSourceBackground.clip == null)
     {
         audioSourceBackground.loop = true;
         audioSourceBackground.clip = audioClips[name];
         audioSourceBackground.Play();
     }
 }
예제 #10
0
    public static void Play(AudioClipName name)
    {
        if (!_initialized)
        {
            return;
        }

        _audioSource.PlayOneShot(audioClips[name]);
    }
예제 #11
0
    public static void PlayBGMusic(AudioClipName name)
    {
        AudioClip bg_music = audioClips[name];

        bgAudioSource.loop   = true;
        bgAudioSource.volume = 0.4f;
        bgAudioSource.clip   = bg_music;
        bgAudioSource.Play();
    }
예제 #12
0
    /// <summary> Fade in of intro music and at its end play other music that will be on loop. </summary>
    /// <param name="_introClipName"> Intro clip name. </param>
    /// <param name="_loopClipName"> Loop clip name. </param>
    /// <param name="_volume"> Optional volume of audio clip. </param>
    private IEnumerator PlayIntroWithFadeAndThenLoopCO(AudioClipName _introClipName, AudioClipName _loopClipName, float _volume = 0.0f)
    {
        repo.music.source.loop = false;
        yield return(StartCoroutine(FadeInMusicCO(_introClipName, _volume)));

        yield return(new WaitWhile(() => repo.music.source.isPlaying));

        PlayMusic(_loopClipName, _volume);
        repo.music.source.loop = true;
    }
예제 #13
0
    /// <summary> Play background music without fade in. </summary>
    /// <param name="_clipName"> Name of audio clip to play on loop. </param>
    /// <param name="_volume"> Optional volume of audio clip. </param>
    private void PlayMusic(AudioClipName _clipName, float _volume = 0.0f)
    {
        repo.music.source.clip = GetClipToEnum(_clipName, SoundType.Music);

        if (!Mathf.Approximately(0.0f, _volume))
        {
            repo.music.source.volume = _volume;
        }

        repo.music.source.Play();
    }
예제 #14
0
 public static AudioClip GetAudioClip(AudioClipName name)
 {
     if (audioClips.ContainsKey(name))
     {
         return(audioClips[name]);
     }
     else
     {
         return(null);
     }
 }
예제 #15
0
    /// <summary>
    /// Plays the given clip as a 3D sound by making the sound originate from the given position
    /// If audio is set to loop it will only stop when either:
    /// <see cref="PauseSounds"/> or <see cref="StopAll"/> are called
    /// or when you stop it through the returned AudioSource
    /// </summary>
    /// <param name="clipName"></param>
    /// <param name="position"></param>
    /// <param name="settings"></param>
    /// <returns></returns>
    public AudioSource PlaySoundAt(AudioClipName clipName, Vector3 position)
    {
        AudioClipInfo   info = GetClipInfo(clipName);
        AudioClip       clip = info.Clip;
        SingleShotAudio fx   = CreateNewSoundSource();

        info.Settings.volume = SoundFxVolume;
        fx.PlaySoundAt(clip, position, info.Settings);
        AudioSource source = fx.Source;

        return(source);
    }
    // Used anytime a menu option is chosen
    public static void Chirp()
    {
        string nameString = "MenuChirp";
        //nameString = nameString.Substring(0, nameString.Length - 1);
        int chirpNum = UnityEngine.Random.Range(1, 6);

        nameString += chirpNum;
        AudioClipName name = (AudioClipName)Enum.Parse(typeof(AudioClipName), nameString);
        AudioClip     clip = soundClips[name];

        gameAudioSource.PlaySound(clip);
    }
예제 #17
0
    /// <summary>
    /// Plays the audio clip with the given name
    /// </summary>
    /// <param name="name">name of the audio clip to play</param>
    public static void Play(AudioClipName name)
    {
        float volumescale;

        if (name == AudioClipName.Catch)
        {
            volumescale = 0.5f;
        }
        else
        {
            volumescale = 1;
        }
        audioSource.PlayOneShot(audioClips[name], volumescale);
    }
예제 #18
0
    /// <summary>
    /// Randomly plays a clip from the list of clips for the specified entry
    /// </summary>
    /// <param name="entry">The key to be played</param>
    public void Play(AudioClipName entry)
    {
        List <AudioClipEntry> audioClips = null;

        if (audioClipDictionary.TryGetValue(entry, out audioClips))
        {
            AudioClipEntry chosenClip = audioClips[UnityEngine.Random.Range(0, audioClips.Count)];
            audioSource.PlayOneShot(chosenClip.audioClip, chosenClip.volume);
        }
        else
        {
            Debug.Log("<color=red>No clip found with this entry</color>");
        }
    }
예제 #19
0
    /// <summary>
    /// Plays the given clip as 2D sound which means it will be heard equally from all speakers
    /// If audio is set to loop it will only stop when either:
    /// <see cref="PauseSounds"/> or <see cref="StopAll"/> are called
    /// or when you stop it through the returned AudioSource
    /// </summary>
    /// <param name="clipName">The name of the clip to play</param>
    /// <param name="volume">Modify the default volume of the clip</param>
    /// <param name="pitch">Modify the pitch of the sound</param>
    /// <returns></returns>
    public AudioSource Play2DSound(AudioClipName clipName, float volume = 1f, float pitch = 1f)
    {
        AudioClipInfo       info     = GetClipInfo(clipName);
        AudioSourceSettings settings = info.Settings;

        // Override settings
        settings.volume = volume * SoundFxVolume;
        settings.pitch  = pitch;

        SingleShotAudio fx = CreateNewSoundSource();

        fx.Play2DSound(info.Clip, settings);

        return(fx.Source);
    }
예제 #20
0
    /// <summary>
    /// Returns the AudipClipInfo assocaited with the given clip name
    /// </summary>
    /// <param name="clipName"></param>
    /// <returns></returns>
    public AudioClipInfo GetClipInfo(AudioClipName clipName)
    {
        AudioClipInfo info = m_clipMapping.ContainsKey(clipName) ? m_clipMapping[clipName] : null;

        if (info == null)
        {
            Debug.LogError($"Clip: '{clipName.ToString()}' has not been assigned in the clips library");
        }
        else if (info.Clip == null)
        {
            Debug.LogError($"'{clipName.ToString()}' has no AudioClip assigned to it");
        }

        return(info);
    }
예제 #21
0
    /// <summary> Search an audio clip inside a specific array. </summary>
    /// <param name="_clipName"> Name of audio clip. </param>
    /// <param name="_type"> Select if clip name parameter is music or effect. </param>
    /// <returns> Return audio clip if exists, otherwise null. </returns>
    private AudioClip GetClipToEnum(AudioClipName _clipName, SoundType _type)
    {
        AudioClip[] pointerClips = _type.Equals(SoundType.Music) ? repo.music.clips : repo.effect.clips;

        for (int i = 0; i < pointerClips.Length; i++)
        {
            if (!pointerClips[i].name.Equals(_clipName.ToString()))
            {
                continue;
            }

            return(pointerClips[i]);
        }

        Debug.LogError("Error: " + _clipName + " was not found, check that enum and clip name is the same\n");
        return(null);
    }
예제 #22
0
        public static void Play(Vector3 position, AudioClipName audioClipName, float volume = 0.5f)
        {
            if (!PlayAudio)
            {
                return;
            }

            for (int i = 0; i < audioSources.Count; i++)
            {
                if (!audioSources[i].isPlaying)
                {
                    audioSources[i].clip = audioClips[(int)audioClipName];
                    audioSources[i].transform.localPosition = position;
                    audioSources[i].volume = volume;
                    audioSources[i].Play();
                    return;
                }
            }
        }
예제 #23
0
    /// <summary> Fade in new music. </summary>
    /// <param name="_clipName"> Name of new audio clip. </param>
    /// <param name="_volume"> Optional volume of audio clip. </param>
    private IEnumerator FadeInMusicCO(AudioClipName _clipName, float _volume = 0.0f)
    {
        repo.music.source.clip = null;

        repo.music.source.volume = 0;
        repo.music.source.clip   = GetClipToEnum(_clipName, SoundType.Music);
        repo.music.source.Play();

        float targetVolume = Mathf.Approximately(0.0f, _volume) ? sourceMusicVolume : _volume;
        float step         = 0;

        while (step < 1)
        {
            step += Time.deltaTime / ConstantValues.FADE_OUT_SWITCH_SCENE * sourceMusicVolume;
            repo.music.source.volume = Mathf.Lerp(0, targetVolume, step);
            yield return(new WaitForEndOfFrame());
        }

        repo.music.source.volume = targetVolume;
    }
예제 #24
0
    /// <summary>
    /// Plays the audio clip with the given name
    /// </summary>
    /// <param name="name">name of the audio clip to play</param>
    public static void Play(AudioClipName name)
    {
        if (name == AudioClipName.die)
        {
            switch (Random.Range(0, 3))
            {
            case 0:
                name = name;
                break;

            case 1:
                name = AudioClipName.die2;
                break;

            case 2:
                name = AudioClipName.die3;
                break;
            }
        }
        audioSource.PlayOneShot(audioClips[name]);
    }
예제 #25
0
    /// <summary>
    /// Plays an instance of the specified AudioClip.
    /// </summary>
    /// <param name="p_audioClipName"> The name of the AudioClip as defined in the enum structure of this class. </param>
    /// <param name="p_audioType"> The type of the sound. This is used for controlling a group of sounds. </param>
    /// <param name="p_loop"> Sets the AudioClip to Loop. By doing so you must stop it manually. </param>
    /// <param name="p_volume"> Sets the volume for this AudioClip. From 0(inclusive) to 1(inclusive). </param>
    /// <returns> Returns an ID for the AudioClip being played, which may be used to stop the sound. </returns>
    public int PlaySound(AudioClipName p_audioClipName, AudioType p_audioType, bool p_loop = false, float p_volume = 1f,
                         float pitchMin = 1f, float pitchMax = 1f)
    {
        CheckForFinishedAudioClips();
        if (_audioSourceInactivePool.Count > 0)
        {
            int[] __parameters = new int[5];
            int   __ID         = _soundIDcounter;

            _soundIDcounter++;
            __parameters[0] = __ID;
            __parameters[1] = (int)p_audioClipName;
            __parameters[2] = (int)p_audioType;
            __parameters[3] = p_loop ? 1 : 0;
            __parameters[4] = (int)(p_volume * _volumeIntegerBase);

            _audioSourceInactivePool[0].clip  = _audioClips[(int)p_audioClipName];
            _audioSourceInactivePool[0].loop  = p_loop;
            _audioSourceInactivePool[0].pitch = (Random.Range(pitchMin, pitchMax));             //Melhoria
            float soundType;
            if (p_audioType == AudioType.MUSIC)
            {
                soundType = musicVolume;
            }
            else
            {
                soundType = sfxVolume;
            }
            _audioSourceInactivePool[0].volume = ((float)__parameters[4] / _volumeIntegerBase) * masterVolume * soundType;
            _audioSourceInactivePool[0].Play();

            AddToActiveList(__parameters);
            return(__ID);
        }
        else
        {
            return(-1);
        }
    }
    /// <summary>
    /// Initializes the audio manager
    /// </summary>
    /// <param name="soundSource">audio source</param>
    public static void Initialize(GameAudioSource source)
    {
        initialized     = true;
        gameAudioSource = source;

        // Load resources, using filenames in AudioClipNames enum

        foreach (AudioClipName clip in AudioClipName.GetValues(typeof(AudioClipName)))
        {
            if (!clip.ToString().StartsWith("Music_"))
            {
                soundClips.Add(clip, Resources.Load <AudioClip>(@"Audio\Sound\" + clip.ToString()));
                //Debug.Log("Loaded: " + clip.ToString());
            }
            else
            {
                // Most music clips include a starter (intro) clip and a looped clip
                musicStartClips.Add(clip, Resources.Load <AudioClip>(@"Audio\Music\" + clip.ToString() + "_start"));
                //Debug.Log("Loaded: " + clip.ToString() + "_start");

                musicLoopClips.Add(clip, Resources.Load <AudioClip>(@"Audio\Music\" + clip.ToString() + "_loop"));
                //Debug.Log("Loaded: " + clip.ToString() + "_loop");
            }
        }

        // Load Player Volume Preferences
        if (PlayerPrefs.HasKey(PlayerPrefsKeys.SoundVolume.ToString()))
        {
            soundVolume = PlayerPrefs.GetFloat(PlayerPrefsKeys.SoundVolume.ToString());
        }
        if (PlayerPrefs.HasKey(PlayerPrefsKeys.MusicVolume.ToString()))
        {
            musicVolume = PlayerPrefs.GetFloat(PlayerPrefsKeys.MusicVolume.ToString());
        }
        gameAudioSource.SetSoundVolume(soundVolume);
        gameAudioSource.SetMusicVolume(musicVolume);
    }
예제 #27
0
    /// <summary> Play one shot an audio clip effect. </summary>
    /// <param name="_clipName"> Name of audio clip to play. </param>
    /// <param name="_volume"> Optional volume of audio clip. </param>
    private void PlayEffect(AudioClipName _clipName, float _volume = 0.0f)
    {
        if (_clipName.Equals(AudioClipName.None) || repo.effect.source.mute)
        {
            return;
        }

        //repo.effect.source.PlayOneShot(GetClipToEnum(_clipName, SoundType.Effect), Mathf.Approximately(0.0f, _volume) ? repo.effect.source.volume : _volume);

        AudioSource targetSource;

        if (repo.effect.source.clip == null)
        {
            targetSource = repo.effect.source;
        }
        else
        {
            targetSource = repo.effect.source.gameObject.AddComponent <AudioSource>();
        }

        float defaultPitch = targetSource.pitch;

        targetSource.pitch  = Random.Range(.925f, 1.075f);
        targetSource.volume = Mathf.Approximately(0.0f, _volume) ? repo.effect.source.volume : _volume;
        targetSource.clip   = GetClipToEnum(_clipName, SoundType.Effect);
        targetSource.Play();

        if (targetSource != repo.effect.source)
        {
            UtilitiesGen.CallMethod(targetSource.clip.length + 0.5f, () => { Destroy(targetSource); });
        }
        else
        {
            targetSource.pitch = defaultPitch;
        }
    }
예제 #28
0
 /// <summary>
 /// Reproduce el sonido del botón
 /// y carga un menú objeto desde la carpeta Resources
 /// </summary>
 /// <param name="menuName">Nombre del objeto menú</param>
 /// <param name="audioName">Nombre del audio a reproducir</param>
 private static void LoadMenuObject(MenuName menuName, AudioClipName audioName)
 {
     AudioManager.Play(audioName);
     Object.Instantiate(Resources.Load(_PREFABS + menuName.ToString() + _MENU),
                        GameObject.FindGameObjectWithTag(_HUD).transform);
 }
예제 #29
0
 /// <summary>
 /// Overlaps audio clip with given name
 /// </summary>
 /// <param name="name">name of sound to play</param>
 public void Overlap(AudioClipName name)
 {
     audioSource.PlayOneShot(audioClips[name]);
 }
예제 #30
0
 public static void PlayOneShotNonRewindable(AudioClipName name)
 {
     audioSourceNonRewindable.PlayOneShot(audioClips[name]);
 }