public void PlaySound(Sound.Type soundName) { Sound sound = GetSoundByName(soundName); if (sound == null) { Debug.LogWarning("Could not find Sound " + soundName.ToString()); return; } audioSource.PlayOneShot(sound.clip); audioSource.volume = sound.volume; audioSource.pitch = sound.pitch; }
void Awake() { if (instance == null) { instance = this; } else { Destroy(gameObject); return; } DontDestroyOnLoad(gameObject); for (int i = 0; i < System.Enum.GetValues(typeof(Sound.Type)).Length; i++) { Sound.Type temp_type = (Sound.Type)i; GameObject new_object = new GameObject(temp_type.ToString()); new_object.transform.SetParent(this.transform); } foreach (Sound s in sounds) { if (s.type == Sound.Type.Menu_Sound) { s.source = transform.Find("Menu_Sounds").gameObject.AddComponent <AudioSource>(); } else if (s.type == Sound.Type.Player1_Sounds) { s.source = transform.Find("Player1_Sounds").gameObject.AddComponent <AudioSource>(); } else if (s.type == Sound.Type.Player2_Sounds) { s.source = transform.Find("Player2_Sounds").gameObject.AddComponent <AudioSource>(); } else if (s.type == Sound.Type.Player3_Sounds) { s.source = transform.Find("Player3_Sounds").gameObject.AddComponent <AudioSource>(); } else if (s.type == Sound.Type.Player4_Sounds) { s.source = transform.Find("Player4_Sounds").gameObject.AddComponent <AudioSource>(); } else if (s.type == Sound.Type.All_Players_Sounds) { s.source = transform.Find("All_Players_Sounds").gameObject.AddComponent <AudioSource>(); } else if (s.type == Sound.Type.Environment_Sound) { s.source = transform.Find("Environment_Sound").gameObject.AddComponent <AudioSource>(); } s.source.clip = s.clip; s.source.volume = s.volume; s.source.loop = s.looping; s.source.panStereo = s.stereo; } }