RectTransform and events

I’m doing the standard public class GalleryViewControl : UIBehaviour, IDragHandler stuff.
I have troubles with getting the pointer events, everything is OK when a game object has an Image but how to force it to catch the events when there are only RectTransform and GalleryViewControl components in the game object?
Adding Box Collider 2D does not help.
The only way to fake it is to add an empty Image and set the alpha to 0.0
But this is unacceptable as it produces a full screen transparent draw call.

Add a collider 2D, and then to your camera add a Physics2DRaycaster.

Couldn’t I just take something out of the Image class and put it inside my class?
Adding a collider sounds inconsistent. What size should it be to fit to the RectTransform, which camera if it should play along with the Canvas it is inside. It must interact correctly with other RectTransforms around.

Yeah, I agree having to use a collider is a bit inconsistent. It also means having to write code to resize that collider to match the RectTransform (unless there’s a built in way?) Having an invisible ‘screen-blocker’ type is a common enough use case I would think.

What I did to get around it was the same ‘Image with 0 alpha’ thing you did. As you mentioned, it still gets drawn even when fully transparent, so extra draw calls (with possibly screen-sized transparent quads) start to appear. In my case I only needed one or two at a time so it wasn’t a big deal; probably matters more on mobile than desktops.

Anyway, there is a slightly nicer workaround for this. You can create a class that inherits from the Graphic class, like so:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;

public class RectBlocker : Graphic
{
    protected override void OnFillVBO(List<UIVertex> vbo)
    {
        base.OnFillVBO(new List<UIVertex>());
    }
}

When placed on an object with a RectTransform, the script will still have the color/preserve aspect/sprite fields because it inherits from Graphic, but they will go unused; with that override OnFillVBO function, it draws nothing and generates no draw calls. But it does block clicks.

I don’t imagine doing it that way should cause any issues, but I haven’t tested it extensively. Let me know if it works for you.

Does CanvasGroup do what you want? It can block raycasts, but I’m not sure how you’re capturing events (maybe you could add an EventTrigger as well)

Tim, don’t you think that this kind of solutions sucks?
I can’t figure out what is the magic behind events catching in all of the build in UI elements.

The pointer events generally are forcing me to do little hacks of UI.
This is another case where I had to add a fake Image collider so everything would run as I wanted.
Think about ScrollRect that reacts only on the pointer inside the scroll elements. If someone hits the space between elements nothing happens.
If you think about mobile apps you will find out that in case of buttons it is often needed to make the button collider bigger than the button itself.

I think that we need something better than hacking it with empty transparent Images that eat precious transparent draw calls.

Why isn’t only a RectTransform enough to capture the Event System events? That defines the screen rect you’re interested in. It seems a waste to have to add an Image that will then need to be invisible? I did notice that you can also use a Text with an empty string.

looking also for a solution to this.