Inspector Context Menu, how to make it call something?

This line creates an Inspector Context Menu, but how to make clicking it actually call something?

[ContextMenu("MyHopeful - CallToSomething")]

Add it to a method, then run whatever code you want inside that method.

2 Likes

Thank you!!!

Do you happen to know how I can add a button to do this in the inspector of this script without making a custom inspector?

ContextMenu will add it to the ContextMenu of the script (the three dots).
If you want a button in the inspector itself, you’ll need to make a custom inspector (or a custom propertydrawer).

However, you can always call base.OnInspectorGUI() from any custom inspector to draw the default inspector.

Thus, you can add a button to the top of the inspector, then draw the default one below.

1 Like

This feels like a hugely missed opportunity, low hanging improvement: having ability to immediately add buttons to inspectors without having to learn about how to do it via editor customisation etc.

Cheers!