Search Unity

CreateAssetMenuAttribute has no constructors

Discussion in 'Immediate Mode GUI (IMGUI)' started by Jeff-Rosenberg, Feb 25, 2016.

  1. Jeff-Rosenberg

    Jeff-Rosenberg

    Joined:
    Oct 23, 2012
    Posts:
    26
    I've also asked on Unity Answers, but after looking into it I think it's more a question for the Unity devs. I found the CreateAssetMenu attribute today, which adds Create/Asset menu options for ScritpableObjects, just like the age-old ScriptableObjectUtility from the Unify Wiki.

    To use the attribute you'd do something like:
    Code (CSharp):
    1. [CreateAssetMenu]
    2. public class ItemReference : ScriptableObject
    3. {
    4.     [SerializeField, Multiline]
    5.     private string description;
    6.     [SerializeField, Range(0, 64)]
    7.     private int stackSize;
    8. }


    The attribute is kind of useless though, despite being much easier to actually use, because the attribute has no constructor!
    • The asset always appears at the top of the Create/Asset menu in it's own separator, where Create/Asset/Folder should go, because the order property cannot be changed.
    • You cannot change the menuName property, so it always displays as the class name. This also means you cannot have a nested menu option like the Shader or Sprites submenus.
    • The class is sealed so you can't inherit from it to add this functionality yourself.
    Is all this intended? The docs have full explanations for each of the properties so you'd think the properties could be set somewhere.

    Edit: The only place I can find it referenced in the Unity dll files is in UnityEditor.ExtractCreateAssetMenuItems, which seems to be a method that builds the menu references for this attribute. It does actually reference the menuName, fileName, and order, so these properties are used. If they are null/empty it uses a default value (ClassName and New ClassName.asset, respectively). It's just that these properties aren't ever set or used anywhere.
     
    Last edited: Feb 25, 2016
  2. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    Whilst there is no constructor; attributes with public properties that have a getter and setter can be specified using the named parameter syntax that C# offers:
    Code (csharp):
    1.  
    2. [CreateAssetMenu(menuName = "My Name")]
    3. public class ItemReference : ScriptableObject
    4. {
    5.     [SerializeField, Multiline]
    6.     private string description;
    7.     [SerializeField, Range(0, 64)]
    8.     private int stackSize;
    9. }
    10.  
     
  3. Jeff-Rosenberg

    Jeff-Rosenberg

    Joined:
    Oct 23, 2012
    Posts:
    26
    Interesting. Is there a reason they went with this over other attributes like Range, Multiline, etc? I've never actually encountered this syntax and it looks like it's an attribute-only thing. It makes sense looking at it since it's very similar to default parameters. Thanks for the help, I never would have found that otherwise!

    For the devs, a note about that or some example code would do wonders for the documentation on this attribute.
     
    Harinezumi likes this.