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 NutellaDaddy · Mar 24, 2014 at 02:17 AM · c#buttonfor-loop

How can I make this for loop go down to the next row when done with 3?(READ FULL)

I have a for loop that makes buttons in a crate. I need it to be so that when the for loop makes 3 buttons, the buttons begin to start on the next row using the variable buttonHeight * cnt to control how far down they go. Can anyone help me out here? Heres the script. using UnityEngine; using System.Collections; using System.Collections.Generic;

 public class OpenCrateScript : MonoBehaviour 
 {
     static public bool lootCrateOpen = false;
     public float maxDistance;
 
     public Texture2D CrateWindow;
     public GUIStyle CrateSlotStyle;
     public GUIStyle CrateButtons;
 
 
     private List<Item> lootItems;
     private int buttonWidth = 78;
     private int buttonHeight = 78;
     void Start () 
     {
         maxDistance = 2.0f;
         lootItems = new List<Item> ();
         Populate ();
     }
     void Update ()
     {
         if (craftingMenu.craftingMenuOn == true || BuildingMenu.buildingMenuOn == true) 
         {
             lootCrateOpen = false;
         }
         if (Input.GetKeyUp(KeyCode.E) && lootCrateOpen == false) {
             RaycastHit hit;
             if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit)) {
                 if (hit.collider.CompareTag("Loot Crate") && Vector3.Distance(hit.collider.transform.position, transform.position) <= maxDistance) {
                     lootCrateOpen = true;
 
                 }
             }
         }
         else if (Input.GetKeyUp(KeyCode.E) && lootCrateOpen == true)
             {
                 lootCrateOpen = false;
                 inventorySystem.inventoryOn = false;
                 inventorySystem.toolbarOn = false;
             }
         if (inventorySystem.inventoryOn == true && Input.GetKeyUp (KeyCode.E)) 
         {
             lootCrateOpen = false;
         }
 
     }
     void OnGUI()
     {
         if (lootCrateOpen == true) 
         {
             GUI.BeginGroup(new Rect(760,30,600,750), CrateWindow);
                 for(int cnt = 0; cnt < lootItems.Count; cnt++)
                     {
                         GUI.Button (new Rect(buttonWidth * cnt,78,buttonWidth,buttonHeight),cnt.ToString(),CrateSlotStyle);
                     }
             GUI.EndGroup();
             inventorySystem.inventoryOn = true;
             inventorySystem.toolbarOn = true;
         }
     }
     void OnMouseEnter()
     {
 
     }
     private void Populate()
     {
         for (int cnt = 0; cnt < 25; cnt++) 
         {
             lootItems.Add (new Item());
         }
     }
 }

Tell me if you need more information! And thanks for any help.

Comment
Add comment · Show 1
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 NutellaDaddy · Mar 24, 2014 at 03:06 AM 0
Share

Please somebody help me!

1 Reply

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

Answer by SirCrazyNugget · Mar 24, 2014 at 09:59 AM

You've got 2 options, use modulo or use 2 for loops

 int offsetX = 100; //position for first button on X axis
 int offsetY = 40; //position for first button on Y axis
 int buttonWidth = 32;
 int buttonHeight = 32;

modulo:

 int totalButtons = 18;
 int maxCols = 6;
 
 for(int i = 0; i < totalButtons; i++){
    Rect r = new Rect ((i % maxCols) * buttonWidth + offsetX, (int)i / maxCols * buttonHeight + offsetY, buttonWidth, buttonHeight);
    //draw button using Rect r
 }

two for loops:

 int maxRows = 3;
 int maxCols = 6;
 
 int offsetX = 100; //start drawing on X axis from 100
     int offsetY = 40; //start drawing on Y axis from 40
     for(y = 0; y < maxRows; y++){
        for(x = 0; x < maxCols; x++){
           Rect r = new Rect (x * buttonWidth + offsetX, y * buttonHeight + offsetY, buttonWidth, buttonHeight);
           //draw button using Rect r
        }
     }

Both of these should draw 18 buttons in a 6 x 3 grid

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

20 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 avatar image avatar image avatar image avatar image

Related Questions

C# For loop in button to set gameObjects in array to active 1 Answer

my wave spawner not working properly 1 Answer

Getting almost nowhere with my buttondrag script 1 Answer

List of Buttons: Adding a listener passes Last element? 2 Answers

(For Loop) Converting Gui Button to GuiText & GuiTexture 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