指定要在 Physics.Raycast 中使用的层。
GameObject 最多可以使用 Editor 支持的 32 个 LayerMask。这些 Layers
中的前 8 个由 Unity 指定;以下 24 个可由用户控制。
位掩码表示 32 层,并将它们定义为 true
或 false
。每个位掩码描述是否使用 Layer
。例如,位 5 可以设置为 1 (true
)。这将允许使用内置的 Water
设置。Edit > Settings > Tags and Layers
选项会显示 32 个位掩码的使用情况。每个 Layer
都会对应显示一个字符串设置。例如,Built-in Layer 0
设置为 Default
;Built-in Layer 1
设置为 TransparentFX
。新命名的 Layer
将添加到位掩码层 8 之上。所选的 GameObject
将在 Inspector 右上方显示选择的 Layer
。以下示例将 User Layer 13
设置为“Wall”。这使得指定的 GameObject
被视为建筑物的一部分。
在下面的脚本示例中,Physics.Raycast 将射线发送到世界中。Camera.main 可围绕 y 轴旋转并发出射线。三个 GameObject 代表可以被发射出的射线击中的墙壁。每个 GameObject 都会将 GameObject.label 设置为 "Wall" layerMask。
using UnityEngine;
// Fire a gun at 3 walls in the scene. // // The Raycast will be aimed in the range of -45 to 45 degrees. If the Ray hits any of the // walls true will be returned . The three walls all have a Wall Layer attached. The left // and right keys, and the space key, are all used to aim and fire. // // Quad floor based at (0, -0.5, 0), rotated in x by 90 degrees, scale (8, 8, 8). // ZCube wall at (0, 0.5, 6), scale (3, 2, 0.5). // LCube wall at (-3, 0, 3), scale (0.5, 1, 4). // RCube wall at (3, 1.5, 3), scale (1, 4, 4).
public class ExampleScript : MonoBehaviour { private float cameraRotation;
void Start() { Camera.main.transform.position = new Vector3(0, 0.5f, 0); cameraRotation = 0.0f; }
// Rotate the camera based on what the user wants to look at. // Avoid rotating more than +/-45 degrees. void Update() { if (Input.GetKey("left")) { cameraRotation -= 1f; if (cameraRotation < -45.0f) { cameraRotation = -45.0f; } }
if (Input.GetKey("right")) { cameraRotation += 1f; if (cameraRotation > 45.0f) { cameraRotation = 45.0f; } }
// Rotate the camera Camera.main.transform.localEulerAngles = new Vector3(0.0f, cameraRotation, 0.0f); }
void FixedUpdate() { Transform transform = Camera.main.transform;
if (Input.GetKeyUp("space")) { // Check for a Wall. LayerMask mask = LayerMask.GetMask("Wall");
// Check if a Wall is hit. if (Physics.Raycast(transform.position, transform.forward, 20.0f, mask)) { Debug.Log("Fired and hit a wall"); } } } }
**注意:**LayerMask 是位掩码。使用 LayerMask.GetMask 和 LayerMask.LayerToName 可生成位掩码。
value | 将层遮罩值转换为整数值。 |
GetMask | 给定在 Tags and Layers manager 中由 Builtin 或 User Layer 定义的一组层名称,返回所有这些层名称的等效层遮罩。 |
LayerToName | 给定层编号,返回在 Tags and Layers manager 中的 Builtin 或 User Layer 中定义的层的名称。 |
NameToLayer | 给定层名称,返回在 Tags and Layers manager 中由 Builtin 或 User Layer 定义的层索引。 |
LayerMask | 将整数隐式转换为 LayerMask。 |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.