public void Play(string name) { SoundsGroup sg = Array.Find(music, m => m.name == name); if (sg == null) { sg = Array.Find(soundEffects, s => s.name == name); } if (sg == null) { Debug.LogWarning("Sound: " + name + " not found!"); return; } Sound randomSound = GetRandomSound(sg); float pitch = randomSound.pitch + UnityEngine.Random.Range(-sg.pitchVariationRange, sg.pitchVariationRange); sg.source.clip = randomSound.clip; sg.source.volume = randomSound.volume; sg.source.pitch = pitch; sg.source.loop = randomSound.loop; sg.source.Play(); }
public static Sound GetRandomSound(SoundsGroup sg) { if (sg.sounds.Length > 0) { int randomIndex = UnityEngine.Random.Range(0, sg.sounds.Length); return(sg.sounds[randomIndex]); } return(null); }