Version: 5.6
public static Enum EnumMaskField (GUIContent label, Enum enumValue, GUIStyle style, params GUILayoutOption[] options);
public static Enum EnumMaskField (string label, Enum enumValue, GUIStyle style, params GUILayoutOption[] options);
public static Enum EnumMaskField (GUIContent label, Enum enumValue, params GUILayoutOption[] options);
public static Enum EnumMaskField (string label, Enum enumValue, params GUILayoutOption[] options);
public static Enum EnumMaskField (Enum enumValue, GUIStyle style, params GUILayoutOption[] options);
public static Enum EnumMaskField (Enum enumValue, params GUILayoutOption[] options);

参数

label 此字段的前缀标签。
enumValue 用于标志的枚举。
options 一个可选的布局选项列表,用于指定额外的布局属性。此处传递的任何值都将覆盖 style 定义的设置。
另请参阅:GUILayout.WidthGUILayout.HeightGUILayout.MinWidthGUILayout.MaxWidthGUILayout.MinHeightGUILayout.MaxHeightGUILayout.ExpandWidthGUILayout.ExpandHeight

返回

Enum 用户修改的值。

描述

针对基于枚举的掩码创建一个字段。


Simple window that shows the enum mask field.

Here is an example of how to implement an EnumMaskField, giving three options:

using UnityEngine;
using UnityEditor;

public class EditorEnumExample : EditorWindow { public enum Example { Option_One = 1, //bits: 0000 0001 Option_Two = 2, //bits: 0000 0010 Option_Three = 4 //bits: 0000 0100 }

Example staticFlagMask = 0;

[MenuItem("Examples/Mask Field Usage")] static void Init() { // Get existing open window or if none, make a new one: EditorEnumExample window = (EditorEnumExample)EditorWindow.GetWindow(typeof(EditorEnumExample)); window.Show(); }

void OnGUI() { staticFlagMask = (Example)EditorGUILayout.EnumMaskField("Static Flags", staticFlagMask); } }

在内部,Unity 会将枚举存储为 int,其中一个值为一个位掩码。选择"Nothing"将清除所有位,并生成一个整数值 0,选择"Everything"将设置所有位,并生成一个整数值 -1。

要确定是否已设置特定枚举类型,您可以将此枚举视为一个 int 并执行位运算 OR 进行测试,也可以通过迭代枚举值并相应地重新构建此枚举来取消"Everything"值的设置。以下示例介绍了如何执行此操作:

using UnityEngine;
using UnityEditor;

public class EditorEnumExample : EditorWindow { public enum Example { Option_One = 1, //bits: 0000 0001 Option_Two = 2, //bits: 0000 0010 Option_Three = 4 //bits: 0000 0100 }

Example staticFlagMask = 0;

[MenuItem("Examples/Mask Field Usage")] static void Init() { // Get existing open window or if none, make a new one: EditorEnumExample window = (EditorEnumExample)EditorWindow.GetWindow(typeof(EditorEnumExample)); window.Show(); }

void OnGUI() { staticFlagMask = (Example)EditorGUILayout.EnumMaskField("Static Flags", staticFlagMask);

// If "Everything" is set, force Unity to unset the extra bits by iterating through them if ((int)staticFlagMask < 0) { int bits = 0; foreach (var enumValue in System.Enum.GetValues(typeof(Example))) { int checkBit = (int)staticFlagMask &amp; (int)enumValue; if (checkBit != 0) bits |= (int)enumValue; }

staticFlagMask = (Example)bits; } } }