Builtin Layer是系统自定义的,User Layer是用户可以添加的,后面的阿拉伯数字是Layer的层索引。后面可输入字符是定义的Layer名称。
1:可以通过LayerMask.NameToLayer(string LayerName)获得改层的索引号
2:如上图所示的1、2、3、4索引号直接是不能用的。需要使用1<<索引号来进行转换
详情可以产考官方Layer文档
使用案例一:相机中的CullingMask
相机中的CullingMask就是通过控制Layer来选择相机能否渲染具体的层,以下代码给出代码控制Camera的CullingMask:
// Turn on the bit using an OR operation:
private void Show(string someLayer)
{
camera.cullingMask |= 1 << LayerMask.NameToLayer(someLayer);
}
// Turn off the bit using an AND operation with the complement of the shifted int:
private void Hide(string someLayer)
{
camera.cullingMask &= ~(1 << LayerMask.NameToLayer(someLayer));
}
// Toggle the bit using a XOR operation:
private void Toggle(string someLayer)
{
camera.cullingMask ^= 1 << LayerMask.NameToLayer(someLayer);
}
使用案例二:射线检测中的LayerMask
射线检测简介:(简介部分引用自:http://www.cnblogs.com/xuling/archive/2013/03/04/2943154.html )
射线:射线是3D世界中一个点向一个方向发射的一条无终点的线,在发射轨迹中与其他物体发生碰撞时,它将停止发射 。
用途:射线应用范围比较广, 多用于碰撞检测(如:子弹飞行是否击中目标)、角色移动等 等。
相关API:
1、Ray Camera.main.ScreenPointToRay(Vector3 pos) 返回一条射线Ray从摄像机到屏幕指定一个点
2、Ray Camera.main.ViewportPointToRay(Vector3 pos) 返回一条射线Ray从摄像机到视口(视口之外无效)指定一个点
3、Ray 射线类
4、RaycastHit 光线投射碰撞信息
5、bool Physics.Raycast(Vector3 origin, Vector3 direction, float distance, int layerMask)
当光线投射与任何碰撞器交叉时为真,否则为假。
bool Physics.Raycast(Ray ray, Vector3 direction, RaycastHit out hit, float distance, int layerMask)
在场景中投下可与所有碰撞器碰撞的一条光线,并返回碰撞的细节信息()。
bool Physics.Raycast(Ray ray, float distance, int layerMask)
当光线投射与任何碰撞器交叉时为真,否则为假。
bool Physics.Raycast(Vector3 origin, Vector3 direction, RaycastHit out hit,float distance, int layerMask)
当光线投射与任何碰撞器交叉时为真,否则为假。
注意:如果从一个球型体的内部到外部用光线投射,返回为假。
参数理解:
origin : 在世界坐标中射线的起始点
direction: 射线的方向
distance: 射线的长度
hit: 使用c#中out关键字传入一个空的碰撞信息类,然后碰撞后赋值。可以得到碰撞物体的transform,rigidbody,point等信息。
layerMask: 只选定Layermask层内的碰撞器,其它层内碰撞器忽略。 选择性的碰撞
6、RaycastHit[] RaycastAll(Ray ray, float distance, int layerMask)
投射一条光线并返回所有碰撞,也就是投射光线并返回一个RaycastHit[]结构体。
补充:LayerMask是一个类型,整形的类型,可以参照官方文档。如我们可以定义:LayerMask mask = 1 << 8;指的就是图示中的索引为8的图层
RaycastHit hit;
LayerMask mask = 1 << 8;
void testRay(){
if(Physics.Raycast(transform.position,Vector3.right,out hit,100, mask.value)){
Debug.DrawLine(transform.position,hit.point,Color.red,1);
}
}
上面的例子是從當前腳本所绑定的物體做標點向右發射長度100的射線,射線碰撞層級为8,其他層級忽略。
为了測試,我們在碰撞到物體後畫一條紅色線條,表示射線,停留一秒钟。
上面代码中mask.value还可以使用1<<8来代替
I just ran a quick check -- rayCasting through a custom layer (one you create) works just fine. Made four cubes lined up on z, added custom layers 8 and 9 (skip and skip2, but the names don't matter,) lined them up on z and added a raycast script to the start cube:
Cubes: start-> wall1 wall2 target
Layer: default skip(8) skip2(9) default
if(Input.GetKey("space")) {
RaycastHit H;
// goes through wall1 (layer 8,) hits wall2:
if(Physics.Raycast(transform.position, transform.forward, out H, 100, ~1<<8))
Debug.Log(H.transform.name);
// goes through both walls (layer 8 and 9) and hits target:
int skip1n2 = ~((1<<8)|(1<<9));
if(Physics.Raycast(transform.position, transform.forward, out H, 100, skip1n2))
Debug.Log(H.transform.name);
// The previous hits ignoreRaycast. This skips 8,9 and ignoreRC(layer 2):
int skip = ~((1<<8)|(1<<9)|(1<<2));
if(Physics.Raycast(transform.position, transform.forward, out H, 100, skip1n2))
}
http://unity3d.com/support/documentation/Components/Layers.html has the official example.
public static int OnlyIncluding(params int[] layers)
{
return MakeMask(layers);
}
public static int EverythingBut(params int[] layers)
{
return ~MakeMask(layers);
}
static int MakeMask(params int[] layers)
{
int mask = 0;
foreach (int item in layers)
{
mask |= 1 << item;
}
return mask;
}