我可以在 CreateAssetMenu 中设置子文件夹的排序顺序吗?

Can I set the sort order of subfolders within CreateAssetMenu?

使用脚本化对象 CreateAssetMenu 时是否可以设置文件夹的顺序?

例如,如果我有这些 类

[CreateAssetMenu(menuName = "My Objects/First Thing", order = 1)]
public class FirstThing : ScriptableObject
{
}

[CreateAssetMenu(menuName = "My Objects/Second Thing", order = 2)]
public class SecondThing : ScriptableObject
{
}

[CreateAssetMenu(menuName = "My Objects/Third Thing", order = 3)]
public class ThirdThing : ScriptableObject
{
}

然后当我右键单击项目 window 和 select 创建时,上下文菜单按此顺序显示我的菜单选项

Create -> My Objects -> First Thing
          |-----------> Second Thing
          |-----------> Third Thing

但是,一旦我开始将菜单选项组织到子文件夹中,我就找不到控制子文件夹顺序的方法。例如,如果我现在有这个

[CreateAssetMenu(menuName = "My Objects/Things/First Thing", order = 1)]
public class FirstThing : ScriptableObject
{
}

[CreateAssetMenu(menuName = "My Objects/Things/Second Thing", order = 2)]
public class SecondThing : ScriptableObject
{
}

[CreateAssetMenu(menuName = "My Objects/Things/Third Thing", order = 3)]
public class ThirdThing : ScriptableObject
{
}

[CreateAssetMenu(menuName = "My Objects/Widgets/First Widget", order = 1)]
public class FirstWidget: ScriptableObject
{
}

[CreateAssetMenu(menuName = "My Objects/Widgets/Second Widget", order = 2)]
public class SecondWidget: ScriptableObject
{
}

现在“创建”上下文菜单如下所示

Create -> My Objects -> Things --> First Thing
          |             |--------> Second Thing
          |             |--------> Third Thing
          |-----------> Widgets -> First Widget
                        |--------> Second Widget

有没有办法控制 ThingsWidgets 文件夹的顺序,就像我控制 First ThingSecond Thing 等文件夹的顺序一样?在他们的父文件夹中?

来自 this post,它传播了一些关于 Sorting Details 下 Unity 菜单中排序顺序的详细信息,您可以在下面找到:

Sort priority for custom submenu groups themselves (as opposed to the menu item) is a bit tricky. A submenu group will be sorted at whatever priority the custom menu item had when it was first created.

换句话说:子文件夹按其中第一个元素的 order 值排序。

由于您的两个子文件夹都以 1 顺序开头,因此它们按字母顺序排序作为后备。

因此,为了将 Things 菜单置于 Widgets 文件夹下,只需确保元素具有绝对更高的值,例如

[CreateAssetMenu(menuName = "My Objects/Things/First Thing", order = 11)]
public class FirstThing : ScriptableObject
{
}

[CreateAssetMenu(menuName = "My Objects/Things/Second Thing", order = 12)]
public class SecondThing : ScriptableObject
{
}

[CreateAssetMenu(menuName = "My Objects/Things/Third Thing", order = 13)]
public class ThirdThing : ScriptableObject
{
}

[CreateAssetMenu(menuName = "My Objects/Widgets/First Widget", order = 1)]
public class FirstWidget: ScriptableObject
{
}

[CreateAssetMenu(menuName = "My Objects/Widgets/Second Widget", order = 2)]
public class SecondWidget: ScriptableObject
{
}

也许在 Things 项上改为 2, 3, 4 就足够了 ;)

这应该导致

Create -> My Objects -> Widgets -> First Widget
          |             |--------> Second Widget
          |
          |-----------> Things --> First Thing
                        |--------> Second Thing
                        |--------> Third Thing

另外注意

If your item has a priority of 11 or more than the previous item, Unity will create a separator before your item

并注意:

As a quick aside, if you change a menu item’s existing sort priority, you may not see it reflected in the Menus. I think Unity is caching the value, so to get it to update you can either restart your Editor, or do some gymnastics where you first remove the Priority from your attribute, compile, then add the new priority.


小专业提示:为了使维护更容易,我通常会有一个带有一些常量的中央 class 并执行例如

public static class Constants
{
    public static string MENU = "My Objects/";
    public static string MENU_THINGS = MENU + "Things/";
    public static string MENU_WIDGETS = MENU + "Widgets/";

    public static int ORDER_WIDGETS = 0;
    public static int ORDER_THINGS = 10;
}

然后做

[CreateAssetMenu(menuName = Constants.MENU_THINGS + "First Thing", order = Constants.ORDER_THINGS + 1)]
public class FirstThing : ScriptableObject
{
}

[CreateAssetMenu(menuName = Constants.MENU_THINGS + "Second Thing", order = Constants.ORDER_THINGS + 2)]
public class SecondThing : ScriptableObject
{
}

[CreateAssetMenu(menuName = Constants.MENU_THINGS + "Third Thing", order = Constants.ORDER_THINGS + 3)]
public class ThirdThing : ScriptableObject
{
}

[CreateAssetMenu(menuName = Constants.MENU_WIDGETS + "First Widget", order = Constants.ORDER_WIDGETS + 1)]
public class FirstWidget: ScriptableObject
{
}

[CreateAssetMenu(menuName = Constants.MENU_WIDGETS + "Second Widget", order = Constants.ORDER_WIDGETS + 2)]
public class SecondWidget: ScriptableObject
{
}