Skip to content

ConsoleCommands

André edited this page Mar 22, 2022 · 8 revisions

The Debug Console allows you to use commands. You can make any type of commands you want and make them do whatever you want.

( Alpha State - Might be refactored later )

Basic Command

Let's say that we want to make a simple command that sends a "Hello World!" to Unity's Console. A possible implementation might be:

DebugConsole.cs

private void Awake()
{
    ...
    // Make an Action to be associated with the command
    Action HelloWorldCommand = () => Debug.Log("Hello World!");

    // Make the command itself
    var basicCommand = new DebugCommand(name = "Hello World", 
                                        description = "Sends a \"Hello World!\" to Unity Editor Console.", 
                                        format = "helloworld", 
                                        command = HelloWorldCommand)
    // Add the command to the list
    commandList.Add(basicCommand);
    ...
}

Command with Arguments

You can add arguments to your commands by passing them through the delegate. Here's an example of how you would do it:

DebugConsole.cs

private void Awake()
{
    ...
    // This Action takes the arguments and creates a message to send on the Console.
    Action<string[]> SayCommand = (args) => 
    {
        // args[0] is the command itself, from  that point beyond it's all the extra arguments that come with it
        if(args.Length <= 1) return;

        // Create the message from the arguments
        string message = args[1];
        for (int i = 2; i < args.Length; i++)
        {
            message += " " + args[i];
        }

        // Lastly, send the message
        Debug.Log(message);
    }

    // Make the command itself
    var sayCommand = new DebugCommand<string[]>(name = "Say", 
                                                description = "Sends the arguments as a message in the Unity Editor Console.", 
                                                format = "say", 
                                                command = SayCommand)
    // Add the command to the list
    commandList.Add(sayCommand);
    ...
}

Clone this wiki locally