|
| 1 | +# Unity - CVar |
| 2 | + |
| 3 | +[CVar](https://github.com/DerKekser/unity-cvar) provides a flexible way to create console variables in Unity projects. With this system, you can mark fields, properties, and methods for console access using a simple attribute. |
| 4 | + |
| 5 | +## Contents |
| 6 | +- [Simple Example](#simple-example) |
| 7 | +- [Supported Types](#supported-types) |
| 8 | +- [Static Members](#static-members) |
| 9 | +- [Methods](#methods) |
| 10 | +- [Manual Registration](#manual-registration) |
| 11 | +- [Console](#console) |
| 12 | + - [Creation](#creation) |
| 13 | + - [Commands](#commands) |
| 14 | +- [Install](#install) |
| 15 | + - [Install via Unity Package](#install-via-unity-package) |
| 16 | + - [Install via git URL](#install-via-git-url) |
| 17 | +- [License](#license) |
| 18 | + |
| 19 | +### Simple Example |
| 20 | + |
| 21 | +Define a console variable by adding the `CVar` attribute to a field, property, or method: |
| 22 | + |
| 23 | +```csharp |
| 24 | +using Kekser.UnityCVar; |
| 25 | +using UnityEngine; |
| 26 | + |
| 27 | +public class PlayerSettings : MonoBehaviour |
| 28 | +{ |
| 29 | + [CVar("player_speed")] |
| 30 | + private float _speed = 5.0f; |
| 31 | + |
| 32 | + [CVar("player_health", "Sets the player's health")] |
| 33 | + public int Health { get; private set; } = 100; |
| 34 | +} |
| 35 | +``` |
| 36 | +### Supported Types |
| 37 | + |
| 38 | +The system supports various types by default, including: |
| 39 | +- Basic types (int, float, bool, string) |
| 40 | +- Unity types (Vector2, Vector3, Vector4, Quaternion) |
| 41 | +### Creation |
| 42 | + |
| 43 | +### Static Members |
| 44 | + |
| 45 | +You can also mark static fields, properties, and methods as CVars: |
| 46 | + |
| 47 | +```csharp |
| 48 | +using Kekser.UnityCVar; |
| 49 | + |
| 50 | +public static class GameSettings |
| 51 | +{ |
| 52 | + [CVar("game_difficulty")] |
| 53 | + private static int _difficulty = 1; |
| 54 | + |
| 55 | + [CVar("game_debug_mode")] |
| 56 | + private static bool DebugMode { get; set; } = false; |
| 57 | +} |
| 58 | +``` |
| 59 | +### Methods |
| 60 | + |
| 61 | +Methods can also be marked with the CVar attribute: |
| 62 | + |
| 63 | +```csharp |
| 64 | +using Kekser.UnityCVar; |
| 65 | +using UnityEngine; |
| 66 | + |
| 67 | +public class GameController : MonoBehaviour |
| 68 | +{ |
| 69 | + [CVar("restart_game")] |
| 70 | + private void RestartGame() |
| 71 | + { |
| 72 | + // Restart game |
| 73 | + } |
| 74 | + |
| 75 | + [CVar("add_score")] |
| 76 | + private void AddScore(int points) |
| 77 | + { |
| 78 | + // Add points |
| 79 | + } |
| 80 | +} |
| 81 | +``` |
| 82 | +### Manual Registration |
| 83 | + |
| 84 | +You can manually register CVars by calling the `CVarAttributeCache.RegisterCVar` method: |
| 85 | + |
| 86 | +```csharp |
| 87 | +CVarAttributeCache.RegisterCVar( |
| 88 | + "go_setactive", |
| 89 | + "Enables or disables the current target GameObject (true/false)", |
| 90 | + typeof(GameObject), |
| 91 | + "SetActive" |
| 92 | +); |
| 93 | +``` |
| 94 | +### Console |
| 95 | + |
| 96 | +#### Creation |
| 97 | + |
| 98 | +You can create a console by dragging the `Console_Prefab` prefab from the `UnityCVarConsole` folder into your scene. |
| 99 | + |
| 100 | +#### Commands |
| 101 | + |
| 102 | +To view all available commands, type `con_list` in the console. You can also use `con_list <filter>` to filter the results. |
| 103 | +### Install |
| 104 | + |
| 105 | +#### Install via Unity Package |
| 106 | + |
| 107 | +Download the latest [release](https://github.com/DerKekser/unity-cvar/releases) and import the package into your Unity project. |
| 108 | +#### Install via git URL |
| 109 | + |
| 110 | +You can add this package to your project by adding these git URLs in the Package Manager: |
| 111 | +``` |
| 112 | +https://github.com/DerKekser/unity-cvar.git?path=Assets/Kekser/UnityCVar |
| 113 | +https://github.com/DerKekser/unity-cvar.git?path=Assets/Kekser/UnityCVarConsole |
| 114 | +``` |
| 115 | + |
| 116 | +### License |
| 117 | + |
| 118 | +This library is under the MIT License. |
0 commit comments