useBodyPosition | Should the mesh be transformed by the position of the attached Rigidbody2D? |
useBodyRotation | Should the mesh be transformed by the rotation of the attached Rigidbody2D? |
Mesh The planar Mesh created that matches the collider geometry or NULL if no Mesh could be created.
Creates a planar Mesh that is identical to the area defined by the Collider2D geometry.
In simple terms, this method will create a Mesh that exactly represents the area defined by the Collider2D. The created Mesh can then be used for any purpose including but not limited to navigation meshes for 2D navigation. The Collider2D does not own the created Mesh and you are responsible for its lifetime therefore not deleting the Mesh will result in a memory leak. The Collider2D is not altered in any way during this call. The Collider2D must have valid shapes present for a Mesh to be created otherwise NULL will be returned indicating that no Mesh was created.
All Collider2D geometry lives in the space of the Rigidbody2D it is attached to. You can select whether the Mesh vertices are transformed by the Rigidbody2D position and rotation using useBodyPosition
and useBodyRotation
respectively. If the Collider2D is not attached to a Rigidbody2D then the geometry is permanently in world-space and both useBodyPosition
and useBodyRotation
should always be set to false.
What follows is more detail on how the Collider2D shapes are considered when creating a Mesh. Whilst there are many types of Collider2D, they only produce collision geometry which comprise of the following primitive shape types: Circle, Capsule, Polygon and Edge. These primitive types are split into two groups known as closed convex primitives and open primitives. Primitives of type Circle, Capsule and Polygon are closed convex primitives which define a closed convex area bounded by the primitive. The Edge primitive is an open type that defines geometry with no internal area.
A CircleCollider2D will produce a single circle primitive. A CapsuleCollider2D will produce a single capsule primitive. A BoxCollider2D will produce a single polygon primitive. A PolygonCollider2D can produce multiple polygon primitives to convert a potentially concave area into multiple convex polygons. An EdgeCollider2D can produce multiple edge primitives. A TilemapCollider2D can produce multiple polygon primitives (per-tile). A CompositeCollider2D can produce either multiple polygon primitives (in Polygon mode) or produce multiple edge primitives (in Outline mode).
When creating a Mesh to represent Collider2D geometry, all closed convex primitives (circle, capsule and polygon) produce the respective filled area defined by those primitives. In the case where there are multiple polygons, the total area of all the polygons is created. Note that when BoxCollider2D.edgeRadius that is greater than zero is used (to produce radial edges), the Mesh is created to also represent that geometry.
When creating a Mesh to represent Collider2D geometry, all open primitives (edges) have special handling. When the start vertex of the first edge is coincident with the end vertex of the last edge (to define a pseudo-closed area) then a mesh is created to represent that closed area even though 2D physics doesn't itself treat it as such. If the vertex are not coincident then a Mesh will only be created if EdgeCollider2D.edgeRadius is greater than zero as this produces edges with an area.
An EdgeCollider2D when used with coincident start/end vertex will create a Mesh that is a convex hull of all edge vertices. Any concave vertex (producing a concave configuration) will be considered as being on the convex hull resulting in a convex planar Mesh. If a convex Mesh is required then multiple polygon primitives should be used i.e. a PolygonCollider2D or a CompositeCollider2D in polygon mode. A CompositeCollider2D in outline mode produces edges with coincident start/end vertex so will always create a closed shape Mesh which also supports CompositeCollider2D.edgeRadius.
See Also: GetShapeHash and NavMeshBuildSource.sourceObject.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.