1
\$\begingroup\$

In Unity 5, I created a ScriptableObject class which adds itself to the Main Menu:

using UnityEngine;
using UnityEditor;

[CreateAssetMenu(fileName = "MyFancyAsset", menuName = "My Framework/Objects/My Fancy Object")]
public class MyFancySO : ScriptableObject
{
    // Public and [SerializeField] data comes here
}

This code works as expected. When the code is compiled, I can see the new menu being crated and working (a "My Framework" entry appears, and when choosing My Fancy Object), it is created appropriately).

What I am trying to do is to create a method with extended functionality o be added to the menu. So I tried like this:

using UnityEngine;
using UnityEditor;

[CreateAssetMenu(fileName = "MyFancyAsset", menuName = "My Framework/Objects/My Fancy Object")]
public class MyFancySO : ScriptableObject
{
    // Public and [SerializeField] data comes here

    [MenuItem("Assets/Create/My Framework/Objects/My Fancy Object (with more stuff)", menuItem = "My Fancy Object (with more stuff)")]
    public static void CreateInstanceWithMoreStuff()
    {
        // Now I create something, with logic based on what
        //   I can read in several files in the unity2d-extras
        //   repository.
    }
}

And although this code compiles with no errors, I don't see the new menu being created (but just the original menu created by CreateAssetMenu).

What am I missing?

\$\endgroup\$
1
\$\begingroup\$

To fix this, remove the code that sets the menuItem variable in the attribute:

[MenuItem("Assets/Create/My Framework/Objects/My Fancy Object (with more stuff)"]
public static void CreateInstanceWithMoreStuff()
{
    // Now I create something, with logic based on what
    //   I can read in several files in the unity2d-extras
    //   repository.
}

For more usage examples, see the Unity (5.6) Documentation for MenuItem

On later versions of Unity (e.g. on 2017.3), your code logs an error:

Ignoring menu item My Fancy Object (with more stuff) because it is in no submenu!

As a side note, I'm not sure why menuItem variable is exposed by Unity. The Unity (5.6) documentation for MenuItem (or later versions) do not mention these variables.

See below for the MenuItem.cs file. You are expected to use the constructors, and not the exposed public variables menuItem, validate, or priority.

public sealed class MenuItem : Attribute
{
    public string menuItem;
    public bool validate;
    public int priority;

    public MenuItem(string itemName);
    public MenuItem(string itemName, bool isValidateFunction);
    public MenuItem(string itemName, bool isValidateFunction, int priority);
}
\$\endgroup\$
  • \$\begingroup\$ Thanks :D I will check that once home. But sounds good so far. \$\endgroup\$ – Luis Masuelli May 11 '18 at 20:10
  • \$\begingroup\$ @LuisMasuelli also, you have a typo in your code. "Asses" instead of "Assets" \$\endgroup\$ – sonny May 11 '18 at 20:23
  • \$\begingroup\$ Sorry. This code is illustrative and not the actual code. However, only data fields exist in my actual code, and the methods I declared in the menu are also empty (yet). \$\endgroup\$ – Luis Masuelli May 11 '18 at 21:48
  • \$\begingroup\$ @LuisMasuelli please mark this answer as accepted if it was the solution \$\endgroup\$ – sonny May 11 '18 at 23:09
  • 1
    \$\begingroup\$ I fixed... my typo. When I go back to home I will test it. \$\endgroup\$ – Luis Masuelli May 11 '18 at 23:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.