Is PolygonCollider2D a legit way to check for mouse clicks in irregular shaped sprites?

Hi there,

I’m new to Unity and always wanted to create a strategy game inspired by Europa Universalis or Hearts of Iron.

So my first goal was to be able to click on a province to select them. For now I illustrate that by changing the color of the province.
I’m using a PolygonCollider2D for each province and adjust the collider to the shape of the sprite.

It then looks like this:

And like this for all the 3 provinces(for now):

So selection is working fine, the province graphics are just white so that I can simply set the color when the mouse click happens to be on the province.
It’s also useful that by using the colliders I can simply zoom in and out and the selection still works fine.
Don’t bother about the province intersections, there are going to be border graphics overlapping them once I’m coming forward.

My concerns are:

  1. Performance: There are going be several more, over hundred, additional provinces, each of them having one of these colliders. If the mouse is clicked somewhere is every PolygonCollider2D checking in detail for itself or are they first checking for rectangle bounds? There will also be other graphics placed upon the provinces which may need a PolygonCollider2D.

  2. Is this the, so to say, legit way of accomplishing my goal in Unity apart from performance issues? I read something about raycasting and intersecting a ray with a plane(the sprites) but as soon as I put something else on the province I’d also need to check for the color of the pixel as Unity is returning a bunch of intersected pixels(if I unterstood it correctly). With the current approach I just need to adjust the Z coordinate of the graphic on the province by a minimal value so that it’s really placed over the province and it is selected instead of the province.

I hope I’ve been clear enough and if I haven’t I’ll try to fix it :slight_smile:

Thanks for every help!

  1. Regarding the first issue you are safe from performance impact caused by the colliders. Colliders are processed by the physics engine and since you only use them in an “inert” state (they do not actually collide, they do not move etc…) the physics engine will not be burdened. Also, a single raycast for touch/mouse detection does not qualify as ‘heavy lifting’ for unity.
  2. Regarding the second issue, I would propose “province detection” of some different kind than pixel-type. Maybe create some kind of Script that will hold the relevant data [name, player who controlls it etc etc…] and check the values upon raycasthit. Furthermore, you can separate the province terrain from province objects by placing them in separate layers. You can get the distance to all hit objects and you can process the logic only for the closest one.

Hope this helps you in your journey :slight_smile: