Skip to content

Repository files navigation

Dev Console

openupm

A drop-down developer console for Unity, for the editor and development builds only.

Dev Console showcase

  • Toggling the console toggles key is the tilde (~) or backtick (`) key, located right below the Escape (Esc) key.
  • Self-registering commands. Declare a static readonly field and the command exists. There is no scene object to add and no list to maintain.
  • One typed argument per command, parsed strictly: string, int, float, bool (true/false only), Vector3 ("1 2 3") and any enum.
  • Suggestions as you type. Each typed word matches the start of a command segment, in order: se pl he finds set_player_health, and se pla finds every set_player_*. Up/Down move the highlight, and Tab or Enter accepts it.
  • Compiled out of release builds. The AlpTheDev.DevConsole assembly only exists under UNITY_EDITOR || DEVELOPMENT_BUILD.

Requirements

  • Unity 2021.3 LTS or newer (2022.3 LTS and Unity 6 included)
  • Input System, set as the active input handler (pulled in by the package)

Install

Git URL

Window > Package Manager > + > Add package from git URL:

https://github.com/kureysalp/Unity-Dev-Console.git

OpenUPM

With the openupm-cli:

openupm add com.alpthedev.dev-console

Or add the scoped registry by hand in Edit > Project Settings > Package Manager:

  • Name: package.openupm.com
  • URL: https://package.openupm.com
  • Scope(s): com.alpthedev

Then install Dev Console from Window > Package Manager > My Registries.

Press Play and hit the key under Esc. The console bootstraps itself after the first scene loads.

Built-in commands

Command Does
help Lists every command
clear Clears the console log
quit Leaves play mode, or exits a build
set_time_scale <float> Sets Time.timeScale

Adding commands

Put your commands in a static class marked [ConsoleCommandSet], one public static readonly field per command. At startup the console finds every such class, in any assembly that references AlpTheDev.DevConsole, and runs its static constructor, so the fields register themselves.

#if UNITY_EDITOR || DEVELOPMENT_BUILD
using AlpTheDev.DevConsole;
using UnityEngine;

[ConsoleCommandSet]
public static class GameConsoleCommands
{
    public static readonly ConsoleCommand<float> SET_GRAVITY = new(
        "set_gravity",
        "Sets downward gravity in m/s^2.",
        "set_gravity <float>",
        SetGravity);

    private static void SetGravity(float metresPerSecondSquared)
    {
        Physics.gravity = Vector3.down * metresPerSecondSquared;
        DevConsole.Log($"gravity {metresPerSecondSquared:0.###}");
    }
}
#endif
  • ConsoleCommand takes no argument and rejects one. ConsoleCommand<T> requires one and converts it with ConsoleArgumentParser.
  • Report from the handler with DevConsole.Log and DevConsole.LogError. Both accept rich-text colour tags.
  • A duplicate id logs an error and keeps the first one registered.
  • Wrap your command files in #if UNITY_EDITOR || DEVELOPMENT_BUILD, because the package assembly does not exist in a release build.

Blocking game input while it is open

DevConsoleUI.IsOpen is true while the console has focus. Check it wherever your game reads input:

#if UNITY_EDITOR || DEVELOPMENT_BUILD
if (AlpTheDev.DevConsole.DevConsoleUI.IsOpen) return;
#endif

The console unlocks and shows the cursor while it is open, and restores the previous cursor state when it closes.

Running commands from code

DevConsole.Execute("set_time_scale 0.5") runs a line exactly as if it had been typed, echo included. DevConsole.GetText() returns the log, which is useful for editor automation and tests.

About

A drop-down developer console UPM package for Unity: self-registering commands and fuzzy suggestions.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages