Skip to content
Alex Lementuev edited this page May 5, 2015 · 2 revisions

Creating Commands

Note: It is recommended to keep all your custom commands in the Assets/Editor folder so they won't increase your final binary size:

You can put all your commands in a single file:

Assets/Editor/Commands.cs

...or you put every command in a separate file and store all command files in:

 Assets/Editor/Commands

To create a custom command you need to create a C# class and derive it from CCommand class. You can pick any name for your command's class but it's strongly recommended to prefix it with Cmd_ followed by its name. Example:

using LunarPlugin;

[CCommand("doSomething")]
class Cmd_doSomething : CCommand
{
  ...
}

Play Mode Only Commands

To make command only available in the Play Mode, use CPlayModeCommand as a base class

Custom Commands Examples:

  • Command with no args:
using LunarPlugin;

[CCommand("test")]
class Cmd_test : CCommand
{
    void Execute()
    {
        PrintIndent("Hello, Unity!");
    }
}

Result:

> test
  Hello, Unity!
  • Command with a single int arg:
using LunarPlugin;

[CCommand("test")]
class Cmd_test : CCommand
{
    void Execute(int value)
    {
        PrintIndent("Your value is {0}", value.ToString());
    }
}

Result:

> test 10
  Your value is 10
  • Command with a single Vector2 arg:
using UnityEngine;
using LunarPlugin;

[CCommand("test")]
class Cmd_test : CCommand
{
    void Execute(Vector2 pos)
    {
        PrintIndent("Your pos is {0}", pos.ToString());
    }
}

Result:

> test 10 20
  Your pos is (10.0, 20.0)
  • Command with multiple args:
[CCommand("test")]
class Cmd_test : CCommand
{
    void Execute(int count, string name)
    {
        PrintIndent("You have {0} {1}s", count.ToString(), name);
    }
}

Result:

> test 5 apple
  You have 5 apples
  • Variable args command:
using LunarPlugin;

[CCommand("test")]
class Cmd_test : CCommand
{
    void Execute(int[] numbers)
    {
        int sum = 0;
        for (int i = 0; i < numbers.Length; ++i)
        {
            sum += numbers[i];
        }

        PrintIndent("Sum: {0}", sum.ToString());
    }
}

Result:

> test 1 2 3 4 5
  Sum: 15
> test
  Sum: 0

Supported arguments types

Any command can accept arguments of the following types (in any combination):

  • int
  • float
  • string
  • bool
  • int[]
  • float[]
  • string[]
  • bool[]
  • Vector2
  • Vector3
  • Vector4

Arguments Auto Completion

You can build your own argument auto completion lists (for Tab and double Tab hits):

[CCommand("test")]
class Cmd_test : CCommand
{
    void Execute(string value)
    {
    }

    protected override string[] AutoCompleteArgs (string commandLine, string token)
    {
        string[] values = { "one", "two", "three" };

        List<string> result = new List<string>();
        foreach (string value in values)
        {
            if (value.StartsWith(token))
            {
                result.Add(value);
            }
        }

        return result.ToArray();
    }
}

Result:

Type test and hit Tab twice:

> test 
three  two   one

Type test t and hit Tab twice:

> test t
three  two

Type test tw and hit Tab:

test two

You can limit possible values when creating command:

using LunarPlugin;

[CCommand("test", Values="one,two,three")]
class Cmd_test : CCommand
{
    void Execute(string value)
    {
    }
}

Or the same with:

[CCommand("test")]
class Cmd_test : CCommand
{
    public Cmd_test()
    {
        this.Values = new string[] { "one", "two", "three" };
    }

    void Execute(string value)
    {
    }
}

Result:

Type test and hit Tab twice:

> test 
three  two  one

Type test t and hit Tab twice:

> test t
three  two

Type test tw and hit Tab:

test two

Pass an unexpected argument:

> test four
  Unexpected argument 'four'
  usage: test one|two|three

Command options:

You can add options (both required/non-required) to any command:

using LunarPlugin;

[CCommand("test")]
class Cmd_test : CCommand
{
    [CCommandOption(ShortName="c")]
    private int count = 1;

    void Execute(string what)
    {
        for (int i = 0; i < count; ++i)
        {
            PrintIndent("{0}: {1}", i.ToString(), what);
        }
    }
}

Result:

> test Unity
  0: Unity
> test -c 3 Unity
  0: Unity
  1: Unity
  2: Unity
> test --count 3 Unity
  0: Unity
  1: Unity
  2: Unity

Required options:

using LunarPlugin;

[CCommand("test")]
class Cmd_test : CCommand
{
    [CCommandOption(ShortName="c", Required=true)]
    private int count = 1;

    void Execute(string what)
    {
        for (int i = 0; i < count; ++i)
        {
            PrintIndent("{0}: {1}", i.ToString(), what);
        }
    }
}

Result:

> test
  Missing required option --count(-c)
  usage: test -c|--count <int> <what>

Supported options types

Any command can accept arguments of the following types (in any combination):

  • int
  • float
  • bool
  • string
  • int[]
  • float[]
  • bool[]
  • string[]

Auto complete options:

Command options can be autocompleted the same way as arguments:

using LunarPlugin;

[CCommand("test")]
class Cmd_test : CCommand
{
    [CCommandOption(ShortName="o1")]
    private int opt1;

    [CCommandOption(ShortName="o2")]
    private string opt2;

    [CCommandOption(ShortName="o3")]
    private bool opt3;

    void Execute()
    {
    }
}

Result:

Type test - and hit Tab:

> test -o

Hit Tab twice:

> test -o
-o1  -o2  -o3

Type test -- and hit Tab:

> test --opt

Hit Tab twice:

> test --opt
--opt1  --opt2  --opt3