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 HHameline · Apr 28, 2011 at 12:48 PM · eventgetbutton

GUI.Button Functionality which button is clicked

Hello,

I'm new to unity and have hit a snag and I'm hoping that someone can help me out.

I'm trying to create a group of buttons the number of buttons will vary throughout the life time of the application. I'm receiving the total number of buttons I'm going to need at anyone time and calculating from the size of the buttons the correct amount of room for the scroll area they are in. This is all working peachy, what I want to do now is when the user clicks one of the buttons in the scroll area, is open a pop-up window. But I don't know how to access which button is being clicked i have a feeling that because I'm creating my buttons with a for loops I have now no way of accessing their events after the fact.

total = displayItems.Count;

lessThan = (int)(SHOP_WIDTH - 35) / (buttonWidth); ; buttonBuffer = (int)(SHOP_WIDTH - 35 - (buttonWidth * lessThan)) / lessThan; scrollHeight = ((total / lessThan) * (buttonHeight + buttonBuffer)); numrows = total / lessThan;

 if ((total % lessThan) > 0)
 {
     extraItems = total % lessThan;
     numrows++;
     scrollHeight = scrollHeight + (buttonHeight + buttonBuffer);
 } 

 GUI.Box(new Rect(5, 95, (SHOP_WIDTH - 10), (SHOP_HEIGHT - 100)), "");
 scrollViewVector = GUI.BeginScrollView(new Rect(10, 100, (SHOP_WIDTH - 20), (SHOP_HEIGHT - 110)), scrollViewVector, new Rect(0, 0, 0, scrollHeight));        

 for (int i = 0; i < (numrows); i++)
 {            
     if ((i + 1) < numrows)
     {
         for (int j = 0; j < lessThan; j++)
         {
             name = displayItems[count].ItemName;
             if (GUI.Button(new Rect((j * (buttonWidth + buttonBuffer)), (i * (buttonHeight + buttonBuffer)), buttonWidth, buttonHeight), name))
             {
                 openItemWindow(count, name);
             }
             count++;
         }
     }
     else
     {
         for (int j = 0; j < extraItems; j++)
         {
             name = displayItems[count].ItemName;
             GUI.Button(new Rect((j * (buttonWidth + buttonBuffer)), (i * (buttonHeight + buttonBuffer)), buttonWidth, buttonHeight), name);
             count++;
         }
     }
 }

 GUI.EndScrollView();
 GUILayout.EndArea();

This is the part of code that creates the button grid and since unlike Java where you create a button and can access it by name I'm not sure how to do in with a Unity GUI.Button.

Any help would be greatly appreciated!

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
1
Best Answer

Answer by Joshua · Apr 28, 2011 at 01:27 PM

You're absolutely right you can access them by their name! I have no experience with this method, but I assume it'll work, if not - I apologies. Start of by adding an (empty) array which will contain all the buttons.

var buttons : Array = new Array ();

whenever you create a button you'll add it to the array:

for(stuff) {

 newButton = GUI.Button(new Rect((j * (buttonWidth + buttonBuffer)), (i * (buttonHeight + buttonBuffer)), buttonWidth, buttonHeight), name);

 buttons.Add(newButton) //because of your two loops the location in the array will be(i+1)*(lessThan-1)*j or if I'm correct. Edit: I see you have the count var keeping track of this.

}

Now you can reference each single button by looping through this array.

for(k=0;k

if(buttons[k]) {

 openItemWindow(k, name);

}

As I said, untested, but I think it'll work.

Good luck, and for someone who is "new to unity" it looks like you're doing great!

Comment
Add comment · Show 3 · 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 HHameline · Apr 28, 2011 at 01:51 PM 0
Share

Thanks a bunch! It works wonderfully, only been using Unity for about a week, but it's incredible. Just trying to learn everything it can do. Another question slightly off topic, would I be able to access any of the buttons properties? ie. position, or name. The array is an array of boolean values right or is there a way for me to create an array of actual button objects, and I have just overlooked it.

avatar image Joshua · Apr 28, 2011 at 02:06 PM 0
Share

I think it's indeed an array of booleans. You could 'fake' an array of buttons by having this array of booleans be constructed out of an array of Rect's, strings, etc that make up the button and then constantly update the value of each boolean. But for many buttons this would mean looping through each property value array every frame, which is a bit much.

avatar image HHameline · Apr 28, 2011 at 02:18 PM 0
Share

Ok thanks, I was going to try to change the position of the GUI.Window element dynamically based on the buttons positions and was hoping I could easily reference the buttons themselves. Thanks a lot for the help. I will now go tinker with my code some more :D

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

No one has followed this question yet.

Related Questions

Precise Timing 2 Answers

Changing SpriteRenderer color with UnityEvent.Event 1 Answer

How to change a Player direction by colliding a Game Object 1 Answer

IPointerClickHandler works until I add IPointerDownHandler 0 Answers

How to create an in game search engine? And how to select the searched object? 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