- Home /
Unity 4.6B UI Scrollbar Usage
Hey Unitarians,
I've downloaded the new 4.6 Beta UI, and it works for the most part, flawlessly. However, I'm confused on how to use the new scrollbar. I can get it to change it's value in game, but how would I detect the value and add it to playerprefs? Here is my code:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class MainMenuController : MonoBehaviour {
public Scrollbar SensitivityBar;
private float tempSensitivity;
private float SensitivityValue;
void Start () {
if(PlayerPrefs.HasKey("Sensitivity")){
SensitivityValue = PlayerPrefs.GetFloat ("Sensitivity");
}else{
SensitivityValue = 0.5f;
}
SensitivityBar.value = SensitivityValue;
tempSensitivity = SensitivityValue;
}
void Update () {
SensitivityValue = SensitivityBar.value;
if(SensitivityValue != tempSensitivity) {
SensitivityChange (SensitivityValue);
}
tempSensitivity = SensitivityValue;
}
void SensitivityChange (float sensitivityValue) {
PlayerPrefs.SetFloat("Sensitivty", sensitivityValue);
PlayerPrefs.Save ();
}
}
Answer by Gizmoi · Sep 02, 2014 at 02:36 PM
I see no problems with your code.
But if you made your SensitivityChange function public. You could use it as the OnValueChanged callback in the ScrollBar inspector. Which would negate the use of your Update and tempSensitivity.
I tried using that but I coudln't figure out how to send the data of the Scrollbar.value.
And the problem with the code is that when I start the scene, the scrollbar handle is all the way at the left. I move it to the right. Then I stop and restart the scene, and the handle should be on the right. For some reason, it's on the left again.
It works now It seemed I had a typo and wrote PlayerPrefs.SetFloat("Sensitivty" ins$$anonymous$$d of PlayerPrefs.SetFloat("Sensitivity" But your OnValueChanged thing helped :)
Your answer
Follow this Question
Related Questions
Create big city for car game? 2 Answers
C# Applying Transparency to a Single GUI.Button 1 Answer
C# Boolean Doesn't Change Value 1 Answer
Enable and disable a button selection 3 Answers