Search Unity

Blocking click events on canvas

Discussion in 'UGUI & TextMesh Pro' started by m_danish_s, Sep 8, 2014.

  1. m_danish_s

    m_danish_s

    Joined:
    Sep 2, 2014
    Posts:
    13
    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?
     
  2. secondbreakfast

    secondbreakfast

    Joined:
    Jan 5, 2013
    Posts:
    98
    Try adding a CanvasGroup to the canvas you are showing on top and check it's Block Raycasts setting
     
  3. m_danish_s

    m_danish_s

    Joined:
    Sep 2, 2014
    Posts:
    13
    Still doesn't work.
     
    CloudyVR likes this.
  4. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149

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

    Attached Files:

  5. m_danish_s

    m_danish_s

    Joined:
    Sep 2, 2014
    Posts:
    13
    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.
     
    HoangYell likes this.
  6. secondbreakfast

    secondbreakfast

    Joined:
    Jan 5, 2013
    Posts:
    98
    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.
     
    Jani86 likes this.
  7. secondbreakfast

    secondbreakfast

    Joined:
    Jan 5, 2013
    Posts:
    98
    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.
     
    diginikkari likes this.
  8. rlyle1179

    rlyle1179

    Joined:
    Oct 3, 2012
    Posts:
    5
    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?
     
  9. JYKeith

    JYKeith

    Joined:
    Aug 17, 2014
    Posts:
    12
    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/
     
    Last edited: Dec 12, 2014
  10. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    CanvasGroup is the answer. Its listed in AddComponent -> Layout.
     
    Dimkanp and MD_Reptile like this.
  11. maxizrin

    maxizrin

    Joined:
    Apr 13, 2015
    Posts:
    20
    Here's what I did to solve this problem:

        [RequireComponent(typeof(Button))]
    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.
     
  12. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
    AlejMC likes this.