关于Unity3d中的LayerMask使用 .
来源:互联网 发布:2017网络十大英语热词 编辑:程序博客网 时间:2024/05/21 15:42
原文地址
最近看别人写的Demo,里面这个Linecast字段的LayerMask参数看了半天也没看懂
void Update() { // The player is grounded if a linecast to the groundcheck position hits anything on the ground layer. grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground")); // If the jump button is pressed and the player is grounded then the player should jump. if(Input.GetButtonDown("Jump") && grounded) jump = true; }
后来去官网看,半天也没个例子,还是google吧,在网上看到各路大神的解释,终于明白了,LayerMask实际上是一个位码操作,在Unity3d中Layers一共有32层,这个是不能增加或者减少的:
摘自官网:
Layers are used throughout Unity as a way to create groups of objects that share particular characteristics (see this page for further details). Layers are primarily used to restrict operations such as raycasting or rendering so that they are only applied to the groups of objects that are relevant. In the manager, the first eight layers are defaults used by Unity and are not editable. However, layers from 8 to 31 can be given custom names just by typing in the appropriate text box. Note that unlike tags, the number of layers cannot be increased.
所以那个LayerMask实际上是用Int32的32个位来表示每个层级,当这个位为1时表示使用这个层,为0时表示不用这个层。
回到上面的例子:
1 << LayerMask.NameToLayer("Ground")
这一句实际上表示射线查询只在Ground所在这个层级查找。
再来看看NameToLayer是干嘛的。
官网解释:
Description
Given a layer name, returns the layer index as defined by either a Builtin or a User Layer in the Tag Manager.
是返回的该名字所定义的层的层索引,注意是从0开始。
- 关于Unity3d中的LayerMask使用
- 关于Unity3d中的LayerMask使用 .
- 关于Unity3d中的LayerMask使用
- 关于Unity3d中的LayerMask使用
- Unity3D LayerMask 的使用
- Unity3D教程:如何使用layerMask参数
- [Unity3D]射线碰撞检测+LayerMask的使用
- [Unity3D]射线碰撞检测+LayerMask的使用
- [Unity3D]射线碰撞检测+LayerMask的使用
- [Unity3D]射线碰撞检测+LayerMask的使用
- LayerMask使用
- Unity3D射线检测如何使用Layers和LayerMask
- Unity3D Raycast, LayerMask的设置
- LayerMask的使用
- LayerMask 的使用
- 关于Unity3D中的UnitySendMessage方法的使用!!!
- 关于Unity3D中的UnitySendMessage方法的使用!!!
- Unity3D中Layers和LayerMask解析
- eclipse运行报java.lang.OutOfMemoryError: PermGen space解决方法
- Android笔记之View未显示前获得控件宽高
- 在一个应用中,如何针对不同的外部客户系统,使用不同的数字证书?
- SequenceInputStream合并流
- java学习笔记代码块和继承
- 关于Unity3d中的LayerMask使用 .
- how to check if license is downloaded fully
- BasicExcel另存为中文文件名出错的解决
- Android BluetoothProfile之AVRCP
- Openstack 中cinder backup三种backend的对比
- MongoDB 性能分析
- xcode openssl
- NSUserDefaults 存储自定义对象
- 用CSS来控制字符长度和显示长度