Unity是用 int32来表示32个Layer层,int32用二进制来表示一共有32位。
0000 0000 0000 0000 0000 0000 0000 0000
31 0
看几个例子:
开启layer 2
LayerMask mask = 1<<2;
其中 <<左边的 1表示有[开启碰撞],0表示没有该layer[忽略碰撞] 。右边的2表示左移2位即是 layer2层的位置。
开启layer 0和layer 2
LayerMask mask = 1 << 0 | 1 << 2;
开启Layer0 并关闭 Layer2
LayerMask mask = 1 << 0 | 0 << 2
开启Layer Default
LaserMask mask=1 << LayserMask.NameToLayer(“Default”);
下面是一个脚本例子,把它绑定在Camera上
using UnityEngine; using System.Collections; [RequireComponent(typeof(Camera))] public class LayerMaskTest : MonoBehaviour { //打开Npc层,变1 或 运算符 private void Show() { //camera.cullingMask |= 1 << LayerMask.NameToLayer("Npc"); camera.cullingMask = (int) 1<<LayerMask.NameToLayer("Npc");//写法二 } // 关闭Npc层,变0 按位与 & 按位取反 ~ private void Hide() { camera.cullingMask &= ~(1 << LayerMask.NameToLayer("Npc")); } // 开关Npc层,按位异或 ^ private void Toggle() { camera.cullingMask ^= 1 << LayerMask.NameToLayer("Npc"); } void OnGUI() { if (GUILayout.Button("Show")) Show(); if (GUILayout.Button("Hide")) Hide(); if (GUILayout.Button("Toggle")) Toggle(); } }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
Unity的碰撞检测API:
static bool Raycast(Vector3 origin, Vector3 direction, float distance = Mathf.Infinity, int layerMask = DefaultRaycastLayers);
我在项目中进行物理碰撞互斥:
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
float bounds = 2; LayerMask mask = 1 << (int)GameLayerDef.Npc; RaycastHit2D raycastHit = Physics2D.CircleCast(transform.position, bounds, Vector2.right, Mathf.Infinity, mask.value);
注:部分内容参考自: 【风宇冲】二进制:四 Unity
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
Unity LayerMask 与位运算