Menu Items as checkboxes/radio buttons

Hello everyone,

I am making some menu items to enable some functions but comming across a problem. if you go to unity3d and click on the edit menu item (default in the editor) and then all the way down to graphics emulation or network emulation you see some new buttons in a menu next to it with a checkbox. when you select 1 the others are disabled.

i want to make the same function in my menu item but now my question is how? I searched on google for a while now and haven’t find anything close to it.

tried to just add true/false at the end of the name but of course that isn’t possible either… so how can i make such radio/checkboxes buttons in my menu items?

Thx in advance,

Ruben

This has solution. Unity support function - Menu.SetChecked().
You can implement toggle menuitem using this function.

As @darkbaram said, Menu.SetChecked(). From http://answers.unity3d.com/answers/1146949/view.html :

 using UnityEditor;
 [InitializeOnLoad]
 public static class CheckmarkMenuItem {
 
     private const string MENU_NAME = "Example/Toggle";
 
     private static bool enabled_;
     /// Called on load thanks to the InitializeOnLoad attribute
     static CheckmarkMenuItem() {
         CheckmarkMenuItem.enabled_ = EditorPrefs.GetBool(CheckmarkMenuItem.MENU_NAME, false);
 
         /// Delaying until first editor tick so that the menu
         /// will be populated before setting check state, and
         /// re-apply correct action
         EditorApplication.delayCall += () => {
             PerformAction(CheckmarkMenuItem.enabled_);
         };
     }
 
     [MenuItem(CheckmarkMenuItem.MENU_NAME)]
     private static void ToggleAction() {
 
         /// Toggling action
         PerformAction( !CheckmarkMenuItem.enabled_);
     }
 
     public static void PerformAction(bool enabled) {
 
         /// Set checkmark on menu item
         Menu.SetChecked(CheckmarkMenuItem.MENU_NAME, enabled);
         /// Saving editor state
         EditorPrefs.SetBool(CheckmarkMenuItem.MENU_NAME, enabled);
 
         CheckmarkMenuItem.enabled_ = enabled;
 
         /// Perform your logic here...
     }
 }

Well, a silly brute force way would be to write a script that edits and recompiles your scripts whenever you want a state change, and use altcode checkmarks in the menu names.

But that’s silly.

I don’t see a way in the scripting reference, and I don’t recall seeing any screenshots of that behaviour in non-Unity menus, sorry.

Sadly, this is the only solution I was able to come up with. But it may be useful for others.

[MenuItem("Assets/Bundle Builder/Force Offline/On")]
public static void ForceOfflineTrue()
{
    AssetManager.Instance.ForceOffline = true;
}

[MenuItem("Assets/Bundle Builder/Force Offline/Off")]
public static void ForceOfflineFalse()
{
    AssetManager.Instance.ForceOffline = false;
}

[MenuItem("Assets/Bundle Builder/Force Offline/On", true)]
public static bool CanForceOfflineTrue()
{
    return AssetManager.Instance.ForceOffline == false;
}

[MenuItem("Assets/Bundle Builder/Force Offline/Off", true)]
public static bool CanForceOfflineFalse()
{
    return AssetManager.Instance.ForceOffline == true;
}

It creates Submenu in Asset/BundleBuilder/Force Offline with two items On and Off (The first two methods gets called when appropriate item is clicked). To indicate the state of the Force Offline item, I’ve added other two methods (validation functions), that let enabled only the item that changes the state—i.e. when Force Offline is set to true, only Off item will be enabled.

This soultion is far from being optimal, but it works somehow for my case.