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

标签 c# unity-game-engine unity-editor

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

例如,如果我有这些类(class)

[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
{
}

然后,当我在“项目”窗口中右键单击并选择“创建”时,上下文菜单将按此顺序显示我的菜单选项

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 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.


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

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
{
}

关于c# - 我可以在 CreateAssetMenu 中设置子文件夹的排序顺序吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66078718/

相关文章:

c# - 无法确定条件表达式的类型,因为 'int' 和 <null> 之间没有隐式转换

c# - Console.WriteLine 在 Dotnet Web Api 应用程序中不支持土耳其语字符

c# - 使用昂贵的构建缓存对象并允许更新

c# - 从项目中获取引用位置

c# - Unity5.3 Json如何使用unities new Json实用程序构造复杂的Json对象

android - Google Play 服务与 admob 冲突

unity-game-engine - Unity3d 在光标位置打开编辑器窗口

c# - 统一,C# |如何在方法之间设置等待时间?

c# - unity EditorGUI.PropertyField 无法完全禁用数组或列表

c# - 如何制作动态创建和销毁圆圈中的 2d 游戏对象的 slider