Exemplo n.º 1
0
    /// <summary>
    /// Plays a sound at the location the AudioManager is at.
    /// </summary>
    /// <param name="soundName"> Name of SoundClip played </param>
    /// <param name="loop"> Whether the SoundClip loops </param>
    /// <param name="onFinish"> Function called when the SoundClip is done playing </param>
    /// <returns> The played SoundClip </returns>
    public SoundClip PlaySound(string soundName, ESoundChannel soundChannel, bool loop = false, UnityAction onFinish = null)
    {
        if (!sounds.ContainsKey(soundName))
        {
            throw new KeyNotFoundException($"AudioManager does not have a sound named {soundName}.");
        }
        if (DEBUGGING)
        {
            Debug.Log($"Playing Sound {soundName}");
        }

        SoundClip soundClip = new SoundClip(sounds[soundName]);

        soundClip.soundChannel = soundChannel;
        soundClip.loop         = loop;

        if (onFinish != null)
        {
            soundClip.onFinish += onFinish;
        }

        AudioSource audioSource = soundClip.AttachToAudioSource(NextAudiosource());

        audioSource.spatialBlend     = 0;
        audioSource.transform.parent = this.transform;
        soundClip.audioSource.Play();

        if (!loop)
        {
            StartCoroutine(ExecuteAfterSeconds(soundClip.onFinish, soundClip.Length));
        }

        return(soundClip);
    }