TextureRect instead Sprite2d to detect mouse click with gui_input signal, is that bad?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By skeleton60


using TextureRect over Sprite2D is that bad ? i do it because Sprite2D dont have any signal about mouse event.

in order to detect mouse click over a Sprite2D we have to:

1- add an area2d
2- add a 2d shape
3- resize the shape
4- connect area2d signal

in order to detect mouse click over a TextureRect we have to:

1- connect gui_input signal

is there an easy way to detect mouse event on a sprite2D that i dont know ?

Using TextureRect instead Sprite2D will probably impact my game later ?

What the reason of this Sprite2D node since we have TextureRect node too?

They seem to be the same for me, except Sprite2D cant interact with mouse and TextureRect is designed for UI

so should i use TextureRect when i want a “sprite” that can interact with mouse easily, and use Sprite2D for the rest that dont need to react to mouse ?

:bust_in_silhouette: Reply From: Tweedle

One of the reasons I replaced, in a recent project, an Area2D node by a TextureRect is that I needed to filter the mouse to stop the propagation of the input, control nodes can easily handle this, and I couldn’t find a way to filter mouse input with an Area2D.

But Area2D can have a collision shape or polygon while TextureRect can only have a rectangle collision box.

There are surely a lot more differences that I’m not aware of.

:bust_in_silhouette: Reply From: Inces

Sprite serves visual purpose only. Only Area can detect mouse_enter. Area has advantage over texturerect, as it can have multiple complicated shapes, not just a rectangle. If You only need rectangular shape, You can use texturerect. It is even more optimal sollution this way. However You need to be aware of gui_input handicaps. Gui_input can be consumed by other control nodes that overlay your texture rect. This is why they are mainly used for focusing menus and tooltips - to make sure only one UI element is focused and clickable at a time. So make sure nothing interrupts your mouse detection for texture rect and it will work just fine.