5
\$\begingroup\$

New to Unity...Is there an easy way to block all interaction underneath a panel that I would have stationary at top of my game. There will be a pause button in that panel and some game info. Just don't want the mouse input or clicks to go through the panel and interacting with game below.

\$\endgroup\$

4 Answers 4

7
\$\begingroup\$

You're looking for;

if(!EventSystem.current.IsPointerOverGameObject())

Place this line before your code that handles mouse click on any non-UI componenets to stop their functionality when clicking on the UI; the method will return true whilst over UI components.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ this is not 100% percent working solution \$\endgroup\$
    – Yevhen
    Mar 9, 2018 at 18:39
  • \$\begingroup\$ what is EventSystem equal to though? \$\endgroup\$
    – stackers
    Jan 28, 2020 at 22:26
3
\$\begingroup\$

If you are trying to block interation on other UI elements (for example if this panel is a pop-up submenu or notification, which is overlayed on top of the actual menu, which is also made up of UI elements), then you can tick the "Raycast target" checkbox on the panel's Image component. This will prevent all clicks on the panel from going through the panel and interacting with UI components (such as buttons, checkboxes, input fields, sliders, etc.) underneath the panel.

enter image description here

If, however, you are trying to block interaction with game objects (and not ui elements), then there are also two cases I can think of:

If this panel is meant to stop interaction with the entire game (such as a "Game Options" panel, then you can do what Vadim Tatarnikov suggested and simply have a boolean which tracks whether or not the panel is open. Alternatively, since you mention pausing, you can also use the Time.timeScale static variable to determine whether the game is paused or not and use that to decide how to process clicks.

If this panel is however a screen overlay, where the player can interact with the panel, but also the game (for example a "Unit details" panel in a strategy game), then you can use what MrDiVolo suggested:

if(!EventSystem.current.IsPointerOverGameObject())

With this method, you however need to be very diligent in disabling transparent ui elements, setting the raycast target checkbox properly for all visible ui elements (meaning ticking for elements which should block clicks and unticking for overlays which should not block clicks)

\$\endgroup\$
1
  • 1
    \$\begingroup\$ In addition to checking the Raycast Target item, You also need to add a button component (with nothing in the onClick()) or else it won't grab it for some reason. My use case was clicking the background and not in the main panel to close the main panel. Couldn't get it to work without the button. \$\endgroup\$ Aug 20, 2019 at 0:18
0
\$\begingroup\$

You can make a Boolean variable that is true when the game is paused, and make other objects check if it this variable is true, in addition to the check for Input.

\$\endgroup\$
0
\$\begingroup\$

I had a similar issue and I found this video to be very helpful, all credit to kiwasi.com. It's showing 3 possible solution approaches, from the simplest to most complex (and least to most recommended):

  1. Use EventSystem.current.isPointerOverGameObject() from UnityEngine.EventSystems just as MrDiVolo explains
  2. Event Triggers (check also here)
  3. Event System Interfaces (IPointerClickHandler etc.)
\$\endgroup\$
2
  • 2
    \$\begingroup\$ Keep in mind that the video might get taken down of change links at some point in the future, so including a little detail, even just a one-sentence summary for each option, would make this answer more robust against that. \$\endgroup\$
    – DMGregory
    Feb 13, 2018 at 13:08
  • \$\begingroup\$ That is correct indeed, once I get some spare time for such transcription - I could do it. But since the solution is not mine anyway - please feel free to do so yourself in case I forget. \$\endgroup\$
    – reggie_7
    Feb 14, 2018 at 20:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .