Skip to content

DavidF-Dev/Unity-DeveloperConsole

Repository files navigation

In-game Developer Console for Unity

This asset provides an in-game developer console (debug console) for Unity projects, allowing developers or players to execute commands or view incoming Unity messages (i.e. Debug.Log, errors, etc.)

The dev console window has a user-friendly look, inspired by Valve's Source engine console and Discord's user-interface. It includes text suggestion, autocomplete & key bindings that enable quick access to commands.

The asset is completely free, but if you'd like to show support you can buy me a bowl of spaghetti 🍝

Developer console preview

!!! Development notice !!!

This asset is no longer in active development, so use at your own risk.

If you are searching for an alternative, I can highly recommend Quantum Console. I have been using it in a few of my projects.

I am proud of my achievements developing this asset, but I unfortunately don't have the time to maintain or extend it. I may archive the repository at some point in the future. Feel free to continue using the asset, or create a Fork of the source for your own needs, adhering to the license.

Setup

Simply import the package into your project and you're good to go. No additional setup is required.

  • Download directly from the releases tab & import in Unity (Assets>Import Package).
  • Import via the Unity package manager (Window>Package Manager).
    • Git URL: https://github.com/DavidF-Dev/Unity-DeveloperConsole.git
      Package manager install
    • Or add the following line to Packages/manifest.json:
      "com.davidfdev.devconsole": "https://github.com/DavidF-Dev/Unity-DeveloperConsole.git"

Usage

When the game is running, press tilde ~ to toggle the dev console window. The window has an input field along the bottom where commands can be entered. Pressing ENTER will execute the typed command.

  • Use the UP / DOWN arrows to cycle through the command history or suggested commands.
  • Use TAB to autocomplete a suggested command.

Commands

Commands are in the form: commandName parameter1 parameter2 ... parameterN. Some commands have no parameters!

Typing "commands" will display a list of all available commands in the console log.
Typing "help print" will provide helpful information about the print command.
Typing "print "Hello world!"" will display the message "Hello world!" in the console log.

Text that is encased by quotation marks " will be interpreted as a single parameter.

Scripting

The dev console can be accessed via the DevConsole static class in the DavidFDev.DevConsole namespace.

  • Enable/DisableConsole(): enable or disable the dev console entirely (disabled by default in release builds).
  • Open/CloseConsole(): open or close the dev console window.
  • Log(): log a message to the dev console.
  • SetToggleKey(): change or disable the key used to toggle the dev console window.
  • AddCommand(): add a custom command to the dev console database.

Example

using DavidFDev.DevConsole;
DevConsole.EnableConsole();
DevConsole.SetToggleKey(null);
DevConsole.Log("Hello world!");

Custom commands

Custom commands can be added to the dev console by developers. They can be created in two ways:

  • Use Command.Create() to initialise a new command instance, allowing for multiple parameters and aliases.
  • Add the [DevConsoleCommand] attribute above a static method declaration, using the method body as the callback and arguments as command parameters.

Parameters

Default supported parameter types implement the IConvertible interface (e.g. int, float, string, bool, etc.)
To add a custom type, use DevConsole.AddParameterType<T>() (see FAQ below).

  • Enums are also supported.
  • Nullable-type parameters accept "null" and "~" (e.g. GameObject or Color?).
  • Value-type parameters accept "default" and "~" (e.g. Int32 or Color).

Commands that use a nullable bool (Boolean?) are used primarily as a toggle. For example, executing "showfps ~" will toggle showing the fps on-screen.

Example using Command.Create

DevConsole.AddCommand(Command.Create<string>(
  name: "print",
  aliases: "say,display",
  helpText: "Display a message in the dev console",
  p1: Parameter.Create(
    name: "message",
    helpText: "Message to display"
  ),
  callback: (string message) => DevConsole.Log(message)
));

Example using attribute

[DevConsoleCommand(
  name: "print",
  aliases: "say,display",
  helpText: "Display a message in the dev console",
  parameterHelpText: "Message to display"
)]
private static void Print(string message) => DevConsole.Log(message);

Built-in commands

The asset provides various built-in commands.

  • commands: display a list of available commands.
  • help (String)commandName: display helpful information about the specified command.
  • print (String)message: display a message in the dev console log.
  • exit: exit the game.
  • fullscreen (Boolean?)enabled: query or set whether the window is fullscreen.
  • bind (InputKey)key (String)command: bind the specified key to execute a command, even if the dev console window is closed.
  • showfps (Boolean?)enabled: query or set whether to display the fps on-screen.
  • cs_evaluate (String)expression: evaluate a C# expression or statement and display the result.
  • scene_load (Int32)buildIndex: load a scene.
  • scene_info (Int32)sceneIndex: display information about an active scene.
  • obj_list: display a hierarchical list of all game objects in the scene.
  • obj_info (String)name: display information about a game object.
  • sys_info: display information about the system.
  • enum (String)enumName: display all values of the specified enum.

And more...

FAQ

Q. Does the asset support the new input system?
A. Yes, the dev console supports both the legacy and new input system. The correct input system will be chosen automatically by the asset during compilation.

Q. Does the dev console work in release builds?
A. Yes, the dev console can be used in release builds, but will need to be enabled via script: DevConsole.EnableConsole(). It can be included in commercial projects as long as the licensing conditions are fulfilled. Furthermore, specific commands can be set up to only work in development builds.

Q. How do I add a custom parameter type?
A. Use DevConsole.AddParameterType<T>() to enable the use of the specified type as parameters. A parser function must be provided, which converts a string into the parameter type.

DevConsole.AddParameterType<GameObject>((string input) => GameObject.Find(input));

Q. Can I remove a built-in command?
A. Yes, use DevConsole.RemoveCommand() to remove almost any command. There are a few permanent commands that cannot be removed that are deemed essential for the developer console to run correctly.

Q. How can I stop keyboard input in the dev console from triggering game-specific actions (e.g. space will make the character jump even though the input field is focused)?
A. As far as I know, this is an unavoidable issue. I recommend making a global property for your game (e.g. AllowGameInput) which you can query before performing any game-specific input. This property can then reference DevConsole.IsOpenAndFocused, effectively disabling game-specific input when the dev console is open and the input field is focused.

public static bool AllowGameInput => !DevConsole.IsOpenAndFocused;

private void CheckGameInput()
{
  if (!AllowGameInput) return;
  
  // ... check game input
}

Q. This isn't quite what I'm after
A. There are alternatives available by other developers - each slightly different. If this one doesn't meet your needs, then maybe one of theirs will:

Acknowledgements

  • @exdli for help with supporting both input systems.
  • SpeedCrunch calculator which inspired the command suggestion / autocomplete design.
  • FiraCode font used under the SIL Open Font License 1.1.

Contact

If you have any questions or would like to get in contact, shoot me an email at contact@davidfdev.com. Alternatively, you can send me a direct message on Twitter at @DavidF_Dev.

Consider showing support by buying me a bowl of spaghetti 🍝
View my other Unity tools on my website 🔨

About

A Unity package that provides an in-game developer console, allowing developers or users to execute commands or view incoming Unity messages.

Topics

Resources

License

Stars

Watchers

Forks

Languages