center | 盒状形状的中心点。这类似于 Collider2D.offset。 |
size | 盒子的尺寸。这与 BoxCollider2D.size 相同。 |
angle | 盒子围绕 center 旋转的角度(以度为单位)。 |
edgeRadius | 围绕盒子边缘延伸的半径。这与 BoxCollider2D.edgeRadius 相同。 |
int 返回形状添加到 PhysicsShapeGroup2D 中的形状索引。此索引在检索形状时用作主要参考。
将盒状形状(PhysicsShapeType2D.Polygon)添加到形状组。
盒状形状是一种凸多边形,具有四个顶点和一个围绕四个边缘延伸的 edgeRadius
。添加盒状形状的功能仅为方便,并不代表实际的原始形状。
注意:盒子形成一个封闭的形状,并且具有内部,因此其内部可被物理查询等检测到。
using UnityEngine; using UnityEngine.Assertions;
public class Example : MonoBehaviour { void Start() { CreateShape(); GetShapeFromCollider(); }
// Create a custom shape. void CreateShape() { // Create a shape group. var shapeGroup = new PhysicsShapeGroup2D();
// Add a Box (polygon with four vertices). // NOTE: Both the angle and edgeRadius arguments are optional. var shapeIndex = shapeGroup.AddBox ( center: new Vector2(3f, 2f), size: new Vector2(1f, 1f), angle: 0f, edgeRadius: 0f );
// Fetch the actual shape created. var physicsShape = shapeGroup.GetShape(shapeIndex);
// Validate what we created. Assert.AreEqual(PhysicsShapeType2D.Polygon, physicsShape.shapeType); Assert.AreEqual(4, physicsShape.vertexCount); Assert.AreApproximatelyEqual(0f, physicsShape.radius); }
// Get a physics shape from a Collider. void GetShapeFromCollider() { // Fetch the collider. var collider = GetComponent<BoxCollider2D>();
// If the collider is not active and enabled then we'll get no shapes. // Let's just quit if not. if (!collider.isActiveAndEnabled) return;
// Configure the collider. collider.offset = new Vector2(3f, 2f); collider.size = new Vector2(1f, 1f);
// Create a shape group. var shapeGroup = new PhysicsShapeGroup2D();
// Get the collider shapes. collider.GetShapes(shapeGroup);
// Fetch the actual shape. var physicsShape = shapeGroup.GetShape(0);
// Validate what we retrieved. Assert.AreEqual(PhysicsShapeType2D.Polygon, physicsShape.shapeType); Assert.AreEqual(4, physicsShape.vertexCount); } }