Version: 2022.2
public static int LayerField (Rect position, int layer, GUIStyle style= EditorStyles.popup);
public static int LayerField (Rect position, string label, int layer, GUIStyle style= EditorStyles.popup);
public static int LayerField (Rect position, GUIContent label, int layer, GUIStyle style= EditorStyles.popup);

参数

position 屏幕上用于字段的矩形。
label (可选)字段前的标签。
layer 字段中显示的层。
style 可选 GUIStyle

返回

int 用户选择的层。

描述

创建一个层选择字段。


编辑器窗口中的 Layer 字段。

using UnityEngine;
using UnityEditor;

// Change the Tag and/or the layer of the selected GameObjects.

class EditorGUITagLayerField : EditorWindow { string selectedTag = ""; int selectedLayer = 0;

[MenuItem("Examples/Tag - Layer for Selection")] static void Init() { var window = GetWindow<EditorGUITagLayerField>(); window.position = new Rect(0, 0, 350, 70); window.Show(); }

void OnGUI() { selectedTag = EditorGUI.TagField( new Rect(3, 3, position.width / 2 - 6, 20), "New Tag:", selectedTag);

selectedLayer = EditorGUI.LayerField( new Rect(position.width / 2 + 3, 3, position.width / 2 - 6, 20), "New Layer:", selectedLayer);

if (Selection.activeGameObject) { if (GUI.Button(new Rect(3, 25, 90, 17), "Change Tags")) { foreach (GameObject go in Selection.gameObjects) { go.tag = selectedTag; } }

if (GUI.Button(new Rect(position.width - 96, 25, 90, 17), "Change Layers")) { foreach (GameObject go in Selection.gameObjects) { go.layer = selectedLayer; } } } }

void OnInspectorUpdate() { Repaint(); } }