How to add polygonCollider2D to fit the UI image shape by code?

Viewed 94

I have 2D character images with transparent backgrounds and I would like to make sure that the collider is the exact shape as the image of that character. Doing it manually from the collider edit takes a lot of time to adjust because you have to outline it around their body and so on. So I want to do this by code by simply taking the sprite/image and using that exact shape to come up with a collider shaped the same.

Some online stuff also suggested to use Raycaster2D instead to see if it hits the sprite only but I can't figure how that works.

I need it for pop up tooltip that shows the character's name when you hover over any part of their body. I looked in a lot of places and tried whatever solutions I could find online but no solution so far. If anyone can point me to the right direction and maybe even give a basic example then I would really appreciate it.

Edit: Using Sprite Renderer component works with polygoncollider2D but Image component doesn't.

0 Answers
Related