How To Add New Right Click Menu To List Elements In Inspector

I have been struggling with trying to figure out how to do this. I’m currently using Unity 2021 where Lists are now ReorderableLists by default in the inspector. Very cool! There are a couple of right click menu items like “Copy”, “Duplicate Array Element”, etc.

I want to add my own customized entry for this specific List of Types that I’m working on. However, I can’t for the life of me figure out how to do this. It seems like I want to use a “GenericMenu” for this but it also seems like you can’t actually access the ReorderableList callbacks at all. How am I supposed to be able to accomplish this.

For now I will settle for just having it debug log all the indexes that are selected when I select the context menu entry.

Two things I found in the scripting API:

ContextMenuItem unfortunately doesn’t work for my use case as it doesn’t input the index of the selected item. Example:

[ContextMenuItem("MyTest", "MyTest")]
public List<MyCustomClass> items = new ();

I’ll have a look at the other link you provided to see if I can get what I need there.

EDIT: That’s unfortunate. It looks like that link is for an editor window and not a custom inspector editor script. Back to square one :frowning:

EDIT2: I’m applying a screenshot so you can see where I’m trying to add this right click menu item. As you can see in the example picture ideally I would like it to work with multiple selected items. I just not zero clue how to do this.

Do let us know how it turns out!

I’ll note that some editor extension addons like Odin Inspector make customising this stuff a lot easier.

I hear ya and I would use that but I’m making a package and while testing it I noticed that having a right click menu here to do a custom tedious action would make things a lot easier.

1 Like

Hold up! @spiney199 You actually might be right. I got a new right click menu item to show up by doing the following. I don’t know how to get values yet but I’m closer!

[CustomEditor(typeof(PCGSettings))]
    public class PCGSettingsEditor : Editor
    {
        protected virtual void OnEnable()
        {
            EditorApplication.contextualPropertyMenu += OnPropertyContextMenu;
        }

        void OnDestroy()
        {
            EditorApplication.contextualPropertyMenu -= OnPropertyContextMenu;
        }

        void OnPropertyContextMenu(GenericMenu menu, SerializedProperty property)
        {
            menu.AddItem(new GUIContent("TEST"), false, () =>
            {
                Debug.Log(JsonUtility.ToJson(property.objectReferenceValue));
                Debug.Log(property.objectReferenceValue);
            });
            return;
        }
    }

1 Like

That information should be stored in the serialised property that you can passed by the delegate. I presume (or I hope) if you right-click on a list element, the serialised property will be that of that particular list element. Though part of me feels like you might just get the serialised property for the whole collection instead.

Also I imagine there’s ways to do this via property drawers as well. I wouldn’t know the IMGUI way, but it’s possible with UI Toolkit drawers.

Okay, this seems to work for a single item well, but doesn’t recognize when a select more than one. It just works for the item I currently hover over and right click. Was able to get the data like the following:

void OnPropertyContextMenu(GenericMenu menu, SerializedProperty property)
        {
            menu.AddItem(new GUIContent("TEST"), false, () =>
            {
                Debug.Log(property.FindPropertyRelative("name").stringValue);
            });
            return;
        }

I’ll need to start researching what is possible with IMGUI or UI Toolkit. Is there one that is newer or one that I should gravitate toward that you know of?

1 Like

You can see the comparisons here: Unity - Manual: Comparison of UI systems in Unity

IMGUI is Unity’s original and old UI system. UI Toolkit is the fancy new one, and more feature rich.

Does depend on what versions of Unity you want your package to be compatible for. The UI toolkit API has changed a bit over the various major Unity versions, including breaking changes. IMGUI is more backwards compatible in that regard. Go back far enough and UI toolkit also doesn’t exist, or is lacking in many of the newer features.

If you want to target more recent Unity versions, go with UI Toolkit. It’s much more flexible, feature rich, and overall better to work with (in my opinion of course).

Okay thanks for the info. It sounds like I have a bit of a long road ahead of me. Appreciate the info!

1 Like

So here is a question. This is a “ReordableList” from Unity by default unless you apply the “[NotReordable]” value to the variable. Is it possible to access this ReorderableList? If I can then I can access its callbacks, for example when I click a list element.

You’d have to be drawing the list yourself. Inspectors/Property Drawers aren’t made to be accessed from the outside.

Pretty sure the IMGUI list drawer is untouchable as well. You’d have to be using UI Toolkit.

Dang. So am I stuck having to re-draw everything myself at this point?

At the end of the day you’re going to have to make a custom inspector or property drawer. That’s always required.

This is why I always recommend Odin Inspector. Because I know for a fact what you’re trying to do can be done very easily with it.

So I managed to get what I wanted. It was a lot of work I wrote a gist about it here: Unity Custom Right Click Menu To List · GitHub

It was so much code it’s better to store it there for others to more easily copy and use it if they need it.

1 Like