Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to make UI panel block mouse events to gameObjects in the background?

Discussion in 'UGUI & TextMesh Pro' started by RadioVillain, Aug 1, 2023.

  1. RadioVillain

    RadioVillain

    Joined:
    May 9, 2018
    Posts:
    3
    Hi all, I have tried multiple solutions on the forums regarding this issue, but nothing quite worked, nor do I know if what I'm doing is incorrect.

    I have a 2D grid in the background, and a UI panel in the foreground (hierarchy: UI Canvas > Image).
    The canvas render mode is Screen Space - Overlay.
    Its Graphics Raycaster has Blocking Objects = None and Blocking Mask = Nothing.
    The child items all have Raycast Target enabled and are in layer UI, separate from the grid layer beneath.

    As can be seen in this screenshot, the darker panel has allowed a mouse hover event to pass through, lighting up one of the grid tiles beneath.

    upload_2023-8-1_21-8-43.png

    Tried multiple things, but decided to go back to square one and see if I'm doing anything wrong. Is it possible for Screen Space Overlay objects to be detected by Raycasting even?

    Thanks in advance :)
     
  2. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    449
    I have done some tinkering with the Graphics Raycaster and have found that to the death of me I cannot find Blocking Objects or Blocking Mask to have any functionality whatsoever. Unless somebody can correct me (which I'd love to happen) they are dead to me. I haven't found one google hit with anybody describing any successful use of these, just people expressing their headache.

    These methods work, however.

    Method 1. Use Input System UI Input Module (assuming the new Input System) and UI component Raycast Target switches. This will enable the IPointerXXXHandlers to fire. The UI components will respect their hierarchy order in terms of blocking unless overridden with sort order or a sorting layer.

    Method 2. Use the Graphic Raycaster manually to get the full stack of objects on the canvas under the mouse. Then do whatever you need with the stack.
     
    RadioVillain likes this.
  3. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    I'm not too experienced with UI, but if mouse is using raycast, and you want certain states of the game to have certain things not hit by the raycast, you can either:

    turn raycast off, if mousePos = value(complicated)
    or
    make a new panel, that accepts raycasts but does nothing, and set layers appropriately. As in anything to hit, above block panel layer, and ignored stuff behind that layer.

    But if you're not using using raycast on the UI, and it's just mousePos correlation, then just turn off world raycast when mousePos is in certain areas(screen position).
     
  4. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    530
    BlockingObjects and BlockingMask control which world objects can block clicks on canvas objects not the other way around. This is only makes sense for specific world space or even more rare screen space camera setups. In overlay canvas mode it doesn't do anything. Would have been less confusing if Unity had implemented custom component editor which automatically hides those options when not relevant.
     
  5. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    449
    I have tested this by using World Space and placing 3D objects with colliders between the canvas and the camera, using different layers or with selecting BlockingObjects=3D. They have no effect whatsoever in whether the pointer handlers register on the canvas.
     
  6. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    530
    Just did similar test and it worked just fine. Changed blocking object from 3d to none, and the cube stopped blocking button. Changed mask from Everything to Nothing at it also caused button to stop being blocked by cube.

    upload_2023-8-2_19-33-48.png
     
  7. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    449
    Interesting. I tested both. Btw are you using the legacy Input Manager or the new Input System?
     
  8. RadioVillain

    RadioVillain

    Joined:
    May 9, 2018
    Posts:
    3
    Thank you all for answering :) After following some of the suggestions here, I found the root of the issue. The image/panel that i was trying to enlist as a click blocker was itself also a canvas for its children. Canvasses, it turns out cannot receive mouse events?

    Anyways, adding a transparent image object under the panel with a Click Blocking script it seems will do the trick :cool:
     
    karliss_coldwild likes this.
  9. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    Yes, as far as I'm aware a Canvas is just like a painters canvas, all it does is allow images to be displayed. As far as any functions/interactions that's purely up to the gameObject(image/button/panel/etc...) to handle any mouse states at position, or raycast hits.

    That's why I mentioned the extra panel to pop up to block all interactions behind it, which is most commonly shown in UI tutorials for things like a pause menu. There are other ways to handle the same thing, but as far as I know, it's the easiest/quickest way.
     
    RadioVillain likes this.
  10. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    449
    With the new Input System, I'd say the recommended and easiest way is to disable your gameplay action map. This way you won't just block your mouse clicks but all in one go also things like rotating and moving your player.