Skip to content

Instantly share code, notes, and snippets.

@emanuelmaues
Forked from ChemiKhazi/EnumFlagAttribute.cs
Last active May 19, 2019 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emanuelmaues/1c252a6b80c331d44149 to your computer and use it in GitHub Desktop.
Save emanuelmaues/1c252a6b80c331d44149 to your computer and use it in GitHub Desktop.
This fork includes a way to format the property name in the same style as Unity. Also, you can use EnumFlag in private and protected enums now.
using UnityEngine;
public class EnumFlagAttribute : PropertyAttribute
{
public EnumFlagAttribute() {}
}
using System;
using System.Text.RegularExpressions;
using System.Reflection;
using UnityEditor;
using UnityEngine;
[CustomPropertyDrawer(typeof(EnumFlagAttribute))]
public class EnumFlagDrawer : PropertyDrawer {
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
Enum targetEnum = GetBaseProperty<Enum>(property);
string propName = FormatName(property.name);
EditorGUI.BeginProperty(position, label, property);
Enum enumNew = EditorGUI.EnumMaskField(position, propName, targetEnum);
property.intValue = (int) Convert.ChangeType(enumNew, targetEnum.GetType());
EditorGUI.EndProperty();
}
private static T GetBaseProperty<T>(SerializedProperty prop)
{
// Separate the steps it takes to get to this property
string[] separatedPaths = prop.propertyPath.Split('.');
// Go down to the root of this serialized property
System.Object reflectionTarget = prop.serializedObject.targetObject as object;
// Walk down the path to get the target object
foreach (var path in separatedPaths)
{
FieldInfo fieldInfo = reflectionTarget.GetType().GetField(path, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
reflectionTarget = fieldInfo.GetValue(reflectionTarget);
}
return (T) reflectionTarget;
}
/// <summary>
/// This format the name in the same way Unity formats every property name
/// </summary>
private static string FormatName(string propertyName)
{
if (string.IsNullOrEmpty(propertyName))
{
return propertyName;
}
int index = propertyName.IndexOf("_");
if (index != -1 && (index != 1 || index != 2) && propertyName.Length > (index + 1))
{
propertyName = propertyName.Substring(index + 1);
}
string formattedPropertyName = char.ToUpper(propertyName[0]).ToString();
if (propertyName.Length > 1)
{
formattedPropertyName = string.Concat(formattedPropertyName, Regex.Replace(propertyName.Substring(1), "([a-z])([A-Z])", "$1 $2"));
}
return formattedPropertyName;
}
}
@thoroc
Copy link

thoroc commented May 19, 2019

You should be able to use ObjectNames.NicifyVariableName(name); instead of your custom method FormatName(name);. ;)

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