2d Asset automatic collider

Hi guys.

Is there a way to automatically draw polygon collider based on transparency?

I have about 2.000 2d Sprites that Need tight colliders for precise clicking input. its for an hidden object game.

Thanks.

I’m not following what you mean. Physics has nothing to do with rendering. Whilst it’s used a little for hit-testing for a few buttons here and there, using thousands of polygons (where each can produce many collision shapes) is far from optimal. It really sounds like you should use a custom solution for hit-testing, maybe using a shader.

So what’s the problem are you have doing this then? You can set the outline to whateve you like with SetPath.

You do not mean “draw”, but “set up” in fact, right?

In Sprite Editor, you can generate custom physics shape. You can also set precision there.
When you then add Polygon Collider 2D to your sprite, it will take this shape.
If you change physics shape in Sprite Editor, the old shape remains on object in scene - you have to reset Polygon Collider 2D component to take new shape effect. See image below…

Regarding your second question (lot of sprites & automate process): I believe, that with some editor script it should be possible. I did not do this, but I batch added secondary texture to my sprites (here is blog post about it: http://sbcgames.io/batch-setting-secondary-textures-in-unity-sprite-editor/) You can take code from post as base and change part, when I play with secondary texture to do what you need.

Thank you for your answers!

I have to apologize, I think I did not explain the problem correctly.

In the picture you can see that three objects are quite close to each other. When you try to click the bear, you click the brush because of the bounding box.

I am looking for a way to make only the graphics clickable and not the transparency of the graphics.

I would like to use the polygon Collider 2D for this. Is that the wrong way? Do you have another suggestion?

Thanks!

The part you’re not explaining still is whether you mean selecting these in the editor (which isn’t related to 2D) or if you’re asking how to do that in your game; I suspect in your game.

Physics won’t ever give you pixel-perfect selection nor does it understand depth as it isn’t designed for that; it’s not a picking system. For pixel-perfect selection you’d need to use a shader of some kind.

There are various posts around on this subject that you can easily find and I suspect some Asset Store solution too.

Here’s my first hit on Google which seems like it has useful info: Pixel-Perfect selection using shaders | by Philipp Christoph | Medium

A little further down I see a GitHub repo but I’ve not looked at it: GitHub - fnuecke/Picky: Pixel-perfect collider-free object picking for Unity.

1 Like

Yes its for a game and yes I need a pixel-perfect selection for the assets ingame.

I will try a shader-based solution and let you guys know.

Thank you very much!

1 Like

Do you have a suggestion for what I can look for in the Asset Store? Key points like “pixel-perfect sellection” don’t produce any results. I am afraid that I do not know the correct technical terms.

Actually I took a look on the store and couldn’t find anything either which does surprise me.

I know that this answer is posted too late, but an user on Reddit posted a Sprite Collier script that makes colliders via your sprite Renderer, the unique thing that you should do is set the flag write/read on true for each sprite

https://www.reddit.com/r/Unity3D/comments/qzsgfa/i_made_a_script_makes_collider_from_sprites/?utm_medium=android_app&utm_source=share

If Unity allows to generate custom physics outline or custom rendering outline such that they are pixel perfect, then you can archive this quiet easily. By pixel perfect meaning that the outline traces non alpha 0 pixels around perfectly. Which would be a tradeoff between memory and a bit of performance on CPU vs less overdraw and pixel perfect physics selection.

Yes and that user wasn’t aware that this feature already exists in Unity too. It’s what happens when you add a PolygonCollider2D to a GameObject with a SpriteRenderer or go into the Sprite Editor and it also doesn’t require that you keep the texture in memory (read/write).

It’s quite amazing that someone could implement all that without knowing this!

If I have 2d pixel effect asset, which uses sprite animations. So at 2d sprite image, all effect’s shape change drew there. Then can I make collider effect based on this? For inflict damage to enemy who touch this 2d pixel effect (effect drew part only)