Legacy Documentation: Version 2017.1 (Go to current version)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

EditorGUILayout.EnumMaskField

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

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

Parameters

label Prefix label for this field.
enumValue Enum to use for the flags.
options An optional list of layout options that specify extra layouting properties. Any values passed in here will override settings defined by the style.
See Also: GUILayout.Width, GUILayout.Height, GUILayout.MinWidth, GUILayout.MaxWidth, GUILayout.MinHeight, GUILayout.MaxHeight, GUILayout.ExpandWidth, GUILayout.ExpandHeight.

Returns

Enum The value modified by the user.

Description

Make a field for enum based masks.


Simple window that shows the enum mask field.

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

#pragma strict
public class EditorEnumExample extends EditorWindow {
	public enum Example {
		Option_One = 1,
		//bits: 0000 0001
		Option_Two = 2,
		//bits: 0000 0010
		Option_Three = 4,
		//bits: 0000 0100
	}
	var staticFlagMask: Example = 0;
	@MenuItem("Examples/Mask Field Usage")
	static function Init() {
		// Get existing open window or if none, make a new one:
		var window: EditorEnumExample = EditorEnumExampleEditorWindow.GetWindow(EditorEnumExample);
		window.Show();
	}
	function OnGUI() {
		staticFlagMask = ExampleEditorGUILayout.EnumMaskField("Static Flags", staticFlagMask);
	}
}
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); } }

Internally, Unity stores the enum as an int, with each value as a bitmask. Selecting "Nothing" clears all bits, resulting in an integer value of 0; selecting "Everything" will set all bits, producing an integer value of -1.

To determine whether a particular enum value has been set, you can either treat the enum as an int and perform a bitwise OR to test, or you can unset the "Everything" value by iterating through the enum values and reconstructing the enum accordingly. An example of how to do this is shown below:

#pragma strict
public class EditorEnumExample extends EditorWindow {
	public enum Example {
		Option_One = 1,
		//bits: 0000 0001
		Option_Two = 2,
		//bits: 0000 0010
		Option_Three = 4,
		//bits: 0000 0100
	}
	var staticFlagMask: Example = 0;
	@MenuItem("Examples/Mask Field Usage")
	static function Init() {
		// Get existing open window or if none, make a new one:
		var window: EditorEnumExample = EditorEnumExampleEditorWindow.GetWindow(EditorEnumExample);
		window.Show();
	}
	function OnGUI() {
		staticFlagMask = ExampleEditorGUILayout.EnumMaskField("Static Flags", staticFlagMask);
		// If "Everything" is set, force Unity to unset the extra bits by iterating through them
		if (intstaticFlagMask < 0) {
			var bits: int = 0;
			for (var enumValue: var in System.Enum.GetValues(Example)) {
				var checkBit: int = intstaticFlagMask & intenumValue;
				if (checkBit != 0)
					bits |= intenumValue;
			}
			staticFlagMask = Examplebits;
		}
	}
}
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 & (int)enumValue; if (checkBit != 0) bits |= (int)enumValue; }

staticFlagMask = (Example)bits; } } }

Did you find this page useful? Please give it a rating: