- Home /
Question by
Blue Astronaut · Jan 27, 2014 at 02:12 AM ·
c#performance
c# Adjust In-Game audio
Hello! I require a in-game volume slider that will adjust the games volume. I know that I have to use AudioListener.volume(i think, right?), but how to i put that into c# to edit the volume!
Thank you in advance! :D
Comment
Not quite sure if you mean How to add an UI slider, or how to do it from code? If it's just the code part, the easiest would be to use:
float newVolume = 0.4f;
foreach (var a in FindObjectsOfType<AudioSource>())
a.volume = newVolume;
If you only want to reach the audio component on the same game object as the script, you can use audio.volume = 0.4f;
Best Answer
Answer by clunk47 · Jan 27, 2014 at 02:48 AM
using UnityEngine;
using System.Collections;
public class VolSlider : MonoBehaviour
{
float s = 1.0F;
AudioListener main;
void Start()
{
main = Camera.main.GetComponent<AudioListener>();
}
void Update()
{
main.audio.volume = s;
}
void OnGUI()
{
s = GUI.HorizontalSlider (new Rect(0, 0, 256, 32), s, 0.0F, 1.0F);
}
}