Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Core · Dec 07, 2011 at 07:49 PM · guilayouttextfield

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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 }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Core · Dec 08, 2011 at 07:04 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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

TextField text pushed to left on space 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges