center | 圆形形状的中心点。这类似于 Collider2D.offset。 |
radius | 以 center 为中心,在周围定义半径的圆的半径。这与 CircleCollider2D.radius 相同。 |
int 返回形状在 PhysicsShapeGroup2D 中被添加到的形状索引。检索形状时,此索引用作主要引用。
向形状组添加一个圆形(PhysicsShapeType2D.Circle)。
圆形由一个顶点(center
)和一个围绕该顶点延伸的 radius
组成。这是在所有用例中使用时效率最高、内存占用最少的基元。
注意:圆形成一个闭合形状,并且有一个内部,因此物理查询等可以检测到其内部。
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 Circle. var shapeIndex = shapeGroup.AddCircle ( center: new Vector2(-2f, 3f), radius: 1f );
// Fetch the actual shape created. var physicsShape = shapeGroup.GetShape(shapeIndex);
// Validate what we created. Assert.AreEqual(PhysicsShapeType2D.Circle, physicsShape.shapeType); Assert.AreApproximatelyEqual(1f, physicsShape.radius); Assert.AreEqual(1, physicsShape.vertexCount); }
// Get a physics shape from a Collider. void GetShapeFromCollider() { // Fetch the collider. var collider = GetComponent<CircleCollider2D>();
// 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.radius = 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.Circle, physicsShape.shapeType); Assert.AreApproximatelyEqual(1f, physicsShape.radius); Assert.AreEqual(1, physicsShape.vertexCount); } }