Exemplo n.º 1
0
    /// <summary>Command to spawn a prefab that will play a given sound.static Doesn't require object ref</summary>
    /// <param name="clip_name">name of the SoundClip you want to play</param>
    /// <param name="point">position you want to spawn the prefab to (Vector3.zero by default)</param>
    /// <param name="destroy_on_done">sets wether the prefab should be destroyed when done playing</param>
    public static void PlaySound(string clip_name, Action callback = null, bool destroy_on_done = false, Vector3 point = default(Vector3))
    {
        SoundClip selected_sound = AudioManager.instance.FindClipByName(clip_name);

        if (selected_sound == null)
        {
            Debug.LogError("<b>[" + AudioManager.instance.GetType() + "] : </b>Couldn't find SoundClip with name \"" + clip_name + "\"");

            if (callback != null)
            {
                callback.Invoke();
            }
            return;
        }

        AudioSource selected_from_pool = null;

        List <SoundClip> sort = new List <SoundClip>();

        foreach (SoundClip entry in AudioManager.instance.pool)
        {
            if (entry.donePlaying)
            {
                sort.Add(entry);
            }
        }

        if (sort.Count > 0)
        {
            selected_from_pool = sort[0].attachedSource;
            AudioManager.instance.pool.Remove(sort[0]);
        }
        else
        {
            GameObject temp = Instantiate(AudioManager.instance.soundEffectModel);

            if (temp.GetComponent <AudioSource>() != null)
            {
                selected_from_pool = temp.GetComponent <AudioSource>();
            }
            else
            {
                selected_from_pool = temp.AddComponent <AudioSource>();
            }
        }

        selected_sound.attachedSource = selected_from_pool;
        selected_sound.SetupSource(selected_from_pool, point, callback);

        AudioManager.instance.pool.Add(new SoundClip(selected_sound));
        selected_sound.attachedSource.Play();
    }