• 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
0
Question by Dothackking · Jul 19, 2013 at 05:46 PM · c#guiinventory

Is there any easy way to keep buttons inside of box? (c#)

I made an inventory script, but I would love for it to auto-determine how many buttons can fit in a row, and put them on the next row. Is there any way?

Edit: even not "auto determining" but if I say that 2 buttons fit on each row, I would like it to auto-move the next set to the next row and PREFERABLY resize them.

Here is the important part of my current script:

         GUI.BeginGroup (new Rect(Screen.width/2 +100, Screen.height - 300, 400, 100));
         GUI.Box (new Rect(0,0,400,100), "Weapons");
         foreach(GameObject inv in inventory)
             {
             if(invcount < inventory.Count)
                 {
                     invcount ++;
                 }
             if(GUI.Button (new Rect(10 + (invcount - 1) * 205,20,200,30),new GUIContent(inv.name, "Damage: " + inventory[invcount-1].GetComponent<Item>().dmg)))
                 {
                     Debug.Log ("equipping" + inv.name);
                                 if(hasCloned)
                 {
                     Destroy (clone1);
                 }
                 toUpdate = true;
                 hasCloned = true;
                 if (toUpdate)
                     {
                      
                         clone1 = Instantiate(inv, WepHolder.transform.position, WepHolder.transform.rotation) as GameObject;
                         clone1.transform.parent = GameObject.Find ("Main Camera").transform;
                         clone1.SetActive (true);
                         Debug.Log ("You are now using: " + inv.name);
                         clone1.tag = "weaponEquipped";
                         toUpdate = false;
                     }
                 }
             GUI.Label (new Rect(10 + (invcount - 1) * 205, 70, 100, 30), GUI.tooltip);
 
             GUI.tooltip = null;
             }
Comment
Add comment · Show 4
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 perchik · Jul 19, 2013 at 06:04 PM 0
Share
avatar image Dothackking · Jul 19, 2013 at 06:06 PM 0
Share
avatar image perchik · Jul 19, 2013 at 06:13 PM 0
Share
avatar image Dothackking · Jul 19, 2013 at 06:16 PM 0
Share

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by perchik · Jul 19, 2013 at 06:34 PM

You could draw your buttons in a loop and then after [size of row] objects, change the y value.

If you were using GUILayout, it'd be somet$$anonymous$$ng like t$$anonymous$$s:

 int size = inventory.Count
 int rowLen = 5;
 GUILayout.BeginVertical();
 GUILayout.BeginHorizontal();
 for(int i=0; i<size; i++){
     GUILayout.Button( content);
     if( (i+1)%rowLen == 0){ //if the next object needs to be on a new row
         GUILayout.EndHorizontal(); //end row
         GUILayout.BeginHorizontal(); //create new row
     }
 }
 
 GUILayout.EndHorizontal();
 GUILayout.EndVeritcal();


If you're not using GUILayout, you'll have to do somet$$anonymous$$ng a little more fancy like t$$anonymous$$s:

 int xPos =  10 ; //xpos of first button
 int yPos = 10;  //ypos of first row;
 int xSpacing = 5; //horiz distance between buttons
 int ySpacing = 5; //vert distance between buttons
 
 int buttonW = 50;
 int buttonH = 10;
 
 int size = inventory.Count;
 int rowLen = 5; //number of items per row
 
 int currX = xPos;
 int currY = yPos;
 
 for(int i=0; i<size; i++){
     GUI.Button(new Rect(currX, currY, buttonW, buttonH);
     currX += xSpacing;
     if( (i+1)%rowLen == 0 ) 
        currY += ySpacing;
 }
 
 
 

Slight Caveat Don't copy t$$anonymous$$s code verbatim. No clue if it actually works or is the correct syntax, but should be pretty close. I wrote t$$anonymous$$s off the top of my head without Unity around

Comment
Add comment · Show 4 · 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 perchik · Jul 19, 2013 at 06:35 PM 0
Share
avatar image Dothackking · Jul 19, 2013 at 06:42 PM 0
Share
avatar image perchik · Jul 19, 2013 at 07:29 PM 0
Share
avatar image Dothackking · Jul 19, 2013 at 07:46 PM 0
Share

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GUI not working correctly? (Double tooltip display)(c#) 1 Answer

What is frame, How OnGuI is called every frame? 2 Answers

What Am I Doing Wrong? Variable Names 3 Answers

Distribute terrain in zones 3 Answers

Font sizes, GUI, Iphones and even more head scracthing problems 0 Answers


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