unity-console_blog

Above is a screenshot from the Unity3D console plugin I've been working on. I've found it useful in my own development and have released it as an open source project in hopes that it can be a resource for other Unity devs.

Why would you add a developer console to your Unity3D project?

A developer console is a cheap and easy user-interface element for accessing meta-game features like tweaking global settings, enabling cheats, or testing content under development which may not have a proper GUI yet. It also hits all the right nostalgia buttons.

What is involved?

  1. Download the Mikelovesrobots Unity3d Console plugin: https://github.com/mikelovesrobots/unity3d-console
  2. Copy the Console directory to your Assets/Plugins folder. (Make the plugins folder if it doesn't exist.)
  3. Drag the console prefab into your scene.
  4. Run your scene and press ~ to launch the console.
  5. Now register some commands

How do I create commands?

Here's a relatively simple initializer I threw together for saving and loading the scene in a map editor:

using UnityEngine;
using System.Collections;

public class ConsoleInitializer : MonoBehaviour {
    void Start () {
        var repo = ConsoleCommandsRepository.Instance;
        repo.RegisterCommand("help", Help);
        repo.RegisterCommand("save", Save);
        repo.RegisterCommand("load", Load);
    }

    public string Help(params string[] args) {
        return "save [filename] -- saves the map\n" +
            "load [filename] -- loads the map\n" +
            "help -- this command"
    }

    public string Save(params string[] args) {
        var filename = args[0];
        MapPersister.Save(filename);
        return "Saved to " + filename;
    }

    public string Load(params string[] args) {
        var filename = args[0];
        if (MapPersister.Exists(filename)) {
            MapPersister.Load(filename);
            return "Loaded " + filename;
        } else {
            return "No map exists named " + filename;
        }
    }
}

An example console session in my hypothetical map editor scene would now look like this:

> load example.map
No map exists named example.map

> save example.map
Saved to example.map

> load example.map
Loaded example.map

Do I get anything else for free?

Yeah! You can also log directly to the developer console via the ConsoleLog singleton:

ConsoleLog.Instance.Log("Player died");

Then when you bring up the console with the tilde you can see all your recorded log statements:

> 
Player died

Any gotchas?

Nope. It's BSD3 licensed and free.

If you would like to contribute your changes back, it would be appreciated! Please open a pull request on the Github project.

Cheers,
Mike