|
1 | | -# o2-InGame-Console-For-Unity |
2 | | - O2 Console - In-Game Debug Console for Unity |
| 1 | +# O2 Console |
| 2 | + |
| 3 | +**O2 Console** is an in-game, attribute-driven debug console for Unity that allows developers to define and execute commands easily. Built using the **Reflection API**, it dynamically discovers scene objects and registers command methods at runtime, making it incredibly flexible. With its real-time command execution and robust exception handling, O2 Console provides a powerful way to interact with and debug your game, enhancing your development experience. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- **Command Attribute System**: Mark methods as console commands using `[ConsoleCommand("Command Key")]`, and O2 Console automatically registers them. |
| 10 | +- **Reflection-Based Command Registration**: O2 Console scans and registers commands from methods, properties, and fields (both public and private) at runtime. |
| 11 | +- **Dynamic Command Execution**: Execute commands in-game directly through the console interface. |
| 12 | +- **Static and Instance Methods**: Supports executing both static and instance methods. |
| 13 | +- **Real-Time Object Discovery**: Automatically detects and interacts with scene objects during gameplay. |
| 14 | +- **Supports Parameter Passing**: Command methods can accept parameters, and the console automatically converts inputs to match method signatures. |
| 15 | +- **Exception Handling**: Automatically catches and logs exceptions from commands and the console, without crashing or pausing the game. |
| 16 | +- **Built-in Commands**: Includes commands like `/Clear` to clear console logs, with additional built-in functionality to interact with the game. |
| 17 | + |
| 18 | +--- |
| 19 | + |
| 20 | +## Getting Started |
| 21 | + |
| 22 | +### Installation |
| 23 | + |
| 24 | +1. Clone or download the **O2 Console** repository or download the unity package. |
| 25 | +2. Import the O2 Console scripts into your Unity project. |
| 26 | +3. **Set up the Console UI**: The project includes a ready-to-use prefab for the console. Simply drag and drop the prefab into your scene to integrate the in-game console with an input field and log display. |
| 27 | + |
| 28 | +4. Optionally, configure a hotkey to toggle the console (e.g., `~` key). |
| 29 | + |
| 30 | +### Usage |
| 31 | + |
| 32 | +To define a console command, simply use the `[ConsoleCommand]` attribute above any static or instance method, as well as public or private properties. The console will automatically register it for in-game use. |
| 33 | + |
| 34 | +```csharp |
| 35 | +// Method Example |
| 36 | +[ConsoleCommand("SomeFunction")] |
| 37 | +static void SomeFunction() |
| 38 | +{ |
| 39 | + // Do Something |
| 40 | +} |
| 41 | + |
| 42 | +// Property Example (Public) |
| 43 | +public class ExampleClass |
| 44 | +{ |
| 45 | + private int _value; |
| 46 | + |
| 47 | + [ConsoleCommand("GetValue")] |
| 48 | + public int Value |
| 49 | + { |
| 50 | + get { return _value; } |
| 51 | + set { _value = value; } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// Property Example (Private) |
| 56 | +public class ExampleClass |
| 57 | +{ |
| 58 | + private int _privateValue; |
| 59 | + |
| 60 | + [ConsoleCommand("GetPrivateValue")] |
| 61 | + private int PrivateValue |
| 62 | + { |
| 63 | + get { return _privateValue; } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// Instance Method Example |
| 68 | +public class ExampleInstance |
| 69 | +{ |
| 70 | + [ConsoleCommand("InstanceMethod")] |
| 71 | + public void InstanceMethod() |
| 72 | + { |
| 73 | + // Do Something |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// Static Property Example |
| 78 | +public class StaticExample |
| 79 | +{ |
| 80 | + private static int _staticValue; |
| 81 | + |
| 82 | + [ConsoleCommand("GetStaticValue")] |
| 83 | + public static int StaticValue |
| 84 | + { |
| 85 | + get { return _staticValue; } |
| 86 | + set { _staticValue = value; } |
| 87 | + } |
| 88 | +} |
| 89 | +``````` |
0 commit comments