- Home /
How to input float in textfiled and and let the scrollbar change its value at the same time
Hello, everyone.I wanna to draw a Scrollbar and a textfield at the same time.And when the Scrollbar scroll, the textfield's value change with it, also, when the textfield's text changed, the Scrollbar change its position too. I write the code like this:
GUILayout.BeginVertical();
GUILayout.Label("目间距");
_interocularDis = GUILayout.HorizontalScrollbar(_interocularDis, 0.01f, 0, 1f);
_interocularDis = (float)Math.Round(_interocularDis, digit1);
string textInterocular = GUILayout.TextField(_interocularDis.ToString());
try
{
_interocularDis = (float)Math.Round(_interocularDis, digit1);
//float tmp = float.Parse(textInterocular, System.Globalization.NumberStyles.AllowDecimalPoint);
float tmp = 0.0f;
float.TryParse(textInterocular, out tmp);
_interocularDis = tmp;
// _interocularDis = (float)Convert.ToDouble(textInterocular);
}
catch (Exception e)
{
}
}
GUILayout.EndVertical();
However, i can not input a float number in the textfield, how can i input a float number in the textfield and let the scrollbar change its value at the same time? Any answer will be appreciate!
Answer by GuyTidhar · Jul 11, 2011 at 05:55 AM
You are redefining "textInterocular" instead of just fetching the data.
Define the variable before calling the TextField() function or do that outside of the scope if you need to know of it outside of this scope:
// Defined
string textInterocular
GUILayout.BeginVertical();
GUILayout.Label("目间距");
_interocularDis = GUILayout.HorizontalScrollbar(_interocularDis, 0.01f, 0, 1f);
_interocularDis = (float)Math.Round(_interocularDis, digit1);
// now you can change the value
textInterocular = GUILayout.TextField(_interocularDis.ToString());
try
{
_interocularDis = (float)Math.Round(_interocularDis, digit1);
//float tmp = float.Parse(textInterocular, System.Globalization.NumberStyles.AllowDecimalPoint);
float tmp = 0.0f;
float.TryParse(textInterocular, out tmp);
_interocularDis = tmp;
// _interocularDis = (float)Convert.ToDouble(textInterocular);
}
catch (Exception e)
{
}
}
GUILayout.EndVertical();
it is not the variable "textInterocular"'s problem. I can not input float number either although defined it as a member variable.
Your answer
Follow this Question
Related Questions
Textfield number only 1 Answer
How to make floats in the textfield? 1 Answer
[SOLVED] Huge terrains: how about player's coordinates precision? 1 Answer
if statement with floats in C# . what did i do wrong ? 0 Answers
Unexpected token if 1 Answer