- Home /
Use gui slider to change text field and vice versa?
Hello all,
I am working on a rgb slider where you can also change the value by hand, through a text field. I can get it to work one way but not the other. for example:
var myColor : Color;
var myString : String;
function OnGUI()
{
myColor.r = GUI.HorizontalSlider (Rect (225, 25, 200, 30), myColor.r, 0, 1);
myString = GUI.TextField (Rect (155, 25, 100, 30), myColor.r.ToString());
}
in the example you can move the slider and the text in the box will change but you cant edit the text (i realize why this is, i am wondering if anyone knows of a way to do both)
I am sorry if this question has been asked before or if i am missing an obvious answer!
Thank you for your response!
Comment
Best Answer
Answer by robertbu · Feb 06, 2013 at 08:55 PM
You need to compare the value before and after the GUI.* calls. If they change, make the appropriate changes:
var myColor : Color;
var myString : String;
private var slideVal : float;
private var textVal : String;
function Start() {
slideVal = myColor.r;
textVal = myColor.r.ToString();
}
function OnGUI() {
var f : float;
f = GUI.HorizontalSlider (Rect (225, 25, 200, 30), slideVal, 0, 1);
if (f != slideVal) {
slideVal = f;
myColor.r = f;
textVal = f.ToString();
}
myString = GUI.TextField (Rect (155, 25, 100, 30), textVal);
if (textVal != myString) {
textVal = myString;
if (float.TryParse(myString, f)) {
textVal = myString;
myColor.r = Mathf.Clamp01(f);
slideVal = myColor.r;
}
}
}
I figured it was something along those lines. Thanks for your help!