Represents a Collider2D that is configured by assigning PhysicsShape2D geometry to it via a PhysicsShapeGroup2D.
Unlike all other Collider2D which are defined indirectly by controlling geometric parameters such as the size of a Box or the radius of a Circle, here the CustomCollider2D is defined entirely by adding, removing and modifying PhysicsShape2D. This results in a fully customized Collider2D containing an unlimited number of low-level PhysicsShape2D which can represent any type of shape or shapes, or emulate any other existing Collider2D such as the CircleCollider2D, BoxCollider2D, CapsuleCollider2D, EdgeCollider2D, CompositeCollider2D or TilemapCollider2D.
Alongside the customized geometry, there is full support for all existing Collider2D functionality such as triggers, physics materials, queries etc.
When assigning PhysicsShape2D to the CustomCollider2D, you can do so either during Edit mode or Play mode.
When modifying the CustomCollider2D during Edit mode, all assigned PhysicsShape2D and associated vertices will be saved with the Unity Scene. When the Unity Scene is loaded again, the CustomCollider2D will maintain its configuration. In this way, it acts like any other Collider2D that you make changes to during Edit mode. Using this ability, Edit mode authoring scripts can be used to create custom geometry.
When modifing the CustomCollider2D during Play mode, all assigned PhysicsShape2D and associated vertices will be lost when exiting Play mode. This acts like any other Collider2D.
customShapeCount | The total number of custom PhysicsShape2D assigned to the Collider. (Read Only) |
customVertexCount | The total number of vertices used by the Collider. (Read Only) |
ClearCustomShapes | Deletes all the shapes and associated vertices for those shapes from the Collider. |
GetCustomShapes | Gets all the physics shapes and vertices in the Collider and places them in the specified PhysicsShapeGroup2D. |
SetCustomShape | Sets a single shape and all associated shape vertices from the specified physicsShapeGroup into the Collider. |
SetCustomShapes | Sets all the shapes and vertices in the Collider to those represented by the specified PhysicsShapeGroup2D. |
enabled | 启用的 Behaviour 可更新,禁用的 Behaviour 不可更新。 |
isActiveAndEnabled | Reports whether a GameObject and its associated Behaviour is active and enabled. |
attachedRigidbody | 附加到 Collider2D 的 Rigidbody2D。 |
bounciness | 获取碰撞体使用的弹力。 |
bounds | 碰撞体的世界空间包围区域。 |
callbackLayers | The Layers that this Collider2D will report collision or trigger callbacks for during a contact with another Collider2D. |
composite | 获取可附加到该碰撞体的 CompositeCollider2D。 |
contactCaptureLayers | The layers of other Collider2D involved in contacts with this Collider2D that will be captured. |
density | 碰撞体的密度,用于计算其质量(如果启用自动质量)。 |
errorState | The error state that indicates the state of the physics shapes the 2D Collider tried to create. (Read Only) |
excludeLayers | The additional Layers that this Collider2D should exclude when deciding if a contact with another Collider2D should happen or not. |
forceReceiveLayers | The Layers that this Collider2D can receive forces from during a Collision contact with another Collider2D. |
forceSendLayers | The Layers that this Collider2D is allowed to send forces to during a Collision contact with another Collider2D. |
friction | 获取碰撞体使用的摩擦力。 |
includeLayers | The additional Layers that this Collider2D should include when deciding if a contact with another Collider2D should happen or not. |
isTrigger | 是否将碰撞体配置成了触发器? |
layerOverridePriority | A decision priority assigned to this Collider2D used when there is a conflicting decision on whether a contact between itself and another Collision2D should happen or not. |
offset | 碰撞体几何形状的局部偏移。 |
shapeCount | The number of active PhysicsShape2D the Collider2D is currently using. |
sharedMaterial | 应用于碰撞体的 PhysicsMaterial2D。 |
usedByComposite | 设置 CompositeCollider2D 是否将使用碰撞体。 |
usedByEffector | 附加的特效器是否使用该碰撞体。 |
gameObject | 此组件附加到的游戏对象。始终将组件附加到游戏对象。 |
tag | 此游戏对象的标签。 |
transform | 附加到此 GameObject 的 Transform。 |
hideFlags | 该对象应该隐藏、随场景一起保存还是由用户修改? |
name | 对象的名称。 |
Cast | 将碰撞体形状投射到从该碰撞体位置开始的场景中,而忽略该碰撞体本身。 |
ClosestPoint | 返回此碰撞体周边上最接近指定 position 的点。 |
CreateMesh | 创建与 Collider2D 几何体定义的区域相同的平面 Mesh。 |
Distance | 计算该碰撞体与另一个碰撞体的最小间距。 |
GetContacts | 获取此碰撞体的所有接触点。 |
GetShapeHash | 根据 Collider2D 的几何体生成简单哈希值。 |
GetShapes | Gets all the PhysicsShape2D used by the Collider2D. |
IsTouching | 检查该碰撞体是否正在接触 /collider/。 |
IsTouchingLayers | 检查该碰撞体是否正在接触指定 layerMask 上的任何碰撞体。 |
OverlapCollider | 获取与该碰撞体重叠的所有碰撞体的列表。 |
OverlapPoint | 检查碰撞体是否与空间中的某个点重叠。 |
Raycast | 将光线投射到从碰撞体位置开始的场景中,并忽略碰撞体本身。 |
BroadcastMessage | 调用此游戏对象或其任何子项中的每个 MonoBehaviour 上名为 methodName 的方法。 |
CompareTag | Checks the GameObject's tag against the defined tag. |
GetComponent | Gets a reference to a component of type T on the same GameObject as the component specified. |
GetComponentInChildren | Gets a reference to a component of type T on the same GameObject as the component specified, or any child of the GameObject. |
GetComponentInParent | Gets a reference to a component of type T on the same GameObject as the component specified, or any parent of the GameObject. |
GetComponents | Gets references to all components of type T on the same GameObject as the component specified. |
GetComponentsInChildren | Gets references to all components of type T on the same GameObject as the component specified, and any child of the GameObject. |
GetComponentsInParent | Gets references to all components of type T on the same GameObject as the component specified, and any parent of the GameObject. |
SendMessage | 调用此游戏对象中的每个 MonoBehaviour 上名为 methodName 的方法。 |
SendMessageUpwards | 调用此游戏对象中的每个 MonoBehaviour 上或此行为的每个父级上名为 methodName 的方法。 |
TryGetComponent | 获取指定类型的组件(如果存在)。 |
GetInstanceID | Gets the instance ID of the object. |
ToString | 返回对象的名称。 |
Destroy | 移除 GameObject、组件或资源。 |
DestroyImmediate | 立即销毁对象 /obj/。强烈建议您改用 Destroy。 |
DontDestroyOnLoad | 在加载新的 Scene 时,请勿销毁 Object。 |
FindAnyObjectByType | Retrieves any active loaded object of Type type. |
FindFirstObjectByType | Retrieves the first active loaded object of Type type. |
FindObjectOfType | 返回第一个类型为 type 的已加载的激活对象。 |
FindObjectsByType | Retrieves a list of all loaded objects of Type type. |
FindObjectsOfType | Gets a list of all loaded objects of Type type. |
Instantiate | 克隆 original 对象并返回克隆对象。 |
bool | 该对象是否存在? |
operator != | 比较两个对象是否引用不同的对象。 |
operator == | 比较两个对象引用,判断它们是否引用同一个对象。 |
OnCollisionEnter2D | 当传入碰撞体与该对象的碰撞体接触时发送(仅限 2D 物理)。 |
OnCollisionExit2D | 当另一个对象上的碰撞体停止接触该对象的碰撞体时发送(仅限 2D 物理)。 |
OnCollisionStay2D | 在另一个对象上的碰撞体正在接触该对象的碰撞体时发送每个帧(仅限 2D 物理)。 |
OnTriggerEnter2D | 当另一个对象进入附加到该对象的触发碰撞体时发送(仅限 2D 物理)。 |
OnTriggerExit2D | 当另一个对象离开附加到该对象的触发碰撞体时发送(仅限 2D 物理)。 |
OnTriggerStay2D | 在另一个对象位于附加到该对象的触发碰撞体之内时发送每个帧(仅限 2D 物理)。 |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.