개발메모/Unity

[Unity] 레이어가 레이어 마스크에 포함되어있는지 확인 하는 법

RideAbove 2023. 7. 13. 16:08
반응형
if((mask & (1 << layer)) != 0){
	//포함됨
}
else{
	//포함 안됨
}

LayerMask와 Layer는 모두 integer로 저장됨.

Unity는 32개까지의 레이어를 지원하고, integer는 32 bits.

(mask & (1 << layer)) != 0

위 식의 의미는, 특정 Layer가 LayerMask에 포함되는지 확인하려고 할때

1을 그 Layer의 위치로 shift연산을 하여 LayerMask의 해당 위치에 1이 존재하는지 체크

 

 

 

 

How Do LayerMasks Work?

LayerMasks are stored as an integer, but represent any combination of Layers in your game. Layers are also integers... so how does that work?

Unity only supports up to 32 layers. An integer is 32 bits. A LayerMask simply allocates one bit for each layer, where 0 means that layer is not included, and 1 means that it is.

 

참조: https://steemit.com/unity3d/@hardlydifficult/unity3d-layermask-bool-includes-int-layer-extension-method

 

반응형