A quick command parser for use in games.
A command line parser for use in Unity.
To install for use in Unity, copy everything from this repository to <YourNewUnityProject>/Packages/Popcron.Console
folder.
If using 2018.3.x, you can add a new entry to the manifest.json file in your Packages folder:
"com.popcron.console": "https://github.com/popcron/console.git"
After that, call Console.Open = false;
in any Start method.
NB: If you are referencing Console with Package manager you will have to create assembly definition for your code and reference it in settings (see FAQ). Otherwise Console will see only its assembly.
string command = "help";
object result = await Parser.Run(command);
or just help
from the console window.
Command methods that are declared as static can be executed from any context. Instance methods however cannot be called in the same way. These commands require an object that owns the method. The parser class provides methods that allow you to register and unregister objects with a unique ID, which can allow instance commands to be called on an object.
using UnityEngine;
using Popcron.Console;
public class PlayerAmmo : MonoBehaviour
{
[Command("ammo")]
public int ammo = 100;
//when the object gets created, register it
private void OnEnable()
{
Parser.Register(this, "player");
}
//when the object gets destroyed, unregister it
private void OnDisable()
{
Parser.Unregister(this);
}
}
To call this command from the console, you call the method exactly as its writen, with the addition of the @id
prefix.
@player ammo 100
To create a simple command, add a Command
attribute to your method.
using Popcron.Console;
public class Commands
{
[Command("add")]
public static int Add(int a, int b)
{
return a + b;
}
}
add 2 3
will return 5
Categories arent necessary, but they allow you to categorize commands into a list which can be retrieved using Parser.Categories
. To add categories, add a Category
attribute to the class itself. This is primarely useful when listing all of the commands using help
.
using Popcron.Console;
[Category("Default commands")]
public class Commands
{
[Command("add")]
public static int Add(int a, int b)
{
return a + b;
}
}
Commands can have multiple aliases. To give a command another calling name, add the Alias
attribute
using Popcron.Console;
[Category("Default commands")]
public class Commands
{
[Alias("+")]
[Command("add")]
public static int Add(int a, int b)
{
return a + b;
}
}
+ 2 3
will return 5
add 7 -2
will return 5
You can also add your own type converters by inheriting from the abstract Converter
class.
info
Prints the system information (OS, Device, CPU and GPU, RAM)clear
Clears the consoleowners
Prints all registered owners, along with their instance methods, properties and fieldsconverters
Lists all registered convertershelp
Prints all commands found as static methods, properties and fieldsecho
Dumb echo commandscene
Prints out the entire scene hierarchy"com.popcron.console": "https://github.com/popcron/console.git"
to your manifest.json
file in the packages folder.Console.Initialize()
method, or any of the static methods/properties once in the game.Console.Key
property.System.Console.WriteLine("wee")
.Category
attribute to the class type.Converter
class.