Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

RectTransform and events

Discussion in 'UGUI & TextMesh Pro' started by yonek_idreams, Dec 15, 2014.

  1. yonek_idreams

    yonek_idreams

    Joined:
    Sep 10, 2014
    Posts:
    15
    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.
     
  2. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,135
    Add a collider 2D, and then to your camera add a Physics2DRaycaster.
     
  3. yonek_idreams

    yonek_idreams

    Joined:
    Sep 10, 2014
    Posts:
    15
    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.
     
  4. Giometric

    Giometric

    Joined:
    Dec 20, 2011
    Posts:
    170
    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:

    Code (Csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6.  
    7. public class RectBlocker : Graphic
    8. {
    9.     protected override void OnFillVBO(List<UIVertex> vbo)
    10.     {
    11.         base.OnFillVBO(new List<UIVertex>());
    12.     }
    13. }
    14.  
    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.
     
  5. highlyinteractive

    highlyinteractive

    Joined:
    Sep 6, 2012
    Posts:
    94
    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)
     
  6. yonek_idreams

    yonek_idreams

    Joined:
    Sep 10, 2014
    Posts:
    15
    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.
     
  7. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,495
    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.
     
  8. craig4android

    craig4android

    Joined:
    May 8, 2019
    Posts:
    113
    looking also for a solution to this.
     
unityunity