Beispiel #1
0
        public static void GetSoundsByCategory(ObservableCollection <Sound> sounds, Sound.SoundCategory soundCategory)
        {
            var allSounds      = GetSounds();
            var filteredSounds = allSounds.Where(p => p.Category == soundCategory).ToList();

            sounds.Clear();
            filteredSounds.ForEach(p => sounds.Add(p));
        }
    public void Play(Sound.SoundCategory category)
    {
        // Play a random sound within the chosen category
        Sound[] sounds      = Sounds.Where(s => s.category == category).ToArray();
        Sound   randomSound = sounds[UnityEngine.Random.Range(0, sounds.Length)];

        if (randomSound == null)
        {
            throw new System.Exception("Sound clip of type '" + randomSound.category + "' could not be found.");
        }
        else
        {
            // Make sure to only played sounds that have not been played more than once, if restricted
            if (randomSound.playOnce && randomSound.played)
            {
                return;
            }
            else
            {
                randomSound.source?.Play();
                randomSound.played = true;
            }
        }
    }