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 mh4 · May 13, 2013 at 01:43 PM · guibuttonsoverlapresizegui.button

GUI: Overlapping and Box size collision

Hello good people of Unity! Below is a screenshot of my current predicament:

alt text

where my red GUI button overlaps the yellow one on the left but it does not overlap the purple one on its right. I want to make it overlap both buttons when i mouse over it. How do i do this? Also my other problem:

alt text

where the arrow is my mouse position but instead of only the red button being enlarged, the yellow one on the left is enlarged too. How do i resolve this?

Oh and here are my codes: (NOTE: im only showing the yellow and red gui buttons as all 3 buttons are actually identical and so i dont think i need to show the purple one too. So this would mean it's easier to read my code (i hope. lol) )

 void TPToolbar()
 {
     // if user has not yet click the start/play button
     if (!isPlaying)
     {
         TouchInput theTouchInputScript = this.GetComponent<TouchInput>();
         
         // to tell the user how many TP/shapes he has left
         if (tpLimit.Length > 0 && tpButton.Length > 0)
         {
             GUI.Label(new Rect(tpButton[0].x, tpButton[0].y + 50, 100, 20), tpLimit[0].ToString());
         }
             
         if (tpLimit.Length > 1 && tpButton.Length > 1)
         {
             GUI.Label(new Rect(tpButton[1].x, tpButton[1].y + 50, 100, 20), tpLimit[1].ToString());
         }
             
         if (tpButtonNormalTexture.Length > 0 && tpShape.Length > 0 && tpButton.Length > 0)
         {
             Rect buttonToBeDisplayed;
             Texture2D textureToBeDisplayed;
                 
             if(tpButton[0].Contains(Event.current.mousePosition))
             {
                 buttonToBeDisplayed = new Rect( tpButton[0].x - (50/2),
                                                 tpButton[0].y,
                                                 tpButton[0].width + 50, tpButton[0].height + 50);
                 
                 textureToBeDisplayed = tpButtonHighlightTexture[0];
             }
                 
             else
             {
                 buttonToBeDisplayed = tpButton[0];
                 textureToBeDisplayed = tpButtonNormalTexture[0];
             }
                 
             // if the button is presses and its not already grabbing something (creating TP mode)
             if(GUI.Button(buttonToBeDisplayed, textureToBeDisplayed, "label") && (theTouchInputScript.currentState != TouchInput.mouseState.grab) && tpLimit[0] > 0)
             {
                     // do something here
             }
         }
             
         if (tpButtonNormalTexture.Length > 1 && tpShape.Length > 1 && tpButton.Length > 1)
         {
             Rect buttonToBeDisplayed;
             Texture2D textureToBeDisplayed;
                 
             if(tpButton[1].Contains(Event.current.mousePosition))
             {
                 buttonToBeDisplayed = new Rect( tpButton[1].x - (50/2),
                                                 tpButton[1].y,
                                                 tpButton[1].width + 50, tpButton[0].height + 50);
                 
                 textureToBeDisplayed = tpButtonHighlightTexture[1];
             }
                 
             else
             {
                 buttonToBeDisplayed = tpButton[1];
                 textureToBeDisplayed = tpButtonNormalTexture[1];
             }
                 
             // if the button is presses and its not already grabbing something (creating TP mode)
             if(GUI.Button(buttonToBeDisplayed, textureToBeDisplayed, "label") && (theTouchInputScript.currentState != TouchInput.mouseState.grab)  && tpLimit[1] > 0)
             {
                 //// do something here
             }
         }
     }
 }

Any help is greatly appreciated and thanks in advance.

guiask2.png (9.1 kB)
guiask1.png (8.3 kB)
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
0
Best Answer

Answer by robertbu · May 13, 2013 at 08:33 PM

GUI object will be displayed in the order the GUI calls are made, so to make the red overlap the purple, you will need to change the order of your GUI.Button calls. To fix your texture changes, you will need to compare your mouse position against multiple Rects. I don't know how your array is arranged, but the logic will be something like:

 if (tpButton[0].Contains(Event.current.mousePosition) && !tpButton[1].Contains(Event.current.mousePosition)
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 mh4 · May 14, 2013 at 02:29 AM 0
Share

Hi @robertbu thanks for helping me with this. when i saw your solution

 if (tpButton[0].Contains(Event.current.mousePosition) &&!tpButton[1].Contains(Event.current.mousePosition)

i was like "why didn't i think of that?!" hahaha.

also thanks for the tip on the order of GUI calls. i just want to ask though. if i do it this way is it ok?

1) create an array/list (theGUIDrawOrder?) to facilitate the ordering of the calls. (eg.if i want the red button to overlap everything when i mouse over it, then i must call that last right?) but i don't know what the type of array/list should be though. never$$anonymous$$d i'll figure it out myself on that haha

2) then i use a for loop to call the ordering in the OnGUI function.

is this correct or am i doing this wrong?

avatar image robertbu · May 14, 2013 at 03:48 AM 0
Share

$$anonymous$$y personal opinion is that it isn't worth doing an array for only three buttons. The problem is that the array contain an 'if' statement that has to map to different behaviors depending on the index of the array. Not a hard problem, but usually an array is to simplify things, and only three buttons is simpler to implement and to maintain outside of the array. On the other hand, if you have a bunch of buttons...

avatar image mh4 · May 16, 2013 at 06:40 AM 0
Share

Ok thanks for your opinion! :)

this is what i did:

1) initialize the buttons

 // init
             for(int i = 0; i < tpButton.Length; i++)
             {
                 buttonToBeDisplayed[i] = tpButton[i];
                 textureToBeDisplayed[i] = tpButtonNormalTexture[i];
             }

2) check which button has a mouse over

 for(int i = 0; i < tpButton.Length; i++)
             {
                 // if mouse over
                 if(tpButton[i].Contains(Event.current.mousePosition))
                 {
                     indexOfLastDrawn = i;
                     
                     buttonToBeDisplayed[i] = new Rect(  tpButton[i].x - (50/2),
                                                         tpButton[i].y,
                                                         tpButton[i].width + 50, tpButton[i].height + 50);
                 
                     textureToBeDisplayed[i] = tpButtonHighlightTexture[i];
                     break;
                 }
 
             }

3) draw all buttons except for the button that has a mouse over it

 // drawing of the buttons
             // draw it backward as we always want the 1st button to be on top of everything
             for(int i = tpButton.Length - 1; i > -1; i--)
             {
                 if (i != indexOfLastDrawn)
                 {
                     if(GUI.Button(buttonToBeDisplayed[i], textureToBeDisplayed[i], "label") && tpLimit[i] > 0)
                     {
                         // do  something
                     }
                 }
             }

4) then draw the button that has a mouse over it

 if (indexOfLastDrawn > -1)
             {
                 // draw that button
                 if(GUI.Button(buttonToBeDisplayed[indexOfLastDrawn], textureToBeDisplayed[indexOfLastDrawn], "label") && tpLimit[indexOfLastDrawn] > 0)
                 {
                 }
             }

EDIT: this is all in 1 function! :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

14 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

Related Questions

how to get Gui in OnGUI function to resize with the window? 1 Answer

GUI.Button with double functionality 2 Answers

Determining GUI button pressed 2 Answers

GUI Texture Changes Position Depending On Screen Resolution? 1 Answer

GUI.enabled ? 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