I’ve said this before but I think the single best feature in Tiled2Unity is the
PolygonCollider2D instances that are created from Tiled layers, where the colliders on each tile are merged into one big polygon. Concave edges? Holes? Not a problem.
However, some users would like to use the
PolygonCollider2D instances created by Tiled2Unity and feed them into another Unity script or plugin that performs pathfinding, or real-time lighting, or whatever. The problem is, these other plugins commonly only work with polygons that are convex.
And by the way, if that’s a plugin you bought with actual money from the Unity Asset Store then I think that’s weak sauce. Tell those guys to fix their tools!
But I digress … with the latest Tiled2Unity version you can now set an option to have only convex polygons exported.
Note: Convex polygon output can be also be set through the
--convex options for those of you using the command line.
This will increase the number of
PolygonCollider2D components in your Tiled prefabs but through the Hertel-Mehlhorn Algorithm I’ve made that number “reasonably” low. For most cases I’d be surprised if there was a noticeable change in performance.
Different Ways to Set Convex Polygon Output
There is some flexibility when it comes to enabling convex polygon exporting:
- Tiled2Unity Utility: Either use the
Convex Polygon Colliderscheckbox in the Win32 utility or use the
-c|--convexcommand line option. This will be the preferred setting for exporting polygons for all maps.
- Tiled Map: Add a property named
unity:convexto your Map Properties in Tiled set to
false. This will be the preferred setting for all the layers in your map and will override the setting from the Tiled2Unity utility.
- Tiled Layer: Add the
unity:convexproperty to your Layer Properties. This will be the final word on how the polygons in your layer will be exported, overriding the map and utility preferences.
Make sure to keep these precedence rules in mind. If you set
false in a Tiled layer, for instance, then this will always be obeyed, no matter how you “prefer” to export maps in the Tiled2Unity Utility.
As always (and especially with a new feature such as this) you can email me any bugs you come across and I’ll find the spare time to address them. Just be aware a full-time game development which slows me down at times. 🙂
Special thanks go to the Poly2Tri contributors. Their polygon triangulation library came in handy for this feature.