A drop-down developer console for Unity, for the editor and development builds only.
- Toggling the console toggles key is the tilde (~) or backtick (
`) key, located right below the Escape (Esc) key. - Self-registering commands. Declare a
static readonlyfield 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/falseonly),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 hefindsset_player_health, andse plafinds everyset_player_*. Up/Down move the highlight, and Tab or Enter accepts it. - Compiled out of release builds. The
AlpTheDev.DevConsoleassembly only exists underUNITY_EDITOR || DEVELOPMENT_BUILD.
- 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)
Window > Package Manager > + > Add package from git URL:
https://github.com/kureysalp/Unity-Dev-Console.git
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.
| 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 |
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.###}");
}
}
#endifConsoleCommandtakes no argument and rejects one.ConsoleCommand<T>requires one and converts it withConsoleArgumentParser.- Report from the handler with
DevConsole.LogandDevConsole.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.
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;
#endifThe console unlocks and shows the cursor while it is open, and restores the previous cursor state when it closes.
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.
