You can build interactive menus for in-game tools, debug settings, or cheat features β great for prototyping or QA workflows.
π§ Two Ways to Add Options
You can easily register fields, properties or methods as menu items using the attribute.
Call this during setup to register all [ConsoleOption]
entries on a class:
void Start()
{
NjConsole.Options.CreateCatalogFrom(this, "TestOptions");
// ^ second param `TestOptions` is optional, it puts all the items inside the `TestOptions` folder in this example.
// If 'this' is a MonoBehaviour, options will auto-remove when `OnDestroy()`
}
[ConsoleOption]
void SayHello() {
Debug.Log("Hello");
}
// directory / folder
[ConsoleOption("ChildFolder/My Second Button")]
void AnotherButton() {
Debug.Log("Clicked my second [ConsoleOption] button");
}
// header
[ConsoleOption("A button inside a header",
header:"My Header")]
void AButtonInsideHeader() {
Debug.Log("Clicked my second [ConsoleOption] button");
}
// key binding - Shift + W to call WinLevelCheat() in playmode
[ConsoleOption(key:Key.W,
keyModifier:ConsoleKeyBindings.Modifier.Shift)]
void WinLevelCheat() {
Debug.Log("Clicked WinLevelCheat");
}
// auto close console overlay
[ConsoleOption(autoClose:true)]
void AutoCloseConsole() {
Debug.Log("Console overlay should be closed now that you clicked a button with auto close flag");
}
[ConsoleOption()]
bool InfiniteLives;
[ConsoleOption()]
bool InfiniteAmmo {get; set;}
Warning: Keybinding feature only works for buttons and toggles.
[ConsoleOption]
int Health;
[ConsoleOption]
int HealthProperty {get; set;}
// With left and right step buttons
[ConsoleOption(increments:0.5f)]
float Speed;
// Range clamping
[ConsoleOption()]
[Range(1, 5)] // FYI: If you use a version before Unity 6, RangeAttribute can not be used in properties
int Strength;
[ConsoleOption]
[Multiline] // if you need multiline text entry, put [Multiline] attribute.
string UserCommentMessage;
[ConsoleOption]
void SaySomething(string receivedText)
{
Debug.Log("You said: " + receivedText);
}
[ConsoleOption]
DeviceOrientation preferredOrientation;
Use this method for full control and dynamic setup.
var catalog = NjConsole.Options.CreateCatalog();
Catalogs are useful because when you no longer need a set of option menus, you can just call
catalog.RemoveAll()
.
catalog.AddButton("My First Button", () => Debug.Log("Clicked my first button"));
// directory / folder
catalog.AddButton("A Folder / Child Folder / Child Button", () => Debug.Log("Child button was clicked"));
// header sub-grouping
catalog.AddButton("A button in a header sub-group", () => {})
.SetHeader("My Header");
// key binding to space key
catalog.AddButton("My Space Key Bound Button", () => Debug.Log("Clicked my Space key bound button"))
.BindToKeyboard(KeyCode.Space);
// auto close console overlay
catalog.AddButton("My auto close button", () => Debug.Log("Console overlay should be closed now that you clicked a button with auto close flag"))
.AutoCloseOverlay();
var toggle1 = false;
var toggle2 = false;
catalog.AddToggle("My First Toggle", (v) => toggle1 = v, () => toggle1);
// folder + key binding + auto close
catalog.AddToggle("A Folder / My T key Bound Toggle", (v) => toggle2 = v, () => toggle2);
.BindToKeyboard(KeyCode.T)
.AutoCloseOverlay();
Both buttons and toggles can be bound to a keyboard key via
...BindToKeyboard(KeyCode.Space)
.
Shift + Ctrl + E style combo can be done via...BindToKeyboard(KeyCode.E, ConsoleKeyBindings.Modifier.Shift | ConsoleKeyBindings.Modifier.Ctrl)
.
β οΈ Only one keybinding per item.Set console overlay to auto close after you press the button via
...AutoCloseOverlay()
.
var aFloat = 12.34f;
catalog.AddNumberPrompt("A Number", (v) => aFloat= v, () => aFloat);
// clamped int number
var int0To100 = 50;
catalog.AddNumberPrompt("0 to 100", (v) => int0To100 = Mathf.Clamp(v, 0, 100), () => int0To100);
// Number prompt with left and right step buttons
var steppedNumber = 10;
catalog.AddNumberPrompt("Stepped number", (v) => steppedNumber = v, () => steppedNumber, 2);
var text = "Initial text";
catalog.AddTextPrompt("My Text Prompt", (v) => text = v, () => text);
// Text prompt with submission validation and input restriction
var text2 = "Initial text";
catalog.AddTextPromptWithValidation("My validated text",
getter: () => text2,
setter: v => {
if(v.All(char.IsUpper)) // in this example we only accept capital letters
{
text2 = v;
return true; // return true to accept the input and close the prompt.
}
return false; // Return false to block user from closing the dialog due to invalid value.
},
validator: (v) => {
if (v.Length > 5) v = v.Substring(0, 5); // Trim out invalid characters (or length) and return the valid version (optional)
return v;
} );
var choices = new List<string>() { "A", "B", "C", "D" };
var index = 0;
catalog.AddChoice("A Choice List", choices, () => index, (v) => index = v);
// An enum choice:
var platform = RuntimePlatform.OSXEditor;
options.AddEnumChoice("A Choice Enum", () => platform, (v) => platform = v);
π‘ Use grouped paths (like βCategory / Subgroup / Optionβ) to keep menus organized and easier to navigate:
catalog.AddButton("App / Utilities / Reload Scene", () => ReloadScene());
Use [ConsoleOption] when:
Use programmatic registration when:
foreach (var itemType in inventoryItemTypes)
{
var local = itemType;
catalog.AddButton("Inventory/Give " + itemType.Name, () => GiveItem(local));
}
You can create runtime shortcut buttons for quick access to options.
Note: Shortcuts are available only in runtime overlay mode, not in the editor window.
Shortcuts will align to one of four corners:
Once you drop your first shortcut, youβll enter shortcut edit mode:
hide shortcuts
, use: Console > Options > Show Shortcuts to reopen edit mode.