Skip to content

hww/XiDebugMenu

Repository files navigation

XiDebugMenu

The elegant and easy to use debug menu for Unity 3D

⚙ Build and Release openupm semantic-release: angular

It is easy to use, lightweight library initially forked from wataru-ito/DebugMenu but deeply modifyed by hww

Introduction

An easy to use debug menu with little memory requirement. Influenced by the Emacs menu system. The basic concept is the minimum number of lines to add items to the menu (one line per option) and the procedural generation of menu items.

Alternative

This project designed for simplicity, for example it has limited amout of types for menu items. If you need more then consider to use the advanced version extDebug. It has much more features and I believe you will have a professional support from autor Iam1337.

The other alternative is my advanced menu XiKeyboard with advanced keystrokes systems.

Install

The package is available on the openupm registry. You can install it via openupm-cli.

openupm add com.hww.xidebugmenu

You can also install via git url by adding this entry in your manifest.json

"com.hww.xidebugmenu": "https://github.com/hww/XiDebugMenu.git#upm"

Usage

A demonstration of how to use it is below:

// The fields to modify by menu
public enum TrafficLight { Red,Green, Blue }
public TrafficLight enumValue;
public bool toggleValue;
public int integerValue;
public float floatValue;
        
// Create new menu
new DebugMenu("Edit/Preferences");
new DebugMenuToggle("Edit/Preferences/Toggle", () => toggleValue, value => toggleValue = value, 1);
new DebugMenuInteger("Edit/Preferences/Integer", () => integerValue, value => integerValue = value, 2);
new DebugMenuFloat("Edit/Preferences/Float", () => floatValue, value => floatValue = value, 3);
new DebugMenuAction("Edit/Preferences/Action", (item,tag) => { Debug.Log("Action"); }, 4);
new DebugMenuEnum<TrafficLight>("Edit/Preferences/TraficLight", () => enumValue, value => enumValue = value, 5);
new DebugMenu("Edit/Preferences/Extra Preferences", 10);

Picture1 Picture2 Picture3

Other way is to add items directly to menu.

var menu, new DebugMenu("Edit/Preferences");
new DebugMenuToggle(menu, "Toggle", () => toggleValue, value => toggleValue = value, 1);
new DebugMenuInteger( menu, "Integer",() => integerValue, value => integerValue = value, 2);
new DebugMenuFloat(menu, "Float", () => floatValue, value => floatValue = value, 3);
new DebugMenuAction(menu, "Action", (item,tag) => { Debug.Log("Action"); }, 4);
new DebugMenuEnum<TrafficLight>(menu, "TraficLight", () => enumValue, value => enumValue = value, 5);

Default Value

For integer, floats and enum types creating new DebugMenuItem will capture current value as defaut. When value is default it displayed as bright green color. Information about other color tags see below.

  • booleans
    • yellow color of label for enabed feture
    • white color of label for disabled feature
  • integers, floats, enums
    • bright green color of value for default
    • yellow color of value and label for not default value
  • actions
    • gray color for inactive action
    • other color for active action

Keyboard Shortcuts

  • E show or hide menu without closing it
  • ESC close current menu and display previous, or hide menu if there are no more
  • W,S move previous and next menu item
  • A,D edit menu item, open close submenu
  • R reset value to default
  • Shift-A,Shift-D edit menu item even if menu is closed
  • Shift-R reset value to default even if menu is closed

Events

Menu manager sends messages to menu items for rendering and modifying items .

public enum EvenTag
{
    Null,               //< Nothing 
    Render,             //< Render item, update label, value and colors
    Left,               //< Decrease value or call action
    Right,              //< Increase value or call action
    Up,                 //< Go to previous item 
    Down,               //< Go to next item
    Reset,              //< Reset value to default
    OpenMenu,           //< When menu open    
    CloseMenu           //< When menu closed
}

Actions

The action code can update the item's fields, and differently response for events: Inc,Dec and Reset

new DebugMenuAction("Edit/Preferences/Action", (item,tag) => { 
        switch (tag)
        {
        case EventTag.Right:
           item.value = "Increment";
           break;
        case EventTag.Left:
           item.value = "Decrement";
           break;
        ...
        }
}, 1);

Open and Close Menu Events

Possible to add menu items when menu opens, and remove items when it closes.

new DebugMenu("Edit/Preferences/Extra Preferences", 30)
    .OnOpen(menu => 
    {
        new DebugMenuToggle("Toggle2", menu, () => toggleValue, value => toggleValue = value);
    })
    .OnClose(menu =>
    {
        menu.Clear();
    });

Refresh and AutoRefresh Menu

If values in menu can be modified by game, to display it the menu should be rendered time to time.

new DebugMenu("Edit/Preferences").AutoRefresh(1f);

Set value 0 will never refresh menu. Alternative way is calling RequestRefresh method.

menu.RequestRefresh()

Increment Step and Precision

For integers and floats: The step field is a step for Inc and Dec evens. The format field is argument for ToString(...) method.

For floats The precision field is number of digits after period. For example increment=5 and precision=2 will make increment step 0.05

Other Syntax Sugar

// For DebugMenu class
DebugMenu OnOpen(Action<DebugMenu> onOpen)
DebugMenu OnClose(Action<DebugMenu> onClose)
DebugMenu AutoRefresh(float period)

// For MenuItem class
DebugMenuItem Order(int order)
DebugMenuItem AddToMenu(DebugMenu menu)
DebugMenuItem Value(string value)
DebugMenuItem LabelColor(string value)
DebugMenuItem ValueColor(string value)

// For DebugMenuEnum class
DebugMenuEnum<T> Default(T value)

// For DebugMenuInteger class
DebugMenuInteger Default(int value)
DebugMenuInteger Step(int value)
DebugMenuInteger Format(string value)

// For DebugMenuFloat class
DebugMenuFloat Default(float value)
DebugMenuFloat Precision(int value)
DebugMenuFloat Step(int value)
DebugMenuFloat Format(string value)