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
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

So to clarify, you have some number of buttons and you want to have it draw them automatically, such that n items are on each row, and then it draws the next row?

avatar image Dothackking · Jul 19, 2013 at 06:06 PM 0
Share

preferably I'd like it to see how large inventory.Count is, and make room for (for example) x rows of 2 buttons, but, I'd even be fine with manually specifying how much room is needed and it just automatically puts the buttons in rows of 2. I was thinking a switch or something, but that seems like a lot of work

avatar image perchik · Jul 19, 2013 at 06:13 PM 0
Share

Ah! So you always want rows of size 2 and you want to draw as many rows as you need?

avatar image Dothackking · Jul 19, 2013 at 06:16 PM 0
Share

yes! Exactly! As I said, the switch statement seems a bit more than it could need, although I could be wrong.

I am absolutely horrible at this UI stuff, it took me like 3 days to man up and just make the inventory system.

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 something like this:

 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 something a little more fancy like this:

 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 this code verbatim. No clue if it actually works or is the correct syntax, but should be pretty close. I wrote this 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

I would recommend looking heavily at GUILayout unless you have a reason to hardcode widths and heights

avatar image Dothackking · Jul 19, 2013 at 06:42 PM 0
Share

Wow...I wish I had known about this GUIlayout. I'm trying to help a friend right now, but that looks SO much easier.

avatar image perchik · Jul 19, 2013 at 07:29 PM 0
Share

The only trick with the GUILayout is that BeginVertical and BeginHorizontal are somewhat confusingly named. Begin Vertical creates things in a column, Begin Horizontal creates things in a row

avatar image Dothackking · Jul 19, 2013 at 07:46 PM 0
Share

I'll have to figure the GUILayout thing later. I'm having trouble trying to carry over my current GUIContent stuff over to it.

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

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

how to display the damage value from a mob 1 Answer

Help with GUILayout and tooltip(C#) 1 Answer

Setting Scroll View Width GUILayout 1 Answer

Problems with GUI and Resolutions. (C#) 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