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 Zastrow89 · Jul 08, 2012 at 10:10 AM · c#guiarrayinventory

multiple rows, in an array in C#

hello there fellow coders! im completely new to the whole unity and coding in general but i want to learn :) im atm building a little inventory, but i have only mannaged the icons to stack downwards not to the right aswell, i want it so that they show up like 5x4 icons right next to eachother all loaded from my array. here is my code this far. i hope you know what i can do :) thanks alot in advance!

     using UnityEngine;
 using System.Collections;
 
 public class inventory : MonoBehaviour {
  public GameObject[] itemSlots = new GameObject[20];
  public int i = 0;
 
  // Use this for initialization
  void Start () {
  
  }
  
  // Update is called once per frame
  void Update () {
  
  }
  
  void OnGUI () {
  Rect r = new Rect(0,0,35,35); 
     for (i = 0; i < itemSlots.Length; i++){
  if (GUI.Button(r,itemSlots[i].GetComponentInChildren().icon)){
  
  }
  r.y += 36;
  }
  
 }
 }
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

2 Replies

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

Answer by whydoidoit · Jul 08, 2012 at 10:31 AM

Just do something like this where you currently add 36 to the r.y:

   r.x += 36;
   if(r.x > 180) {
      r.x = 0;
      r.y += 36;
   }
Comment
Add comment · Show 2 · 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 Zastrow89 · Jul 08, 2012 at 10:34 AM 0
Share

ah that worked! thanks man! :D took a little to understand what you ment hehe had some mouse trapping and stuff happening xD

avatar image whydoidoit · Jul 08, 2012 at 10:41 AM 0
Share

Probably needs to be >= 180 on reflection.

Or you could move the rect definition into your loop and do:

  var r = new Rect((i%5)*36,(i/5) * 36, 35,35);
avatar image
0

Answer by Brian Stone · Jul 08, 2012 at 10:58 AM

The following script will do precisely what you want, and a little more. It creates a grid of buttons with the desired top-left grid offset, column width, button size, and button spacing.

  void OnGUI () 
  {
     // square width of each button in pixels
     int buttonSize = 35; 
     
     // number of pixels between the buttons
     int buttonSpacing = 1; 
     
     // top-left position of the button grid
     int xOffset = 20;
     int yOffset = 20;
      
     // number of columns
     int numCols = 5;
 
     // number of buttons
     int numButtons = 20; // Change this to itemSlots.Length
     
     Rect r = new Rect(0,0,buttonSize, buttonSize); 
     for (int i = 0; i < numButtons; i++)
     {
        r.x = xOffset + (i % numCols) * (buttonSize + buttonSpacing); // column position
        r.y = yOffset + (int)Mathf.Floor((float)i/numCols) * (buttonSize + buttonSpacing); // row position
  
        if (GUI.Button(r, i.ToString())) // change i.ToString() to itemSlots[i].GetComponentInChildren().icon
        {
           // button was clicked
        }
     }
  }
Comment
Add comment · 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

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

7 People are following this question.

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

Related Questions

Mysterious crash involving an array 2 Answers

Which is better for an inventory system, Array or List? 0 Answers

Click on a button that is created post start. 0 Answers

How to go around creating inventory with new GUI (4.6 Beta)? 1 Answer

How to start an inventory system? 2 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