Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Resolved MenuItem priority

Discussion in 'Authoring Dev Blitz Day 2023' started by Baste, Jan 26, 2023.

  1. Baste


    Jan 24, 2013
    MenuItem has some pretty strange and archaic rules around their ordering and layout. It's all built around the priority value of the menu item, but that system seems to have been thrown together a decade ago as a stopgap and then never touched again.

    If you want to position an element next to a different one, you'll need to give it a priority value that's one more or less than that item. But there's no way to figure out the priority value of builtin menu items other than trial and error! So if we want to be close to something related, we have to binary search our priority until we hit the correct spot.

    You can also get vertical dividers between the elements. As an example, here's a vertical divider between Package Manager and Asset Management in the window menu.

    That shows up if the difference between the priority of different elements is larger than 10:

    Code (csharp):
    1. [MenuItem("Test/Item1", false, 0)]
    2. public static void Foo() {}
    3. [MenuItem("Test/Item2", false, 10)]
    4. public static void Bar() {}

    Code (csharp):
    1. [MenuItem("Test/Item1", false, 0)]
    2. public static void Foo() {}
    3. [MenuItem("Test/Item2", false, 11)]
    4. public static void Bar() {}

    That's a) not written down and b) a really strange way to do things! It also means that if there are two things with a divider, but the difference between them is less than 22, then you can't add a new item with dividers between them.

    Finally, it's really confusing how we position a category. The ID's used are used to position things next to each other inside a category menu, but how is the category itself positioned? Hard to explain, so if you look here:

    TextMeshPro is positioned over General, and under Text. But there's no [MenuItem("Window/TextMeshPro")]! So I assume that the positioning of TextMeshPro here is based on the value of either the lowest or highest element that starts with Window/TextMeshPro, and then that's compared to the same for things that are inside of General/ or Text/, but this is again not written down and also totally confusing.

    So this system could really be improved! Some suggestions are:
    - Make a static class or enum that lists all the ID's that Unity uses internally, so we can access that to position ourselves next to elements.
    - Write down all the rules for positioning
    - Make dividers not be based on the numeric distance between elements, but instead have bools or enums or whatever on MenuItem that allows us to say things like "I want a divider below/above this element"

    Knowing Unity, I assume that what's happened here is that this whole thing has been known of as problems, and then you have tried to make a fancy, perfect system to replace it, and all of those have been to fancy and have failed. Any chance you'd (for once) go for an incremental improvement here?
  2. marius-zrelskis


    Unity Technologies

    Feb 4, 2021
    You're absolutely right! Layout and order limiting rules are indeed the limiting factor to progress with this faster. As you correctly identified one of the limiter is priority number based order instead of groups (collections) or relations between items (e.g. "Paste" relates to "Copy"). Adding automatic separators feature every tenth item is sometimes good to have but sometimes very questionable.
    To solve another question of current menu items priorities it is possible to enable it via editor preferences as pictured. When enabled, after reload it will display number value next to each menu item, hope it helps somebody to not get lost in this :)

    Attached Files:

    Baste likes this.