Version: 2023.2
LanguageEnglish
  • C#

Collider2D.GetShapes

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Declaration

public int GetShapes(PhysicsShapeGroup2D physicsShapeGroup);

Parameters

physicsShapeGroup The PhysicsShapeGroup2D to store the retrieved PhysicsShape2D in.

Returns

int Returns the number of PhysicsShape2D retrieved from the Collider2D.

Description

Gets all the PhysicsShape2D used by the Collider2D.

All Collider2D represent a high-level definition of 2D geometry that is used to create efficient low-level primitive shapes used by the physics engine for collision detection. These low-level primitive shapes are what the 2D physics gizmos draw within the Unity Editor. This method provides access to a copy of these primitive shapes which can be useful in various use-cases such as debugging and gizmos or rendering Collider2D geometry at runtime.

Use this method to gain access to a copy of all the low-level primitive shapes that are produced by a Collider2D. If no such shapes exist on the Collider2D as indicated by Collider2D.shapeCount then the shape group will not be populated with shapes. If shapes do exist, the group is first emptied before being populated with shapes.

Each Collider2D produces either a single or multiple PhysicsShape2D of a single PhysicsShapeType2D as indicated below:

All Collider2D shape vertices are stored in the local space of the Rigidbody2D they are attached to i.e. their vertices are relative to the pose of the Rigidbody2D therefore when a Rigidbody2D translates or rotates, the vertices of attached Collider2D do not change. This is why changes to position or rotation should only ever happen via the Rigidbody2D as this is the most efficient method. When retrieving shapes, the shape vertices are in the local space of the Rigidbody2D. The current position and rotation of the Rigidbody2D is stored in PhysicsShapeGroup2D.localToWorldMatrix and can be used to transform the shape vertices to world space. If the Collider2D is not attached to any Rigidbody2D i.e. it is Static then the local space and world space are identical therefore the PhysicsShapeGroup2D.localToWorldMatrix is set to Matrix4x4.identity.

Note: It is recommened to reuse the PhysicsShapeGroup2D because then memory allocations only occur if the number of shapes or vertices retrieved are greater than the existing capacity of the PhysicsShapeGroup2D. Reusing this object therefore minimizes allocations to a bare minimum and eventually to none so no work is produced for the garbage collector.

See Also: Rigidbody2D.GetShapes.


Declaration

public int GetShapes(PhysicsShapeGroup2D physicsShapeGroup, int shapeIndex, int shapeCount = 1);

Parameters

physicsShapeGroup The PhysicsShapeGroup2D to store the retrieved PhysicsShape2D in.
shapeIndex The index of the first shape to retrieve. This should be in the range of 0 to Collider2D.shapeCount-1.
shapeCount The number of shapes to retrieve. The shapeIndex + shapeCount must be less than or equal to Collider2D.shapeCount.

Returns

int Returns the number of PhysicsShape2D retrieved from the Collider2D. In all cases this should be the same number as shapeCount .

Description

Gets the specified range of the PhysicsShape2D used by the Collider2D.

Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961