Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


PhysicsMask

A 64-bit mask, effectively 64 flags. The default enumerator will iterate all the bits that are set (1).
Read time 3 minutesLast updated 10 days ago

Definition

public struct PhysicsMask : IEnumerable<int>, IEnumerable, IEquatable<PhysicsMask>

Remarks

Use a
PhysicsMask
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
PhysicsMask
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.

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

bitMaskA 64-bit mask, effectively 64 flags.

Static Fields

Value

Description

AllAll 64 bits set (1) in the PhysicsMask.
NoneNo bits set in the PhysicsMask, effectively zero.
OneBit #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

resetBitsGets an enumerable group of bits that are currently reset (0). The bits are returned in ascending bit-index order. This uses PhysicsMask.ResetBitIterator.
setBitsGets 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

AreAnyBitsSetChecks if any of the provided PhysicsMask set bits are also set in this PhysicsMask.
AreBitsSetChecks if all the provided PhysicsMask set bits are also set in this PhysicsMask.
IsBitSetIs the specified bit set.
ResetBitReset (0) the specified bit.
SetBitSet (1) the specified bit.
ToLayerMaskConvert 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.ShowAsPhysicsMaskAttributeWhen 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 _usePhysicsLayers).

Structs

Struct

Description

PhysicsMask.ResetBitIteratorAn iterator that will iterate only the bits that are reset (0) in a PhysicsMask
PhysicsMask.SetBitIteratorAn iterator that will iterate only the bits that are set (1) in a PhysicsMask