vertices | 表示凸多边形连续的边缘集合的顶点列表,其中每个顶点连接到后续顶点以形成每条边缘。最后一个顶点隐式连接到第一个顶点。允许的最大列表长度由 Physics2D.MaxPolygonShapeVertices 定义。 |
int 返回多边形形状在 PhysicsShapeGroup2D 中被添加到其中的形状索引。在检索形状时,此索引用作主参考。
向形状组添加多边形形状(PhysicsShapeType2D.Polygon)。
多边形形状由所有指定的 vertices
定义的多个边缘(线段)组成。
注意:一个多边形形成一个闭合形状,并且有内部,因此其内部可通过物理查询等检测到。
using System; using System.Collections.Generic; 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 Polygon. var shapeIndex = shapeGroup.AddPolygon ( vertices: new List<Vector2> { new Vector2(-1f, -1f), new Vector2(1f, -1f), new Vector2(1f, 1f), new Vector2(-1f, 1f) } );
// 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); }
// Get a physics shape from a Collider. void GetShapeFromCollider() { // Fetch the collider. var collider = GetComponent<PolygonCollider2D>();
// 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.SetPath(0, new List<Vector2>() { new Vector2(-1f, -1f), new Vector2(1f, -1f), new Vector2(1f, 1f), 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); } }