- Home /
How do I create Text Fields on the fly?
I'm creating a simulation that allows the user to select items from a 3D product catalogue. I've set up a series of arrays that sorts unique and identical items into groups with a text field where a price can be added to calculate some totals. My problem is that a user can select any number of items, even hundreds if they so choose. Is there a way to adjust the number of text fields based on the number of items chosen? Right now I'm using a finite switch statement:
function GUITextFieldSwitcher(){
GUILayout.BeginVertical();//End the automatic vertical layout
GUILayout.BeginHorizontal();//End the automatic horizontal layout
switch (i7) {
case 0 :
stringToEdit0 = GUILayout.TextField (stringToEdit0, 5, GUILayout.Width(50));
GUILayout.Label("\n "+" "+PriceCalc00.ToString(), mySkin20);
var temp0 : float = 0.0f;
if (stringToEdit0 != ""){
if (float.TryParse(stringToEdit0, temp0)){
PriceCalc00 = Mathf.Clamp(0.00, temp0, PriceCalc00) * pickDuplicateNumber;
}
}
break;
case 1 :
stringToEdit1 = GUILayout.TextField (stringToEdit1, 5, GUILayout.Width(50));
GUILayout.Label("\n "+" "+PriceCalc01.ToString(), mySkin20);
var temp1 : float = 0.0f;
if (stringToEdit1 != ""){
if (float.TryParse(stringToEdit1, temp1)){
PriceCalc01 = Mathf.Clamp(0.00, temp1, PriceCalc01) * pickDuplicateNumber;
}
}
break;
etc, etc, etc. This approach would require a case for each possible item and, therefore, a unique text field variable for each, otherwise the user can't type in unique prices in each field. Is there a way to create text fields on the fly so that I don't have to set up such an immense switch statement?
Answer by aldonaletto · Dec 07, 2011 at 09:29 PM
I'm not sure about what exactly you're trying to do, but if you have a variable number of items maybe a couple of builtin arrays and a for loop can do the job - something like this:
var stringToEdit: String[]; var PriceCalc: float[]; var nItens: int = 0;
// call this function to initialize the arrays to "quant" elements function LoadItems(quant: int){ stringToEdit = new String[quant]; PriceCalc = new float[quant]; nItens = quant; // assign the stringToEdit and PriceCalc elements here }
function GUITextFields(){ GUILayout.BeginVertical();//Start the automatic vertical layout for (var i = 0; i < nItems; i++){ stringToEdit[i] = GUILayout.TextField (stringToEdit[i], 5, GUILayout.Width(50)); GUILayout.Label("\n "+" "+PriceCalc[i].ToString(), mySkin20); if (stringToEdit[i] != ""){ var temp : float = 0.0f; // no need to have different temp temporary variables if (float.TryParse(stringToEdit[i], temp)){ PriceCalc[i] = Mathf.Clamp(0.00, temp, PriceCalc[i]) * pickDuplicateNumber;
} } } GUILayout.EndVertical();//End the automatic vertical layout }
Thanks aldonaletto! That takes care of the dynamic text field creation very nicely, but how would I go about setting up code that would allow the user to edit these text fields? When I run your code the content of the stringToEdit built-in array appears in each text field (as it should) but the user needs to be able to change the content as well, i.e. the price/float to what they prefer, in that field at runtime. I wouldn't need to resize an array because by that stage the user has picked a fixed collection of items. All they are doing is entering their own prices for this collection of items in the text fields and then that is multiplied by the number of items to provide a total.
Your answer
Follow this Question
Related Questions
TextField does not show up 2 Answers
Head Scratcher for GUILayout.textfield 2 Answers
Sideways textfield 1 Answer
Instantiating a text field 0 Answers