Blocking click events on canvas

I have placed a button in the game scene, when that button is click, a canvas is displayed, and canvas works with the new ui system; it contains a scrolling list. behind the canvas there is another scrolling list. When I drag the list on the canvas, the list behind the canvas also scrolls. How can I block click events so objects behind the canvas will not receive them?

Try adding a CanvasGroup to the canvas you are showing on top and check it’s Block Raycasts setting

Still doesn’t work.

1 Like

When you say CanvasGroup do you mean just a Canvas? I can’t find “CanvasGroup” anywhere in the docs or in the component menus.

And when you say “Block Raycasts”, so you mean “Blocking Objects” as in the image below? If so, do you happen to know what the options mean? Again, I can’t find those details in the docs yet.

Thanks!!

1766473--111979--Screen Shot 2014-09-08 at 10.27.15 PM.png

Yes it is just a canvas, not CanvasGroup.

What I want to do is, when canvas is displayed, it should be the only object which detects touch events, and any thing else in the scene should not respond to events.

1 Like

No, not Canvas. CanvasGroup. It’s not under the UI section in the component menus. I think it’s in Misc. Just type it in in the box at the top of the component menu. I actually use this to represent a single window instead of using multiple canvases like the OP is doing. It can block events from being triggered below it. I just don’t know if it only works with items that are in different canvases so you’ll have to try it out.

1 Like

You can also try using one canvas with either panels or regular recttransform game objects that use a canvas group instead of a separate canvas for each pop-up. This is how I manage my pop-ups and windows and if works perfectly for my use case.

1 Like

I’ve got the opposite problem… we have a sub-canvas (a Main canvas at the root, then a child GameObject with a Canvas component) with “Override Sorting” set to true, because we need it sorted differently. Anyway, nothing under this canvas can receive an event. I’ve tried just about everything I can think of, including putting a GraphicsRaycaster on the the object.

Anyone have an idea on how we can make this work?

If I could see the whole project, it could be easier… but maybe cant?
I think it is better to read new UI source code to solve your problem.

CanvasGroup has two main property for event.
interactable & blockRaycasts.

Currently, ‘interactable’ property works for only ‘Selectable’ objects which are ‘Button’, ‘InputField’, ‘Scrollbar’ ‘Slider’ ‘Toggle’. Doesn’t work for the others.

‘blockRaycast’ property works for ‘Graphic’ objects and also ‘Mask(mask does not inherit Graphic class)’, So most of UI Elements work.

It works when you put ‘CanvasGroup’ to the parent GameObject which you want to ignore all, and it will apply for the all children.
If you set ‘blockRaycast’ to false, the ‘GraphicRaycast’ will not catch all the applyed GameObject.

This is what i did for applying interactable property to ‘ScrollRect’.
http://forum.unity3d.com/threads/scrollrect-interactable.284798/

CanvasGroup is the answer. Its listed in AddComponent → Layout.

2 Likes

Here’s what I did to solve this problem:

public class EventBlocker : MonoBehaviour
{
void Awake() {
GetComponent<Button>().onClick.AddListener(OnClick);
}

void OnClick() {
Event.current.Use();
}
} ```

The line in OnClick consumes the event, making all other UI elements ignore it. 
Add this script to any UI element that you wish would block touches/clicks. 
As you can see, it requires a button, to catch the event. 
You can turn transition off, and set the graphic to whatever you want, even transparent, provided it's still there and has "Raycast Target" checked.

I have no idea why this isn't default behavior, or built-in, like in Android where buttons return a Boolean indicating if the event was handled or not. 
But that's the best solution to this that I have found so far.

That shouldn’t work. That really, really shouldn’t work.

The UI is driven through the event system. Event.Current is part of the old IMGui. And OnMouseDown is another system altogether.

You’ve stumbled on something weird. I wouldn’t trust it.

1 Like