- Home /
Edit Vector2 values with GUI.TextField
Title kinda says it. Can't find any clue everywhere.
public Vector2 Size = new Vector2( 40, 40 );
GUI.TextField (new Rect (10, 150, 200, 20), Size, 25);
the gui is in OnGUI and vector2 is just in the class
Comment
Best Answer
Answer by drod7425 · Oct 31, 2014 at 07:41 PM
public Vector2 Size = new Vector2( 40, 40 );
void OnGUI () {
float.TryParse (GUI.TextField (new Rect (10, 150, 200, 20), Size.x.ToString(), 25),out Size.x);
float.TryParse (GUI.TextField (new Rect (10, 350, 200, 20), Size.y.ToString(), 25),out Size.y);
}
Note that float.Parse() will raise an exception if the user puts in a bad number (not a number, number too big, etc.), which this code is not catching. If you don't want to deal with exceptions, rewrite the code to use float.TryParse() and check the return value.
Right, I updated the answer to avoid exceptions. Thanks, @robertbu!
Your answer