Skip to content

Instantly share code, notes, and snippets.

@FFouetil
Forked from ChemiKhazi/EnumFlagAttribute.cs
Last active February 10, 2021 02:16
Show Gist options
  • Save FFouetil/dd081256da0e3475d524d88b414076e3 to your computer and use it in GitHub Desktop.
Save FFouetil/dd081256da0e3475d524d88b414076e3 to your computer and use it in GitHub Desktop.
Unity3d property drawer for automatically making enums flags into mask fields in the inspector.
Unity3d property drawer for automatically making enums flags into mask fields in the inspector.
Usage: Put the .cs files somewhere in your project. When you have an enum field you want to turn into a mask field in the inspector, add the EnumFlag attribute over the field. Eg:
[EnumFlag]
MyCustomEnum thisEnum;
// This lets you give the field a custom name in the inspector.
[EnumFlag("Custom Inspector Name")]
MyCustomEnum anotherEnum;
My updates:
-Added modification check to better support multi-object editing
-Added support for the better Unity 2017.3 version of the flag property drawer: EditorGUI.EnumFlagsField()
-Added Nicifier formatting on field name for parameter-less attribute
-Removed "static T GetBaseProperty<T>(SerializedProperty prop)", and replaced it
with the cleaner "Enum targetEnum = (Enum)Enum.ToObject(fieldInfo.FieldType, property.intValue);" line from another fork
The EnumFlagsAtrribute.cs is the same as other forks
using UnityEngine;
public class EnumFlagAttribute : PropertyAttribute
{
public string enumName;
public EnumFlagAttribute() {}
public EnumFlagAttribute(string name)
{
enumName = name;
}
}
using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;
[CustomPropertyDrawer(typeof(EnumFlagAttribute))]
public class EnumFlagDrawer : PropertyDrawer {
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EnumFlagsAttribute flagSettings = (EnumFlagsAttribute)attribute;
Enum targetEnum = (Enum)Enum.ToObject(fieldInfo.FieldType, property.intValue);
string propName = flagSettings.enumName;
if (string.IsNullOrEmpty(propName))
propName = ObjectNames.NicifyVariableName(property.name);
EditorGUI.BeginChangeCheck();
EditorGUI.BeginProperty(position, label, property);
#if UNITY_2017_3_OR_NEWER
Enum enumNew = EditorGUI.EnumFlagsField(position, propName, targetEnum);
#else
Enum enumNew = EditorGUI.EnumMaskField(position, propName, targetEnum);
#endif
if (!property.hasMultipleDifferentValues || EditorGUI.EndChangeCheck())
property.intValue = (int)Convert.ChangeType(enumNew, targetEnum.GetType());
EditorGUI.EndProperty();
}
}
@rakkarage
Copy link

rakkarage commented Apr 30, 2018

thanks a lot
here is one that does not need attributes

using System;
using UnityEditor;
using UnityEngine;
namespace UnityEngine.Tilemaps
{
	[Flags]
	public enum TileOrientation
	{
		None = 0,
		FlipX = 1,
		FlipY = (1 << 1),
		Rot90 = (1 << 2),
	}
#if UNITY_EDITOR
	[CustomPropertyDrawer(typeof(TileOrientation))]
	public class TileOrientationDrawer : PropertyDrawer
	{
		public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
		{
			label = EditorGUI.BeginProperty(position, label, property);
			property.intValue = (int)(TileOrientation)EditorGUI.EnumFlagsField(position, label, (TileOrientation)property.intValue);
			EditorGUI.EndProperty();
		}
	}
#endif
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment