Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to make a SerializableProperty with a MaskField?

Discussion in 'Scripting' started by thrmotta, Nov 17, 2014.

  1. thrmotta

    thrmotta

    Joined:
    May 27, 2014
    Posts:
    32
    Hi everyone!

    So, usually, when I need to do a serializable field, Ill just go and write the following down:
    Code (CSharp):
    1. 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 ?
     
  2. RSG

    RSG

    Joined:
    Feb 20, 2013
    Posts:
    93
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,513
    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/spacepupp...pacepuppyUnityFramework/PropertyAttributes.cs

    And looks like this:
    Code (csharp):
    1.  
    2.     [System.AttributeUsage(System.AttributeTargets.Field, AllowMultiple = false)]
    3.     public class GenericMaskAttribute : PropertyAttribute
    4.     {
    5.  
    6.         private string[] _maskNames;
    7.  
    8.         public GenericMaskAttribute(params string[] names)
    9.         {
    10.             _maskNames = names;
    11.         }
    12.  
    13.         public string[] MaskNames { get { return _maskNames; } }
    14.  
    15.     }
    16.  
    Pretty straight forward there.

    As well as the PropertyDrawer itself:
    https://code.google.com/p/spacepupp...ditor/Inspectors/GenericMaskPropertyDrawer.cs

    And here is a simple example of its use:

    Code (csharp):
    1.  
    2.     [GenericMask("Blargh", "Poop", "Harp")]
    3.     public int TestMask;
    4.  
    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).
     
  4. thrmotta

    thrmotta

    Joined:
    May 27, 2014
    Posts:
    32
    @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?
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,513
    No it doesn't.

    How does the mask field change at runtime?
     
  6. thrmotta

    thrmotta

    Joined:
    May 27, 2014
    Posts:
    32
    This is what I was doing before multiple edition:

    Code (CSharp):
    1. listNames = new List<string> ();
    2.          
    3. for(int i = 0; i < childCount; i++)
    4. {
    5.         Transform child =  link.GetChild(i);
    6.         LinkVisual linkVisual = child.gameObject.GetComponent<LinkVisual>();
    7.         if(linkVisual != null)
    8.         {
    9.                listLinkVisual.Add(linkVisual);
    10.                listNames.Add (linkVisual.name);
    11.         }
    12. }
    13.  
    14. 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
     
  7. thrmotta

    thrmotta

    Joined:
    May 27, 2014
    Posts:
    32
    Anyone?