2D Toolkit Forum
2D Toolkit => Support => Topic started by: Tiarny on August 13, 2012, 03:39:28 pm
-
I've got some sprites in my scene of various different shapes and I want to detect which one the user is currently hovering over, but only when they are hovering over the "visible" parts of a sprite.
It is easy enough to do a bounding box check to see which sprite the mouse is currently over, but the problem is that this doesn't handle transparency. I was trying to think of the best way to handle this and I believe it would be to pull colour information for the sprite, as then I could do a simply check on the pixel co-ordinate to see if the current part of the sprite is transparent or not. If it is transparent then I can ignore it.
I believe what I need is a way to check the material on the atlas for my sprite, but to do that I would need some kind of bounding rectangle that tells me where my sprite is saved on the atlas (I think, but I might be wrong). Is it possible to get this information from the sprite itself?
-
You could use the Polygon Option in the sprite collection to define the collision areas yourself. That's what i do :)
Edit: I only use it for buttons so there aren't many sprites for me to do it for. Still, it would be faster doing it that way than colorchecking the atlas.
So if you still want a general solution for colorchecking, you can get the textureCoord information from a raycast hit info~.
-
As MooNiZZ says, a polygon collider with caps would be the easiest option. It is likely to be the most efficient too.
-
Yup, that does sound like the perfect implementation. I was able to get it up and running pretty quickly too. That's what I get for not properly reading the documentation! :-[
Thanks for your help!