Version: 2022.2
public static Enum EnumPopup (Enum selected, params GUILayoutOption[] options);
public static Enum EnumPopup (Enum selected, GUIStyle style, params GUILayoutOption[] options);
public static Enum EnumPopup (string label, Enum selected, params GUILayoutOption[] options);
public static Enum EnumPopup (string label, Enum selected, GUIStyle style, params GUILayoutOption[] options);
public static Enum EnumPopup (GUIContent label, Enum selected, params GUILayoutOption[] options);
public static Enum EnumPopup (GUIContent label, Enum selected, GUIStyle style, params GUILayoutOption[] options);
public static Enum EnumPopup (GUIContent label, Enum selected, Func<Enum,bool> checkEnabled, bool includeObsolete, params GUILayoutOption[] options);
public static Enum EnumPopup (GUIContent label, Enum selected, Func<Enum,bool> checkEnabled, bool includeObsolete, GUIStyle style, params GUILayoutOption[] options);

参数

label (可选)字段前的标签。
selected 该字段显示的枚举选项。
style 可选 GUIStyle
options 一个可选的布局选项列表,用于指定额外的布局属性。此处传递的任何值都将覆盖 style 定义的设置。
另请参阅:GUILayout.WidthGUILayout.HeightGUILayout.MinWidthGUILayout.MaxWidthGUILayout.MinHeightGUILayout.MaxHeightGUILayout.ExpandWidthGUILayout.ExpandHeight
includeObsolete 设置为 true 将使 ObsoleteAttribute 附带枚举值。设置为 false 将使 ObsoleteAttribute 不附带枚举值。
checkEnabled 为每个显示的枚举值调用的方法。如果可以选择该选项,则指定的方法应返回 true,否则返回 false。

返回

Enum 用户已选择的枚举选项。

描述

创建一个枚举弹出选择字段。

将当前所选的枚举值作为参数,并返回用户选择的枚举值。


根据用户所选选项创建原始项。

using UnityEditor;
using UnityEngine;
using System.Collections;

// Creates an instance of a primitive depending on the option selected by the user.

public enum OPTIONS { CUBE = 0, SPHERE = 1, PLANE = 2 }

public class EditorGUILayoutEnumPopup : EditorWindow { public OPTIONS op; [MenuItem("Examples/Editor GUILayout Enum Popup usage")] static void Init() { UnityEditor.EditorWindow window = GetWindow(typeof(EditorGUILayoutEnumPopup)); window.Show(); }

void OnGUI() { op = (OPTIONS)EditorGUILayout.EnumPopup("Primitive to create:", op); if (GUILayout.Button("Create")) InstantiatePrimitive(op); }

void InstantiatePrimitive(OPTIONS op) { switch (op) { case OPTIONS.CUBE: GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); cube.transform.position = Vector3.zero; break; case OPTIONS.SPHERE: GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); sphere.transform.position = Vector3.zero; break; case OPTIONS.PLANE: GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane); plane.transform.position = Vector3.zero; break; default: Debug.LogError("Unrecognized Option"); break; } } }