public SoundItem(string itemName, AudioCategory catrgory) { AudioPath = $"Assets/Audio/{catrgory.ToString()}/{itemName}.wav"; ImgPath = $"Assets/Images/{catrgory.ToString()}/{itemName}.png"; Catrgory = catrgory; Name = itemName; }
/// <summary> /// Play requested audio on GameObject. Adds AudioSource if necessary. /// </summary> /// <param name="obj">Gameobject that plays the sound</param> /// <param name="type">Type of sound to be made</param> /// <param name="volume">Range: [0,1]</param> /// <param name="loop"></param> public static void PlayAudio(GameObject obj, AudioCategory type, float volume = 1.0f, bool loop = false) { AudioSource source = obj.GetComponent <AudioSource>(); if (source == null) { // Create AudioSource if necessary and set default values. source = obj.AddComponent <AudioSource>(); source.spatialBlend = 1; source.spread = 90; source.rolloffMode = AudioRolloffMode.Linear; source.playOnAwake = false; } // Select random clip List <AudioClip> audioClips = AudioDictionary[type]; // All audio files of this type int number = RNG.Next(0, audioClips.Count); AudioClip audioClip = audioClips[number]; // Loads the audioclip from the dictionary if (audioClip != null) { source.clip = audioClip; source.loop = loop; if (type.ToString().ToLower().Contains("pain") || type.ToString().ToLower().Contains("scream")) { source.volume = 0.2f; } source.Play(); } else { throw new IndexOutOfRangeException( "Unable to select AudioClip. Ensure that lists in AudioDictionary are filled."); } }