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.
Define a console variable by adding the CVar
attribute to a field, property, or method:
using Kekser.UnityCVar;
using UnityEngine;
public class PlayerSettings : MonoBehaviour
{
[CVar("player_speed")]
private float _speed = 5.0f;
[CVar("player_health", "Sets the player's health")]
public int Health { get; private set; } = 100;
}
The system supports various types by default, including:
- Basic types (int, float, bool, string)
- Unity types (Vector2, Vector3, Vector4, Quaternion)
You can also mark static fields, properties, and methods as CVars:
using Kekser.UnityCVar;
public static class GameSettings
{
[CVar("game_difficulty")]
private static int _difficulty = 1;
[CVar("game_debug_mode")]
private static bool DebugMode { get; set; } = false;
}
Methods can also be marked with the CVar attribute:
using Kekser.UnityCVar;
using UnityEngine;
public class GameController : MonoBehaviour
{
[CVar("restart_game")]
private void RestartGame()
{
// Restart game
}
[CVar("add_score")]
private void AddScore(int points)
{
// Add points
}
}
You can manually register CVars by calling the CVarAttributeCache.RegisterCVar
method:
CVarAttributeCache.RegisterCVar(
"go_setactive",
"Enables or disables the current target GameObject (true/false)",
typeof(GameObject),
"SetActive"
);
You can create a console by dragging the Console_Prefab
prefab from the UnityCVarConsole
folder into your scene.
To view all available commands, type con_list
in the console. You can also use con_list <filter>
to filter the results.
Download the latest release and import the package into your Unity project.
You can add this package to your project by adding these git URLs in the Package Manager:
https://github.com/DerKekser/unity-cvar.git?path=Assets/Kekser/UnityCVar
https://github.com/DerKekser/unity-cvar.git?path=Assets/Kekser/UnityCVarConsole
This library is under the MIT License.