- Home /
NGUI Volume And Quality Controle With Sliders Not working
I have looked all over here and google with no luck!
Ok so I am thinking about buying NGUI for my game but if I cant get these two scripts to work then its a no go. They are Music Slider & Quality Slider.
I am working on these two scripts for my friends game He has NGUI 2.2.0 i think or something like that.
First the script I am using is going to adjust a music volume from an outside object and not the overall sound or sound effects. I got the slider to reconize the outside object and the audio source in it but the slider still will not adjust the volume this is my code so far any incite would be nice as im about to give up .
using UnityEngine;
[RequireComponent(typeof(UISlider))]
[AddComponentMenu("NGUI/Interaction/Sound Volume")]
public class UIMusicVolume : MonoBehaviour
{
UISlider mSlider;
public GameObject MusicObject;
//public AudioSource MusicSound;
void Awake ()
{
mSlider = GetComponent<UISlider>();
//mSlider.sliderValue = NGUITools.soundVolume;
mSlider.eventReceiver = MusicObject;
//MusicSound = (AudioSource)MusicObject.GetComponent ("AudioSource/volume");
}
void OnSliderChange (float val)
{
audio.volume = val;
}
}
And next is to call the graphics quality depending on what value its own slider is 0 being lowest 1 being highest 6 places 0, .2, .4, .6, .8, 1 thouse should call each setting. This is to only reference to how to call them but i cant make sence of any of it.
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
void OnGUI() {
string[] names = QualitySettings.names;
GUILayout.BeginVertical();
int i = 0;
while (i < names.Length) {
if (GUILayout.Button(names[i]))
QualitySettings.SetQualityLevel(i, true);
i++;
}
GUILayout.EndVertical();
}
}
I tried wrighting my own script but it was a fail if anyone could please help. I want to buy it but not if this dont work.
Answer by Alienmadness · Sep 03, 2013 at 04:18 AM
Hope this helps someone else out, it's been posted for a while.
For the volume I used an NGUI slider and attached this bit of code:
using UnityEngine;
using System.Collections;
public class UIVolumeControl : MonoBehaviour {
private UISlider _Volumeslider;
// Use this for initialization
void Start ()
{
_Volumeslider = gameObject.GetComponent<UISlider>();
_Volumeslider.sliderValue = AudioListener.volume;
_Volumeslider.onValueChange += OnValueChange;
}
void OnValueChange(float val)
{
AudioListener.volume = val;
}
}
For the graphics quality selection I used an NGUI slider and attached this bit of code:
using UnityEngine;
using System.Collections;
public class UIQualitySettings : MonoBehaviour {
public float qualityLevel;
private UISlider _sliderQuality;
// Use this for initialization
void Start()
{
_sliderQuality = gameObject.GetComponent<UISlider>();
qualityLevel = QualitySettings.GetQualityLevel();
_sliderQuality.sliderValue = qualityLevel;
_sliderQuality.onValueChange += OnValueChange;
}
// Update is called once per frame
void OnValueChange(float qualityLevel)
{
qualityLevel = qualityLevel/2*10+1;
if (qualityLevel == 1)
QualitySettings.SetQualityLevel (1, true);
print (qualityLevel);
// QualitySettings.currentLevel = QualityLevel.Fastest;
if (qualityLevel == 2)
QualitySettings.SetQualityLevel (2, true);
// QualitySettings.currentLevel = QualityLevel.Fast;
if (qualityLevel == 3)
QualitySettings.SetQualityLevel (3, true);
// QualitySettings.currentLevel = QualityLevel.Simple;
if (qualityLevel == 4)
QualitySettings.SetQualityLevel (4, true);
// QualitySettings.currentLevel = QualityLevel.Good;
if (qualityLevel == 5)
QualitySettings.SetQualityLevel (5, true);
// QualitySettings.currentLevel = QualityLevel.Beautiful;
if (qualityLevel == 6)
QualitySettings.SetQualityLevel (6, true);
// QualitySettings.currentLevel = QualityLevel.Fantastic;
}
}
and finally I used a drop down menu (NGUI UIPopup List) for resolution detection and selection (referenced from here: link text)
2 needed files (apply change and screen resolution) Create a new dropdown list, It should create a new UIPopupList script in that game object. Attach ScreenResolutionChange.cs to the object and change the messageReciever variable is selected to the ScreenResolutionChange object (the slider in this case).
ApplyChangesResolution.cs
using UnityEngine;
using System.Collections;
public class ApplyChangesResolution : MonoBehaviour {
public ScreenResolutionChange messageReceiver;
//NGUI Click function
void OnClick() {
messageReceiver.gameObject.SendMessage("ApplyChanges", SendMessageOptions.RequireReceiver);
}
}
The ScreenResolutionChange.cs file
using UnityEngine;
using System.Collections;
public class ScreenResolutionChange : MonoBehaviour {
public bool isFullscreen = true; //Is this game going to be in fullscreen?
private UIPopupList popupList;
private Resolution[] resolutions;
private string[] resolutionMenuText;
private string selectedWidth;
private string selectedHeight;
void Start() {
popupList = GetComponent<UIPopupList>();
resolutions = Screen.resolutions;
resolutionMenuText = new string[resolutions.Length];
popupList.items.Clear();
for(int i = 0; i < resolutions.Length; i++) {
resolutionMenuText[i] = resolutions[i].width.ToString() + "x" + resolutions[i].height.ToString();
if(resolutionMenuText[i] != "512x384") {
popupList.items.Add(resolutionMenuText[i]);
}
if(resolutionMenuText[i] == Screen.currentResolution.width.ToString() + "x" + Screen.currentResolution.height.ToString()) {
popupList.selection = resolutionMenuText[i];
}
}
}
void Update() {
string[] splitter = {"x"};
string[] curDimensions = popupList.selection.Split(splitter, System.StringSplitOptions.None);
if(curDimensions.Length == 2) {
selectedWidth = curDimensions[0];
selectedHeight = curDimensions[1];
}
}
public void ApplyChanges() {
if((selectedWidth + "x" + selectedHeight) != (Screen.currentResolution.width.ToString() + "x" + Screen.currentResolution.height.ToString())) {
int w = int.Parse(selectedWidth);
int h = int.Parse(selectedHeight);
Screen.SetResolution(w, h, isFullscreen);
}
}
}
Thank you for your answer I will mark this as answered if I can figure out how lol I will try it when I get ngui back thank you for your knowledge I will learn from this and I'm sure you have helped do many people other then me thanks again.
Edit Can some one mark this as answered or tell me how too :-\
idk if it is because I am using the free version of NGUI or if Unity updated or something but I get an error for _Volumeslider.onValueChange += OnValueChange; specifically ".onValueChange", I've seen this solution multiple times but it doesn't work for me
Your answer
Follow this Question
Related Questions
Tracker modules on Android 0 Answers
Why does sound on Android play at double speed and crackle? 3 Answers
Audio controls disabled??! 0 Answers
Calculating rhythm of any music? 3 Answers