/// <summary> /// Carga un efecto de sonido y lo guarda en el diccionario. /// </summary> /// <param name="tag">Tag identificador del efecto de sonido a cargar.</param> public void LoadSfx(SfxTags tag) { GameObject go = new GameObject(); go.AddComponent <AudioSource>(); go.GetComponent <AudioSource>().clip = GetAudioFromResources(tag); go.AddComponent <AudioFX>(); sfxGOs.Add(tag, go); }
/// <summary> /// Reproduce un audio. /// Si no esta cargado en su diccionario correspondiente lo carga. /// </summary> /// <param name="tag">Tag idenficador del efecto de sonido a reproducir.</param> /// <param name="volume">Volumen al que se reproduce el audio.</param> private void Play(SfxTags tag) { try { if (!sfxGOs.ContainsKey(tag)) { LoadSfx(tag); } sfxGOs[tag].GetComponent <AudioFX>().Play(); } catch (NullReferenceException nre) { Debug.LogError("No se ha podido carga el sfx. Game Object eliminado." + "\n" + nre.ToString() + "\n" + nre.HelpLink); } }
/// <summary> /// Carga una fichero de audio desde la carpeta Resources. /// </summary> /// <param name="tag">Tag identificador del efecto de sonido a cargar</param> /// <returns>AudioClip con el fichero de audio</returns> private AudioClip GetAudioFromResources(SfxTags tag) { string path = Constants.SFX_RESOURCES_PATH; switch (tag) { case SfxTags.FLAP: path += Constants.FLAP_SFX; break; case SfxTags.GRAVITY_SWAP: path += Constants.GRAVITY_SFX; break; case SfxTags.DEATH: path += Constants.DEATH_SFX; break; case SfxTags.PICKUP_BIG: path += Constants.PICKUP_BIG_SFX; break; case SfxTags.PICKUP_SMALL: path += Constants.PICKUP_SMALL_SFX; break; case SfxTags.UI_INTERACTED: path += Constants.UI_INTERACTED_SFX; break; default: path += Constants.FLAP_SFX; break; } return(Resources.Load(path) as AudioClip); }