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.
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!!
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.
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.
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.
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.
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.