0
\$\begingroup\$

I am having a hard time getting my UI to work.

Basically I have a canvas with a UI image A that is the size of the screen. This image has raycast enabled and if effectively receiving inputs.

Not in the canvas I have a sprite B that have a 2D collider attached to it. No matter what I try, the UI image is always consuming the input event when I click on the sprite.

When I disable the image A , the sprite B gets the events, so the setup looks right (camera with Physic2D raycaster for the sprite B, layerMask set to the layer of the sprite B, canvas with Graphic raycaster for the image A, no blocking object, no mask).

What do I need to do so that the sprite B gets the input event BEFORE the image A and consume it?

EDIT: Those two elements are part of the UI and must be interactable at all time. I use a sprite for the second element as it is dragged from the UI inside the game and I don't want to make it an image.

The image A is a big cancel button with opacity 0 on the whole screen that must be active at all time.

I just want to know if the Collider of sprite B can intercept the input before the image A. If it isn't possible, I'll create an Image B instead of the sprite B and destroy it to create a Sprite with the same texture when the is interacted with.

\$\endgroup\$
  • 1
    \$\begingroup\$ Can you show a pic of the issue. Might be easier with a visual representation of what you are trying to achieve. \$\endgroup\$ – Justin Markwell Nov 9 at 14:18
1
\$\begingroup\$

If you need the Image enabled during the process but also do not want it to 'receive input' you can get a reference to the image and disable the Raycast Target. Then re-enable it when you need it.

If you never want it to be selectable you can just uncheck the box in the bottom right.

image.raycastTarget = false;

enter image description here

\$\endgroup\$
  • \$\begingroup\$ Thanks for the input (pun intended), but this approach can't work for my use case. Basically those two elements must be interactable at all time as they are both buttons. I have edited my question accordingly. \$\endgroup\$ – downstroy Nov 9 at 8:12
  • \$\begingroup\$ @downstroy if you want both things to have button functionality then why don't you use button components? The problems you face right now is because you're mixing two kinds of input systems. Is this really necessary? You can still make a button draggable and give it all sorts of other functionality. Just write your own class, which inherits from Button and implements IOnDragBegin, IOnDrag and IOnDragEnd (all three of them must be implemented for it to work) \$\endgroup\$ – iQew Nov 9 at 10:13
  • \$\begingroup\$ If you had the raycast off and right as you clicked on the one you wanted first and then enabled the raycast dynamically it should get the behavior you describe. But a pic would really help visualize and determine the best route to help. I was thinking the same thing as iQew, you may be able to approach it differently all together with a normal button. \$\endgroup\$ – Justin Markwell Nov 9 at 14:26
0
\$\begingroup\$

As per the comments it doesn't look possible to mix input systems together. I will use the answer given by IQew and use plain Image UI Element to interact with the mouse, drag the object and then, when it is in game I will switch it to sprite , either by destroying the gameObject with the image attached and creating a new Sprite in place, or just toggling the Image component off and the spriteRederer component on.

Thanks!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.