Object Drawers and EnumFlagsField

Gbcf

New member
Hi,

I'm trying to have a bitmask work similar to how LayerMask's work in the Unity Editor. I have code that works for PropertyAttribute and PropertyDrawer and works on the UnityEditor, but not when I use it through Behavior Designer. I looked into ObjectDrawers, but their OnGUI function parameters are different and I'm not sure how to get it to work without having access to SerializedProperty.

I am wondering if there is another solution to getting a bitmask editor field similar to LayerMasks, if we can use PropertyAttributes in Behavior Designer, and is it possible to do what I want in ObjectDrawers or if anyone has done it before?

Thanks!
 

Justin

Administrator
Staff member
You can get a SerializedProperty from a SerializedObject:

Code:
var myObject = new SerializedObject(unityObject);
However, because the tasks are regular System.objects this won't work for them. If you haven't seen it this page gives an example of adding an attribute using the object drawer system (the Range Attribute):

 

Gbcf

New member
I got it to work. I forgot to switch the attribute CustomPropertyDrawer to CustomObjectDrawer. Here's the code for those that want to do something similar:

C#:
[CustomObjectDrawer(typeof(EnumFlagAttribute))]
public class EnumFlagDrawer : ObjectDrawer
{
    public override void OnGUI(GUIContent label)
    {
        value = EditorGUILayout.EnumFlagsField(label, value as Enum);
    }
}
C#:
public class EnumFlagAttribute : ObjectDrawerAttribute
{
    public string enumName;

    public EnumFlagAttribute() { }

    public EnumFlagAttribute(string name)
    {
        enumName = name;
    }
}
Then just use [EnumFlag] above your enum in the Task.

C#:
[EnumFlag]
public MyEnum enumVariable;
 
Top