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.
CloseFor 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.
ClosephysicsShapeGroup | The PhysicsShapeGroup2D to use as the source of shapes and vertices. |
Sets all the shapes and vertices in the Collider to those represented by the specified PhysicsShapeGroup2D.
All existing shapes and vertices are replaced by the shapes and vertices contained in the specified PhysicsShapeGroup2D.
Where possible, the Collider will reduce the amount of work required to assign the specifed shapes and vertices. The Collider checks each PhysicsShape2D being assigned at each shape index and if the Collider already contains the same physics shape then no work is done. If the physics shape has the same shape type and vertex count then the existing physics engine shape will only have its radius and vertices updated. If the physics shape has a different shape type or vertex count then the physics engine shape will be recreated.
Any existing contacts for this Collider will be recalculated during the next simulation step.
using UnityEngine;
public class Example : MonoBehaviour { void Start() { // Fetch the custom collider. var customCollider = GetComponent<CustomCollider2D>();
// Create a shape group. var shapeGroup = new PhysicsShapeGroup2D();
// Add a Circle to the shape group. shapeGroup.AddCircle ( center: new Vector2(-1f, 0f), radius: 0.5f );
// Add a box to the shape group. shapeGroup.AddBox ( center: new Vector2(1f, 0f), size: new Vector2(1f, 1f) );
// Assign the shapes and vertices. customCollider.SetCustomShapes(shapeGroup); } }
shapes | The array containing all the shapes used to assign to the Collider. |
vertices | The array containing all the vertices used to assign to the Collider. |
Sets all the shapes and vertices in the Collider to those represented by the specified arrays.
All existing shapes and vertices are replaced by the shapes and vertices contained in the specified arrays.
Where possible, the Collider will reduce the amount of work required to assign the specified shapes and vertices. The Collider checks each PhysicsShape2D being assigned at each shape index and if the Collider already contains the same physics shape then no work is done. If the physics shape has the same shape type and vertex count then the existing physics engine shape will only have its radius and vertices updated. If the physics shape has a different shape type or vertex count then the physics engine shape will be recreated.
Any existing contacts for this Collider will be recalculated during the next simulation step.
using Unity.Collections; using UnityEngine; using UnityEngine.Assertions;
public class Example : MonoBehaviour { void Start() { // Create a native shapes array and populate it with a Circle and Capsule. var shapes = new NativeArray<PhysicsShape2D>(2, Allocator.Temp) { [0] = new PhysicsShape2D() { shapeType = PhysicsShapeType2D.Circle, radius = 1f, vertexStartIndex = 0, vertexCount = 1 }, [1] = new PhysicsShape2D() { shapeType = PhysicsShapeType2D.Capsule, radius = 0.5f, vertexStartIndex = 1, vertexCount = 2 } };
// Create a native vertices array and populate it with the vertices for the shapes. var vertices = new NativeArray<Vector2>(3, Allocator.Temp) { [0] = new Vector2(2f, 3f), [1] = Vector2.down, [2] = Vector2.up };
// Fetch the custom collider. var customCollider = GetComponent<CustomCollider2D>();
// Set custom collider to the shapes and vertices. customCollider.SetCustomShapes(shapes, vertices);
// Dispose of the native arrays. vertices.Dispose(); shapes.Dispose();
// Validate the collider. Assert.AreEqual(2, customCollider.customShapeCount); Assert.AreEqual(3, customCollider.customVertexCount);
// Get all the custom shapes. var shapeGroup = new PhysicsShapeGroup2D(); customCollider.GetCustomShapes(shapeGroup);
// Validate the first shape and vertex. var shape0 = shapeGroup.GetShape(0); Assert.AreEqual(PhysicsShapeType2D.Circle, shape0.shapeType); Assert.AreApproximatelyEqual(1f, shape0.radius); Assert.AreEqual(0, shape0.vertexStartIndex); Assert.AreEqual(1, shape0.vertexCount); Assert.AreEqual(new Vector2(2f, 3f), shapeGroup.GetShapeVertex(shapeIndex: 0, vertexIndex: 0));
// Validate the second shape and vertices. var shape1 = shapeGroup.GetShape(1); Assert.AreEqual(PhysicsShapeType2D.Capsule, shape1.shapeType); Assert.AreApproximatelyEqual(0.5f, shape1.radius); Assert.AreEqual(1, shape1.vertexStartIndex); Assert.AreEqual(2, shape1.vertexCount); Assert.AreEqual(Vector2.down, shapeGroup.GetShapeVertex(shapeIndex: 1, vertexIndex: 0)); Assert.AreEqual(Vector2.up, shapeGroup.GetShapeVertex(shapeIndex: 1, vertexIndex: 1)); } }
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.