版本: Unity 6 (6000.0)
语言English
  • C#

PhysicsShapeGroup2D.GetShapeData

建议更改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法接受所有提交,但我们会阅读用户提出的每个建议更改,并在适用时进行更新。

关闭

提交失败

由于某些原因,您的建议更改无法提交。请<a>稍后再试</a>。感谢您抽出时间帮助我们提高 Unity 文档的质量。

关闭

取消

声明

public void GetShapeData(List<PhysicsShape2D> shapes, List<Vector2> vertices);

参数

shapes 一个列表,其中将填充 PhysicsShapeGroup2D 中所有形状的副本。
vertices 一个列表,其中将填充 PhysicsShapeGroup2D 中所有顶点的副本。

描述

获取 PhysicsShapeGroup2D 中的形状和顶点的副本。

注意:因为这是一个副本,所以之后更改指定的 shapesvertices 列表不会更改 PhysicsShapeGroup2D

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions;

public class Example : MonoBehaviour { void Start() { // Create a shape group. var shapeGroup = new PhysicsShapeGroup2D();

// Add a Circle to the shape group. var circleShapeIndex = shapeGroup.AddCircle ( center: new Vector2(-2f, 3f), radius: 1f );

// Add a Capsule to the shape group. var capsuleShapeIndex = shapeGroup.AddCapsule ( vertex0: Vector2.down, vertex1: Vector2.up, radius: 0.5f );

// Validate the contents. Assert.AreEqual(PhysicsShapeType2D.Circle, shapeGroup.GetShape(circleShapeIndex).shapeType); Assert.AreEqual(PhysicsShapeType2D.Capsule, shapeGroup.GetShape(capsuleShapeIndex).shapeType);

// Fetch the shapes and vertices from the shape group. var shapes = new List<PhysicsShape2D>(); var vertices = new List<Vector2>(); shapeGroup.GetShapeData(shapes, vertices);

// Fetch the Circle shape and validate shape and vertex. var circleShape = shapes[circleShapeIndex]; Assert.AreEqual(PhysicsShapeType2D.Circle, circleShape.shapeType); Assert.AreApproximatelyEqual(1f, circleShape.radius); Assert.AreEqual(new Vector2(-2f, 3f), vertices[circleShape.vertexStartIndex]);

// Fetch the Capsule shape and validate shape and vertices (2). var capsuleShape = shapes[capsuleShapeIndex]; Assert.AreEqual(PhysicsShapeType2D.Capsule, capsuleShape.shapeType); Assert.AreApproximatelyEqual(0.5f, capsuleShape.radius); Assert.AreEqual(Vector2.down, vertices[capsuleShape.vertexStartIndex]); Assert.AreEqual(Vector2.up, vertices[capsuleShape.vertexStartIndex + 1]); } }

声明

public void GetShapeData(NativeArray<PhysicsShape2D> shapes, NativeArray<Vector2> vertices);

参数

shapes 一个 NativeArray,其中将填充 PhysicsShapeGroup2D 中所有形状的副本。
vertices 一个 NativeArray,其中将填充 PhysicsShapeGroup2D 中所有顶点的副本。

描述

获取 PhysicsShapeGroup2D 中的形状和顶点的副本。

注意:因为这是一个副本,所以之后更改指定的 shapesvertices 数组不会更改 PhysicsShapeGroup2D

using Unity.Collections;
using UnityEngine;
using UnityEngine.Assertions;

public class Example : MonoBehaviour { void Start() { // Create a shape group. var shapeGroup = new PhysicsShapeGroup2D();

// Add a Circle to the shape group. var circleShapeIndex = shapeGroup.AddCircle ( center: new Vector2(-2f, 3f), radius: 1f );

// Add a Capsule to the shape group. var capsuleShapeIndex = shapeGroup.AddCapsule ( vertex0: Vector2.down, vertex1: Vector2.up, radius: 0.5f );

// Validate the contents. Assert.AreEqual(PhysicsShapeType2D.Circle, shapeGroup.GetShape(circleShapeIndex).shapeType); Assert.AreEqual(PhysicsShapeType2D.Capsule, shapeGroup.GetShape(capsuleShapeIndex).shapeType);

// Fetch the shapes and vertices from the shape group. var shapes = new NativeArray<PhysicsShape2D>(shapeGroup.shapeCount, Allocator.Temp); var vertices = new NativeArray<Vector2>(shapeGroup.vertexCount, Allocator.Temp); shapeGroup.GetShapeData(shapes, vertices);

// Fetch the Circle shape and validate shape and vertex. var circleShape = shapes[circleShapeIndex]; Assert.AreEqual(PhysicsShapeType2D.Circle, circleShape.shapeType); Assert.AreApproximatelyEqual(1f, circleShape.radius); Assert.AreEqual(new Vector2(-2f, 3f), vertices[circleShape.vertexStartIndex]);

// Fetch the Capsule shape and validate shape and vertices (2). var capsuleShape = shapes[capsuleShapeIndex]; Assert.AreEqual(PhysicsShapeType2D.Capsule, capsuleShape.shapeType); Assert.AreApproximatelyEqual(0.5f, capsuleShape.radius); Assert.AreEqual(Vector2.down, vertices[capsuleShape.vertexStartIndex]); Assert.AreEqual(Vector2.up, vertices[capsuleShape.vertexStartIndex + 1]);

// Dispose of the native arrays. vertices.Dispose(); shapes.Dispose(); } }