对可编写脚本的对象使用 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
有没有办法控制 Things
和 Widgets
文件夹的顺序,类似于控制 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/