Scripting API
Open the console, run commands and react to it from your own code.
MirageConsole
The static class MirageConsole (namespace MI.Console) lets your game drive the console.
Call its members from the main thread.
| Member | What it does |
|---|---|
Execute(string entry) | Runs an entry as if it had been typed in the CLI. Errors are reported in the console, not thrown. |
Open(), Close(), Toggle() | Opens, closes, or toggles the console. |
IsOpen | Whether the console is open. |
IsAvailable | Whether this build has the console. false in a Release build without it. |
Opened, Closed | Events raised when the console opens and closes. If a handler throws, the exception is logged and the other handlers still run. |
Running commands from code
scene and timescale are commands of the Generic Unity commands sample:
using MI.Console;
// from a debug menu of your own
MirageConsole.Execute("scene 2");
MirageConsole.Execute("timescale 0.5");
The entry is echoed in the CLI like a typed one. A protected command still asks for the password.
An "Open console" button
On a touch screen you may prefer a button of your own to the three-finger gesture (set Open Method to Button in the settings):
using MI.Console;
using UnityEngine;
public class OpenConsoleButton : MonoBehaviour
{
void Start()
{
// in a build without the console there is nothing to open: hide the button
gameObject.SetActive(MirageConsole.IsAvailable);
}
public void OnClick() // wire it to your button
{
MirageConsole.Open();
}
}
Keeping your game from reacting
Typing in the console doesn't stop your game from reading the keyboard. Pause your own input while the console is open:
void OnEnable()
{
MirageConsole.Opened += OnConsoleOpened;
MirageConsole.Closed += OnConsoleClosed;
}
void OnDisable()
{
MirageConsole.Opened -= OnConsoleOpened;
MirageConsole.Closed -= OnConsoleClosed;
}
void OnConsoleOpened() { playerInput.enabled = false; }
void OnConsoleClosed() { playerInput.enabled = true; }
Or test MirageConsole.IsOpen before reading your own input.
A "Report a bug" button
With a webhook set, your game can send the log by itself. With -m, the
message is given and nothing is asked:
public void ReportBug(string whatHappened)
{
MirageConsole.Execute("sysinfo"); // the device and the versions, in the log sent
MirageConsole.Execute("send-log --last 300 --message \"" + whatHappened.Replace("\"", "'") + "\"");
}
The outcome (Log sent successfully. or the error) is logged once the server answers.
In Release builds
MirageConsole, the attributes and the settings class live in an assembly that is compiled in every
build, so code that uses them builds everywhere, without #if. In a player without the console, calls to
Execute, Open, Close and Toggle are removed at compile time, like a
stripped Debug.Log, and IsAvailable is false.