Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Is it possible to have two buttons on top of each other both respond to mouse events?

Discussion in 'UGUI & TextMesh Pro' started by Edvard-D, Aug 30, 2014.

  1. Edvard-D

    Edvard-D

    Joined:
    Jun 14, 2012
    Posts:
    125
    The title pretty much says it all. I've been looking around for an answer but haven't found anything yet. Not sure what other details might be needed but if there's anything that needs to be explained or cleared up let me know!
     
    Tom980 likes this.
  2. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    I'm sure it could be made to happen somehow, but I can't for the life of me imagine *why* you'd do such a thing? Without knowing that, I can't being to work through what should actually be done in that situation.
     
    Arowx likes this.
  3. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    Are they different in size, e.g. is one button smaller and inset and the other larger?

    If so they you only need to detect the smaller inset button a have it's trigger code call the larger buttons code. You could also have the smaller inset button update the larger buttons appearance if needed.

    Or are you asking can you create GUI events within code?
     
  4. Edvard-D

    Edvard-D

    Joined:
    Jun 14, 2012
    Posts:
    125
    As for the reason why, I have one button that acts as a test for mouse over events. When hovered over it causes a menu to drop down, which has other buttons on it. The mouse over button needs to stay in the same place since it also causes the menu to be hidden when the mouse is moved away from that area, but the other buttons are within the mouse over space.

    Maybe there's a better way to implement the above rather than using a button for mouse over events? I can always do the mouse position check in code but I was hoping to use the event system since it's much cleaner.
     
  5. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    So wouldn't that work better with a mouse over on the first button and another mouse over on the menu panel, then when the user moves off the menu it will pop up/off.
     
  6. Edvard-D

    Edvard-D

    Joined:
    Jun 14, 2012
    Posts:
    125
    Here's an example of the menu in its two states: hidden at the top and visible at the bottom. The transparent box represents the hover area (not normally visible). Each of the circles are individual buttons that need to respond to their own unique hover events and be selectable. Note that in the hierarchy the buttons and number are all children of the same parent object used to position them; the hover area is separate to ensure it stays in the same position.

    Because of this the hover area has to come after the other objects in the hierarchy or it won't respond to events. The problem then is that when active nothing below it will respond to events, such as the buttons.



    If I'm understand what you're suggesting correctly you recommend adding an on hover event to the hover area and another on hover off event to the button area. The problem this creates is that when I move out of the button area the hover button will get reactivated, causing the menu to get stuck in the visible state. I've tried playing around with the size of the hover area to make it smaller than the button space, but the menu will automatically start moving back to the hidden position so it's not reliable enough. Add an on hover off event to the hover area as well causes it to activate and deactivate over and over.
     
    Last edited: Aug 30, 2014
  7. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    Sounds like you need a delayed reactivation of the button triggered by the off hover event of the pull down menu.
     
  8. Edvard-D

    Edvard-D

    Joined:
    Jun 14, 2012
    Posts:
    125
    That'll probably have to do for now, but there's still the chance of the player leaving their cursor in the hover area causing it to get stuck until they hover back over and off the buttons.