/// <summary> /// Change the Volume of a sound type. /// Use SoundManager.Instance.soundValues[SoundManager.SoundTypes.TYPE] /// </summary> public void ChangeVolume(float volume, SoundManager.SoundTypes type) { soundValues[type] = volume; List <NamedAudioSource> destroyedSources = new List <NamedAudioSource>(); for (int i = 0; i < namedAudioSources.Count; i++) { if (namedAudioSources[i]._type == type) { if (namedAudioSources[i] != null) { namedAudioSources[i].audioSource.volume = soundValues[type]; } else { destroyedSources.Add(namedAudioSources[i]);//queue for deletion } } } for (int d = 0; d < destroyedSources.Count; d++) { namedAudioSources.Remove(destroyedSources[d]);//EXTERMINATE! EXTERMINATE! } destroyedSources = null; PlayerPrefs.SetFloat("e_sm_music", soundValues[SoundTypes.MUSIC]);//Create one and set to 1 PlayerPrefs.SetFloat("e_sm_effect", soundValues[SoundTypes.EFFECT]); PlayerPrefs.SetFloat("e_sm_voice", soundValues[SoundTypes.VOICE]); PlayerPrefs.SetFloat("e_sm_ambient", soundValues[SoundTypes.AMBIENT]); }
public void PlaySound(AudioClip audioClip, Vector3 position, SoundManager.SoundTypes audioType, bool loop = false) { gameObject.transform.position = position; if (audioSource == null) { audioSource = gameObject.AddComponent <AudioSource>(); } else { //isAlwaysThere = true; loop = audioSource.loop; SoundManager.Instance.AddSound(this); } if (audioSource.clip == null) { audioSource.clip = audioClip; } _type = audioType; audioSource.volume = SoundManager.Instance.soundValues[audioType]; audioSource.loop = loop; if (!loop) { soundTimerEnd = Time.time + audioClip.length;//Mathf.Ceil(audioClip.length * 1000); } audioSource.Play(); }
/// <summary> /// Change the Volume of a sound type. /// </summary> private static void ChangeVolume(SoundManager.SoundTypes type) { NamedAudioSource[] audioSources = GameObject.FindObjectsOfType <NamedAudioSource>() as NamedAudioSource[]; for (int i = 0; i < audioSources.Length; i++) { if (type == audioSources[i]._type) { switch (type) { case SoundManager.SoundTypes.MUSIC: audioSources[i].audioSource.volume = SoundManager.MUSIC_VOLUME; break; case SoundManager.SoundTypes.EFFECT: audioSources[i].audioSource.volume = SoundManager.EFFECT_VOLUME; break; case SoundManager.SoundTypes.VOICE: audioSources[i].audioSource.volume = SoundManager.VOICE_VOLUME; break; case SoundManager.SoundTypes.AMBIENT: audioSources[i].audioSource.volume = SoundManager.AMBIENT_VOLUME; break; default: audioSources[i].audioSource.volume = 1; break; } } } }
void OnGUI() { EditorGUILayout.BeginVertical(); { EditorGUILayout.LabelField("Test Volumes"); EditorGUILayout.HelpBox("You can play around with the volumes here. Be aware the volumes won't be same as the build(if put on another computer)", MessageType.Info); EditorGUILayout.BeginHorizontal(); { SoundManager.Instance.soundValues[SoundManager.SoundTypes.MUSIC] = EditorGUILayout.Slider("Music:", SoundManager.Instance.soundValues[SoundManager.SoundTypes.MUSIC], 0, 1); SoundManager.Instance.ChangeVolume(SoundManager.Instance.soundValues[SoundManager.SoundTypes.MUSIC], SoundManager.SoundTypes.MUSIC); //SoundManager.Instance.MUSIC_VOLUME = EditorGUILayout.Slider("Music:", SoundManager.Instance.MUSIC_VOLUME, 0, 1); } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); { SoundManager.Instance.soundValues[SoundManager.SoundTypes.EFFECT] = EditorGUILayout.Slider("Effect:", SoundManager.Instance.soundValues[SoundManager.SoundTypes.EFFECT], 0, 1); SoundManager.Instance.ChangeVolume(SoundManager.Instance.soundValues[SoundManager.SoundTypes.EFFECT], SoundManager.SoundTypes.EFFECT); } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); { SoundManager.Instance.soundValues[SoundManager.SoundTypes.VOICE] = EditorGUILayout.Slider("Voice:", SoundManager.Instance.soundValues[SoundManager.SoundTypes.VOICE], 0, 1); SoundManager.Instance.ChangeVolume(SoundManager.Instance.soundValues[SoundManager.SoundTypes.VOICE], SoundManager.SoundTypes.VOICE); // SoundManager.Instance.VOICE_VOLUME = EditorGUILayout.Slider("Voice:",SoundManager.Instance.VOICE_VOLUME, 0, 1); } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); { SoundManager.Instance.soundValues[SoundManager.SoundTypes.AMBIENT] = EditorGUILayout.Slider("Ambient:", SoundManager.Instance.soundValues[SoundManager.SoundTypes.AMBIENT], 0, 1); SoundManager.Instance.ChangeVolume(SoundManager.Instance.soundValues[SoundManager.SoundTypes.AMBIENT], SoundManager.SoundTypes.AMBIENT); //SoundManager.Instance.AMBIENT_VOLUME = EditorGUILayout.Slider("Ambient:",SoundManager.Instance.AMBIENT_VOLUME, 0, 1); } EditorGUILayout.EndHorizontal(); EditorGUILayout.HelpBox("Add a named audio source ", MessageType.Info); EditorGUILayout.LabelField("Add a sound!"); audioName = EditorGUILayout.TextField("Name:", audioName); audioClip = EditorGUILayout.ObjectField("AudioClip:", audioClip, typeof(AudioClip)) as AudioClip; looping = EditorGUILayout.Toggle("Looping:", looping); audioType = (SoundManager.SoundTypes)EditorGUILayout.EnumPopup("Audio Type:", audioType); if (GUILayout.Button("Add Sound")) { GameObject newGameObject = new GameObject(audioName); NamedAudioSource namedAudioSource = newGameObject.AddComponent <NamedAudioSource>(); AudioSource audioSource = newGameObject.GetComponent <AudioSource>(); audioSource.clip = audioClip; namedAudioSource._type = audioType; namedAudioSource.audioSource = audioSource; } } EditorGUILayout.EndVertical(); }
public void PlaySound(AudioClip audioClip, Vector3 position, SoundManager.SoundTypes audioType, bool loop = false) { /* * GameObject gameObject = new GameObject("NamedAudioSource"); * gameObject.AddComponent<Transform>(); */ gameObject.transform.position = position; if (audioSource == null) { audioSource = gameObject.AddComponent <AudioSource>(); } else { isAlwaysThere = true; loop = audioSource.loop; } if (audioSource.clip == null) { audioSource.clip = audioClip; } _type = audioType; switch (audioType) { case SoundManager.SoundTypes.MUSIC: audioSource.volume = SoundManager.MUSIC_VOLUME; break; case SoundManager.SoundTypes.EFFECT: audioSource.volume = SoundManager.EFFECT_VOLUME; break; case SoundManager.SoundTypes.VOICE: audioSource.volume = SoundManager.VOICE_VOLUME; break; case SoundManager.SoundTypes.AMBIENT: audioSource.volume = SoundManager.AMBIENT_VOLUME; break; default: audioSource.volume = 1; break; } audioSource.loop = loop; if (!loop && !isAlwaysThere) { Timer endTimer = new Timer(Mathf.Ceil(audioClip.length * 1000)); endTimer.Elapsed += EndTimer_Elapsed; endTimer.Start(); //Debug.Log(audioClip.length + ":" + endTimer.Interval); } audioSource.Play(); }