An open API service indexing awesome lists of open source software.

https://github.com/barionlp/commandsystem

An automated and extendable command system as Unity package
https://github.com/barionlp/commandsystem

csharp dotnet unity

Last synced: 9 months ago
JSON representation

An automated and extendable command system as Unity package

Awesome Lists containing this project

README

          

# Command System
A system for calling funcions via text input
(made for Unity, can be used outside)

## Usage (Unity)
- (optional) install UpmGitExtension package (https://github.com/mob-sakai/UpmGitExtension.git)
- install Command Sytem package (https://github.com/BarionLP/CommandSystem.git)

### Run Commands
```csharp
CommandManager.Execute("command");
```

### Set Logger
```csharp
CommandManager.SetLogger(new UnityDebugCommandLogger()); //print logs in the unity console

// Custom Logger
public class CustomLogger : ICommandLogger
{
public void Log(string message) => Debug.Log(message);
public void LogWarning(string message) => Debug.LogWarning(message);
public void LogError(string message) => Debug.LogError(message);
}
```

### Simple Commands
```csharp
CommandManager.Commands.Register(new SimpleCommand("test", ()->{
//Do something

//Log
CommandManager.Log();
CommandManager.LogWarning();
CommandManager.LogError();
}));
```

### Advanced Commands
```csharp
//register static methods with the Command attribute
// atomatically generates syntax hints
CommandManager.Commands.Register();

//register command "cmd" with the methods as subcommands (e.g. "cmd add 4 2")
CommandManager.Commands.RegisterGroup("cmd");

public class TestCommands
{
[Command("add")] //automatically parses primitive arguments
public static void Add(int left, int right) => CommandManager.Log($"{left} + {right} is {left+right}");

[Command("wait")] // supports async
public static async Task Test(float seconds) => await Task.Delay((int)(seconds*1000));

[Command("give")] //other objects require custom parsers (see below)
public static void GiveItem(Item item) => Player.Give(item);
}
```

### Parsers
```csharp
//has built in parsers for most primitives (float, int, uint, short, ...) see in `ArgumentParser.cs`

ArgumentParsers.Register(new CustomParser()); // register a custom parser
ArgumentParsers.Register(new EnumArgumentParser()); //generates an enum parser (parses by name)

public class CustomParser : IArgumentParser
{
public CustomType Parse(ReadOnlySpan raw) => //however you want, e.g. from a registry (`null` if fails)
public IEnumerable GetSuggestions() => Enumerable.Empty(); //or all possible values
}

```