PhysicsMask
A 64-bit mask, effectively 64 flags. The default enumerator will iterate all the bits that are set (1).
阅读时间3 分钟最后更新于 21 天前
Definition
- Type: Struct
- Namespace: Unity.U2D.Physics
- Assembly: UnityEngine.PhysicsCore2DModule
- Implements: IEnumerable<int>, IEnumerable, IEquatable<PhysicsMask>
public struct PhysicsMask : IEnumerable<int>, IEnumerable, IEquatable<PhysicsMask>
Remarks
Use a together with PhysicsLayers.GetLayerMask and PhysicsShape.ContactFilter to control which objects collide with each other in a script. If the field or property that stores a represents a raw bitmask rather than a set of named layers, apply PhysicsMask.ShowAsPhysicsMaskAttribute so Unity displays it as bit values in the Inspector window.
PhysicsMaskPhysicsMaskAdditional Resources: PhysicsLayers.GetLayerMask, PhysicsShape.ContactFilter
Examples
// Get the physics mask for the "Car" layer, then get another mask for the// layers the object should collide with.using UnityEngine;using Unity.U2D.Physics;public class PhysicsMaskExample : MonoBehaviour{ void Start() { PhysicsMask objectLayer = PhysicsLayers.GetLayerMask("Car"); PhysicsMask contactLayer = PhysicsLayers.GetLayerMask("Walls"); PhysicsShape.ContactFilter myContactFilter = new PhysicsShape.ContactFilter { categories = objectLayer, contacts = contactLayer }; }}
Fields
Value | Description |
|---|---|
| bitMask | A 64-bit mask, effectively 64 flags. |
Static Fields
Value | Description |
|---|---|
| All | All 64 bits set (1) in the PhysicsMask. |
| None | No bits set in the PhysicsMask, effectively zero. |
| One | Bit #0 set (1) in the PhysicsMask. The remaining bits are reset (0). |
Constructors
Constructor | Description |
|---|---|
| PhysicsMask(System.Int32[]) | Create a PhysicsMask by specifying multiple bits to set (1). |
| PhysicsMask(UnityEngine.LayerMask) | Create a PhysicsMask from a LayerMask. A LayerMask is only 32-bits wide so the PhysicsMask will have the upper 32-bits set to zero. |
Properties
Property | Description |
|---|---|
| resetBits | Gets an enumerable group of bits that are currently reset (0). The bits are returned in ascending bit-index order. This uses PhysicsMask.ResetBitIterator. |
| setBits | Gets an enumerable group of bits that are currently set (1). The bits are returned in ascending bit-index order. This uses PhysicsMask.SetBitIterator. |
Methods
Method | Description |
|---|---|
| AreAnyBitsSet | Checks if any of the provided PhysicsMask set bits are also set in this PhysicsMask. |
| AreBitsSet | Checks if all the provided PhysicsMask set bits are also set in this PhysicsMask. |
| IsBitSet | Is the specified bit set. |
| ResetBit | Reset (0) the specified bit. |
| SetBit | Set (1) the specified bit. |
| ToLayerMask | Convert the lower 32-bits of the 64-bit mask to the 32-bit LayerMask. A LayerMask is only 32-bits wide so the upper 32-bits of the PhysicsMask will be ignored. |
Operators
Operator | Description |
|---|---|
| operator & | Bitwise AND operator for PhysicsMask. |
| operator | | Bitwise OR operator for PhysicsMask. |
| operator ^ | Bitwise XOR operator for PhysicsMask. |
| operator << | Bitwise LEFT-SHIFT operator for PhysicsMask. |
| operator ~ | Bitwise COMPLEMENT operator for PhysicsMask. |
| operator >> | Bitwise RIGHT-SHIFT operator for PhysicsMask. |
Classes
Class | Description |
|---|---|
| PhysicsMask.ShowAsPhysicsMaskAttribute | When applied to a field/property of type PhysicsMask, the field/property drawer will not be display it as PhysicsLayers. Instead, the field/property will be displayed as bit numbers only i.e. a raw 64-bit mask allowing each bit to be (de)selected. This is only used when physics layers are active (see PhysicsCoreSettings2D.usePhysicsLayers). |
Structs
Struct | Description |
|---|---|
| PhysicsMask.ResetBitIterator | An iterator that will iterate only the bits that are reset (0) in a PhysicsMask |
| PhysicsMask.SetBitIterator | An iterator that will iterate only the bits that are set (1) in a PhysicsMask |