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

CustomCollider2D.ClearCustomShapes

建议更改

成功!

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

关闭

提交失败

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

关闭

取消

声明

public void ClearCustomShapes();

描述

从碰撞器中删除所有形状及其关联的顶点。

此碰撞器的任何现有接触将在下一个模拟步骤中重新计算。

using UnityEngine;
using UnityEngine.Assertions;

public class Example : MonoBehaviour { void Start() { // Fetch the custom collider. var customCollider = GetComponent<CustomCollider2D>();

// Create a shape group. var shapeGroup = new PhysicsShapeGroup2D();

// Add 5 Circles to the shape group. shapeGroup.AddCircle(center: new Vector2(3f, 0f), radius: 0.5f); shapeGroup.AddCircle(center: new Vector2(4f, 0f), radius: 0.5f); shapeGroup.AddCircle(center: new Vector2(5f, 0f), radius: 0.5f); shapeGroup.AddCircle(center: new Vector2(6f, 0f), radius: 0.5f); shapeGroup.AddCircle(center: new Vector2(7f, 0f), radius: 0.5f);

// Assign our shapes. customCollider.SetCustomShapes(shapeGroup);

// Validate the contents of the custom collider. Assert.AreEqual(5, customCollider.customShapeCount);

// Clear all the remaining shapes. customCollider.ClearCustomShapes();

// Validate the contents of the custom collider. Assert.AreEqual(0, customCollider.customShapeCount); } }

声明

public void ClearCustomShapes(int shapeIndex, int shapeCount);

参数

shapeIndex 存储在碰撞器中的形状的索引。
shapeCount 从指定索引开始要删除的形状数量。

描述

删除从 shapeIndex 开始的由 shapeCount 定义的特定数量的形状,以及这些形状使用的所有关联顶点。

此碰撞器的任何现有接触将在下一个模拟步骤中重新计算。

using UnityEngine;
using UnityEngine.Assertions;

public class Example : MonoBehaviour { void Start() { // Fetch the custom collider. var customCollider = GetComponent<CustomCollider2D>();

// Create a shape group. var shapeGroup = new PhysicsShapeGroup2D();

// Add 5 Circles to the shape group. shapeGroup.AddCircle(center: new Vector2(3f, 0f), radius: 0.5f); shapeGroup.AddCircle(center: new Vector2(4f, 0f), radius: 0.5f); shapeGroup.AddCircle(center: new Vector2(5f, 0f), radius: 0.5f); shapeGroup.AddCircle(center: new Vector2(6f, 0f), radius: 0.5f); shapeGroup.AddCircle(center: new Vector2(7f, 0f), radius: 0.5f);

// Assign our shapes. customCollider.SetCustomShapes(shapeGroup);

// Validate the contents of the custom collider. Assert.AreEqual(5, customCollider.customShapeCount);

// Clear the first 2 shapes. customCollider.ClearCustomShapes(shapeIndex: 0, shapeCount: 2);

// Validate the contents of the custom collider. Assert.AreEqual(3, customCollider.customShapeCount);

// Clear the remaining 3 shapes. customCollider.ClearCustomShapes(shapeIndex: 0, shapeCount: 3);

// Validate the contents of the custom collider. Assert.AreEqual(0, customCollider.customShapeCount); } }