How to make a SerializableProperty with a MaskField?

Hi everyone!

So, usually, when I need to do a serializable field, Ill just go and write the following down:

EditorGUILayout.PropertyField ( serializableProperty,  new GUIContent ( name, tooltip) ) );

But when working with a MaskField, this isnt quite working, as it’ll only display an integer field once I dont know how to fill in the string values for the Mask. How can this be done ?

have you tried using EditorGUI.MaskField

You have to write a custom editor, or a property drawer.

Here is a custom property drawer I made up to do what you want on the fly.

The Attribute is in here:
https://code.google.com/p/spacepuppy-unity-framework/source/browse/trunk/SpacepuppyUnityFramework/PropertyAttributes.cs

And looks like this:

    [System.AttributeUsage(System.AttributeTargets.Field, AllowMultiple = false)]
    public class GenericMaskAttribute : PropertyAttribute
    {

        private string[] _maskNames;

        public GenericMaskAttribute(params string[] names)
        {
            _maskNames = names;
        }

        public string[] MaskNames { get { return _maskNames; } }

    }

Pretty straight forward there.

As well as the PropertyDrawer itself:
https://code.google.com/p/spacepuppy-unity-framework/source/browse/trunk/SpacepuppyUnityFrameworkEditor/Inspectors/GenericMaskPropertyDrawer.cs

And here is a simple example of its use:

    [GenericMask("Blargh", "Poop", "Harp")]
    public int TestMask;

That will have the editor display a mask allowing for selection of entries Blargh, Poop, and Harp. Which result in values 1,2,4 respectively (or some summation there from, as it is a mask).

@lordofduct , thank you! This worked quite nicely at my first try, but then I couldnt get it to work with dynamic strings. I have a case where the size of the MaskField changes during runtime. Is it possible to use your solution in this situation?

No it doesn’t.

How does the mask field change at runtime?

This is what I was doing before multiple edition:

listNames = new List<string> ();
         
for(int i = 0; i < childCount; i++)
{
        Transform child =  link.GetChild(i);
        LinkVisual linkVisual = child.gameObject.GetComponent<LinkVisual>();
        if(linkVisual != null)
        {
               listLinkVisual.Add(linkVisual);
               listNames.Add (linkVisual.name);
        }
}

linkCollision.inspectorState.mask = EditorGUILayout.MaskField ("Fit", linkCollision.inspectorState.mask, listNames.ToArray());

So the strings passed to MaskField could change in size depending on the number of components

Anyone?