如图,如果我们定义了一个LayerMask,命名为lm(可以选择多个不同的layer),我们要判断本身gameobject的layer是否在我们选择的layermask中,就需要按位与(&)了,
public class NewBehaviourScript:MonoBehaviour{
public LayerMask lm;
private void Update(){
if(Input.GetMouseButtonDown(0)){
Debug.Log(lm.value + ":" + gameObject.layer + ":" + (lm.value & gameObject.layer));
if((lm.value & (int)Mathf.Pow(2,gameObject.layer)) == (int)Mathf.Pow(2,gameObject.layer)){
Debug.Log("在层中");
}else{
Debug.Log("不在层中");
}
}
}
}