- Home /
Text.Field erratically updating
Hi Unity Community!
I am in the process of creating a script which will allow users to adjust the vertices of an object (in this trial case, a simple cube) via the GUI in the script. As I am very new to Unity, I have finally managed to get the vertex vectors into the GUI textFields which is great, however the textFields won't allow user input (by 'won't allow' I mean the fields won't let the user delete values of the vertices and replace them with new ones).
The script isn't throwing any errors but via my rudimentary Debug.Log and research on other existing posts on this topic, I think that the fields are constantly being updated outside of the function. If anyone would be able to cast some light as to the cause of this issue or areas for me to look into I would be very, very grateful!
#pragma strict
private var menuArea : Rect;
private var menuAreaHidden : Rect;
private var mesh : Mesh;
var vertVect : Vector3[];
var vertLength : int;
var vertAdjustField : String;
function Start ()
{
menuArea = Rect( 0, 0, Screen.width * 0.1, Screen.height * 0.9 );
menuAreaHidden = Rect( 0, 0, Screen.width * 0.3, Screen.height * 0.15 );
VertReport();
vertAdjustField = String.Empty;
}
function Update ()
{
}
function VertReport()
{
var mesh : Mesh = GameObject.Find("Cube").GetComponent.<MeshFilter>().sharedMesh;
vertVect = mesh.vertices; //original vert positions held in vertVect
for (var i = 0; i < vertVect.Length; i++) //move through ea. of the vertices until end of array
{
Debug.Log( vertVect[i] );
}
mesh.vertices = vertVect;
Debug.Log(mesh.vertexCount);
}
function OnGUI () {
GUI.Box( menuArea, "CubeTest03" );
GUILayout.BeginArea( Rect( menuArea.x + 10, menuArea.y + 30, menuArea.width - 20, menuArea.height - 50 ) );
GUILayout.BeginVertical();
var vertLength = vertVect.Length;
for (var i = 0; i < vertVect.Length; i++) //issue is not here (check w/ limit of 8)
{
Debug.Log("InputFieldCheck");
vertAdjustField = new GUILayout.TextField( "v" + vertVect[i], 25 );
}
GUILayout.EndVertical();
GUILayout.EndArea();
}
Many thanks in advance,
Ryan
I forgot to mention; I'm uncertain if the vertVect.Length is returning the actual length of the array as this seems to go on forever - I'm not sure if this is a related factor or not. However I have tested around this using other < limits to no avail so I'm pretty sure it is not the cause of the issue.
If I understood right... The problem is that you draw textfields with vertex positions in the gui, but you don't do anything significant with the values the user types in.
vertAdjustField = new GUILayout.TextField( "v" + vertVect[i], 25 );
Whatever the user types in, you assign into vertAdjustField. You don't do anything with what the user types in and in the next OnGUI call you just draw the contents of vertVect[i]
again.
You have to parse vertAdjustField
to see what the user tried to set into the vertex, and eventually place it back into vertVect
and the mesh.
Also a cube has 6 sides, each side has 2 triangles, each triangle has 3 vectors so there are supposed to be 36 vectors in the list
Hi @Nose$$anonymous$$ills
Thanks for your prompt response! I have spent the last few days trying to rectify my code inline with your suggestion, however to no avail.
I have tried storing the string result of the textField to another variable to assign back into the textField (I have annotated my logic in the attached code) but can't get anything to work. I'm struggling to find useful tutorials in .js on the topic so would you please expand on where my logic is missing.
$$anonymous$$any thanks in advance! Ryan
#pragma strict
private var menuArea : Rect;
private var menuAreaHidden : Rect;
private var mesh : $$anonymous$$esh;
var vertVect : Vector3[];
var vertLength : int;
var vertField : String;
var vertFieldResult : String;
//var vertTextField : TextField;
function Start ()
{
menuArea = Rect( 0, 0, Screen.width * 0.1, Screen.height * 0.9 );
menuAreaHidden = Rect( 0, 0, Screen.width * 0.3, Screen.height * 0.15 );
VertReport();
vertField = String.Empty;
}
function Update ()
{
}
function VertReport()
{
var mesh : $$anonymous$$esh = GameObject.Find("Cube").GetComponent.<$$anonymous$$eshFilter>().shared$$anonymous$$esh;
vertVect = mesh.vertices; //original vert positions held in vertVect
for (var i = 0; i < vertVect.Length; i++) //move through ea. of the vertices until end of array
{
Debug.Log( vertVect[i] );
}
mesh.vertices = vertVect;
Debug.Log(mesh.vertexCount);
}
function OnGUI () {
GUI.Box( menuArea, "CubeTest03" );
GUILayout.BeginArea( Rect( menuArea.x + 10, menuArea.y + 30, menuArea.width - 20, menuArea.height - 50 ) );
GUILayout.BeginVertical();
var vertLength = vertVect.Length;
for (var i = 0; i < vertVect.Length; i++) //for every vertex in mesh
{
vertField = "v " + vertVect[i];//vertField is assigned vertex vectors from mesh
var vertTextField = new GUILayout.TextField( vertField, 25 );//textField crated for each vector set
}
var vertFieldResult = vertTextField; //vertFieldResult holds the user input return of this textField
vertTextField = GUILayout.TextField( vertFieldResult, 25); //vertTextField is assigned user input
GUILayout.EndVertical();
GUILayout.EndArea();
}