• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
2
Question by Jeff-Rosenberg · Feb 25, 2016 at 12:39 AM · scriptableobjectcreateattribute

CreateAssetMenuAttribute menuName and order constructors

Today I found that you can use the CreateAssetMenu attribute to mark a ScriptableObject as something that should appear in the Create/Asset menu, just like the CreateScriptableObjectAsset from the Unify Wiki. It's great that we have this functionality built in now, but there are some problems:

The attribute places the button at the top of the Create/Asset menu, where Create/Asset/Folder should go, instead of placing at the bottom of the menu.

alt text

The constructor for the attribute does not provide ways to set the menuName, fileName, or order variables, so you cannot change the name of the entry on the Create/Asset menu or the order (which is why it appears on top). Because of this you also cannot nest things in submenus.

The CreateAssetMenuAttribute is also sealed, so it cannot be inherited from to add this functionality.

 public class CreateMenuAttribute : CreateAssetMenuAttribute
 {
     public CreateMenuAttribute(string menuName) : base()
     {
         this.menuName = menuName;
     }
 
     public CreateMenuAttribute(string menuName, int order) : base()
     {
         this.menuName = menuName;
         this.order = order;
     }
 }

 Assets/Scripts/Utility/CreateMenuAttribute.cs(4,14): error CS0509: `CreateMenuAttribute': cannot derive from sealed type `UnityEngine.CreateAssetMenuAttribute'

Is there a way to add this functionality? Can you create a C# attribute that adds and manages another attribute? Doing it with the attribute is much better than having to make a CreateMenu method for every class.

problem-1.png (42.7 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image numberkruncher · Feb 25, 2016 at 04:49 PM 0
Share

Duplicate of: http://answers.unity3d.com/questions/1079664/how-do-i-use-the-crea$$anonymous$$ssetmenu-attribute.html

1 Reply

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by numberkruncher · Feb 25, 2016 at 04:48 PM

Whilst there is no constructor; attributes with public properties that have a getter and setter can be specified using the named parameter.aspx) syntax that C# offers: Code (csharp):

 [CreateAssetMenu(menuName = "My Name")]
 public class ItemReference : ScriptableObject
 {
     [SerializeField, Multiline]
     private string description;
     [SerializeField, Range(0, 64)]
     private int stackSize;
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Harinezumi · Mar 08, 2018 at 10:46 PM 0
Share

I would have never figured this out without this answer. Why isn't this documented?

avatar image VagabondOfHell Harinezumi · Nov 15, 2018 at 01:40 AM 0
Share

it is documented. It's part of the .Net language.

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/named-and-optional-arguments

avatar image Harinezumi VagabondOfHell · Nov 15, 2018 at 11:12 AM 0
Share

O$$anonymous$$, you are right, but I meant they should give code example of this on the Unity page. I know, I'm expecting too much...

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

49 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Strange issue with "create menu" (since 2018.3 ?) 1 Answer

Custom Editor for embedded ScriptableObjects list 2 Answers

Custom inspector - How to serialize scene objects in ScriptableObject? 0 Answers

Singleton ScriptableObject unable to Find itself, returns null 2 Answers

Scriptable Object issues when inside .dll 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges