This is an ongoing project, so expect changes. All code is not final and is subject to change. I am currently working on restructuring the repo to contain less git fluff
- Automatic Command Registering (source code generator?)
- Async for Executing/Registering
- Attribute Flags (eg Hidden, Roles etc)
- Role assignment and command permissions
- Command Line Example
- Abstraction for user customisation
- Open Unity Package Manager
- Click '+' icon and select 'Install from git URL'
- Paste
https://github.com/maxfleetdev/unity-command-tool.git
into prompt - Hit Enter/Click done
- Package will begin installing
- Download commander.unitypackage from latest release page
- Double click on downloaded file
- Unity will automatically install inside the open project
Commands are assigned by using the Command[...]
attribute:
[Command("give", "Gives player an item using ID and Quantity")]
private void GiveCommand(int id, int quantity)
{
...
Attributes have many overloads, so you can provide descriptions, hidden commands, certain role access etc (working on improving with extra attributes)
Commands can be assigned in any class, static or instanced (MonoBehaviour)
- Static Command methods are registered on startup automatically by CommandRegistry
- Instanced Commands methods must be registered on creation (Awake/Start etc)
[Command("give", "Gives player an item using ID and Quantity")]
private static void GiveCommand(int id, int quantity)
{
AddItem(id, quantity);
}
private void Awake()
{
// Called Only Once
CommandRegistry.RegisterInstanceCommands(this);
}
[Command("give", "Gives player an item using ID and Quantity")]
private static void GiveCommand(int id, int quantity)
{
AddItem(id, quantity);
}
To execute commands, you simply pass the raw inputted string (eg "give 192 1") to the CommandExecuter class. The CommandExecuter class automatically converts the string into 2 parts: The command name, and the arguments
- The command name is what we are looking to invoke (eg give)
- The arguments are the method parameters the command method takes (eg int id, int quantity)
It achieves this by converting the split string into each parameter Data Type. You can take a look inside CommandParser.cs for how it works.
// userInput = "give 192 1"
private void OnInputSubmitted(string userInput)
{
CommandExecutor.ExecuteCommand(userInput);
// Returns success - player recieves 1x of item ID 192
}
If the command is successful, a log will be created. Otherwise, a warning will appear notifying the user that the command was incorrect.