Version: 2021.3
public int layer ;

描述

The layer the GameObject is in.

You can use Layers for selective rendering from cameras or to ignore Raycasts. Unity generates 32 layers, labelled with integers from 0 to 31 and reserves layers 0 to 5 for its own systems. You can use layers 5 and above. To add or view a layer, click the Layout button in the top-right of the Editor window.

// Put the game object in the ignore raycast layer (2)

using UnityEngine;

[ExecuteInEditMode] public class ExampleClass : MonoBehaviour { void Awake() { //gameObject.layer uses only integers, but we can turn a layer name into a layer integer using LayerMask.NameToLayer() int LayerIgnoreRaycast = LayerMask.NameToLayer("Ignore Raycast"); gameObject.layer = LayerIgnoreRaycast; Debug.Log("Current layer: " + gameObject.layer); } }