- Home /
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
Are you talking about in a Scene(OnGUI) or are you making an editor extension and want that behavior in the editor?
in the editor like this : [$$anonymous$$enuItem ("Objecthandler/Default option/Objects")] but then Objects must be a boolean with [$$anonymous$$enuItem ("Objecthandler/Default option/Textures")] .
it has to change a boolean in editor script turn true/false
I'm not sure why everyone seems confused by the question. He wants his menu item to support having a checkmark next to it so the user can visually see the state of the boolean he's toggling behind the scenes within the editor.
Having said that, I don't know how to do it...if you find out, post your own answer. :)
I really don't think you can create a menu item with sub-directories and then pick one of them like you are asking. The closest thing I could find in the Scripting Reference is this : http://docs.unity3d.com/Documentation/ScriptReference/Generic$$anonymous$$enu.html
Answer by darkbaram · Mar 07, 2016 at 03:27 PM
This has solution. Unity support function - Menu.SetChecked(). You can implement toggle menuitem using this function.
Answer by Selmar · Mar 15, 2016 at 08:50 PM
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...
}
}
Answer by Loius · Apr 06, 2013 at 05:23 PM
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.
Answer by rzo · Jul 20, 2015 at 12:45 PM
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.
Your answer
Follow this Question
Related Questions
(Editor) How to add Checkmarks to MenuItems 4 Answers
MenuItem toggle getting reset on play. 0 Answers
Editor Scripting Question 1 Answer
Can I create MenuItem from code? 3 Answers