Skip to content
This repository was archived by the owner on Nov 21, 2026. It is now read-only.

Latest commit

 

History

History
332 lines (250 loc) · 11.8 KB

README.md

File metadata and controls

332 lines (250 loc) · 11.8 KB

Made with Claude

unity-cli

CLI tool for controlling Unity Editor via AgentBridge HTTP Server. Designed for AI agents (Claude Code, Cursor, etc.) and human developers to automate Unity workflows from the terminal.

Note: This repository was auto-sanitized from an internal project. Some Lua code snippets contain placeholder symbols (e.g. CHECK_VIEW_FUNC, BATTLE_ENGINE, GLOBAL_FIRE_EVENT) that need to be replaced with your project's actual API. The data/example_hooks.yaml shows how to configure project-specific hooks. Using an AI assistant to search-and-replace these placeholders is the quickest way to adapt this to your codebase.

Why?

  • Typing raw HTTP/MCP commands is painful
  • Unity crashes and you have to restart manually
  • sleep 30 wastes time when the editor might be ready in 5 seconds
  • AI agents need structured, scriptable access to Unity

unity-cli fixes all of these.

Install

pip install -e .

Requires Python 3.9+ and a Unity project with AgentBridge installed (see deploy command).

Quick Start

unity-cli status              # Check connection
unity-cli doctor              # Full health check
unity-cli play --wait         # Enter PlayMode, wait until ready
unity-cli find "Main Camera"  # Search GameObjects by name
unity-cli screenshot          # Capture game view
unity-cli ui snapshot         # Structured UI snapshot (for AI analysis)
unity-cli ui click "Button"   # Click a UI button by name
unity-cli go inspect "Player" # List components on a GameObject
unity-cli scene active        # Show current scene
unity-cli log -e              # Show error logs

Commands

Connection & Status

unity-cli status                    # Bridge connection + editor state
unity-cli doctor [--project PATH]   # Full diagnostic (bridge, process, editor state)
unity-cli instances                 # List all running Unity bridge instances

PlayMode Control

unity-cli play [--wait] [-t 30]     # Enter PlayMode, optionally wait for reconnection
unity-cli stop                      # Exit PlayMode, return to EditMode
unity-cli pause                     # Pause PlayMode (freeze game time)

Smart Waiting

unity-cli wait [-t 60]              # Wait for bridge to become available
unity-cli compile [--wait]          # Trigger compilation, optionally wait

Process Management

unity-cli launch PATH [--wait]      # Launch Unity with project, wait for bridge
unity-cli restart PATH [-t 120]     # Kill + relaunch + wait
unity-cli kill [--project PATH]     # Kill Unity process
unity-cli recover [--exe PATH]      # Auto-detect crash/hang and recover

Code Execution

unity-cli lua "expression"          # Evaluate script expression, print result
unity-cli lua -s "statement"        # Execute script statement (no return)
unity-cli lua -f script.lua         # Execute script file
unity-cli csharp TYPE METHOD [ARGS] # Call C# static method via reflection

Input & Interaction

unity-cli key esc                   # Simulate Escape key press
unity-cli key enter                 # Simulate Enter key press
unity-cli screenshot [-o path]      # Capture game view screenshot
unity-cli menu "Window/General/..." # Execute Unity menu item by path

Find GameObjects

unity-cli find "name"               # Auto-detect mode (PlayMode/EditMode)
unity-cli find "name" --playmode    # Force PlayMode search
unity-cli find "name" --editmode    # Force EditMode search

Logs & Monitoring

unity-cli log                       # Last 10 entries
unity-cli log -e                    # Errors only
unity-cli log -n 50 --grep "error"  # Filter by text
unity-cli watch                     # Continuously monitor logs
unity-cli watch -e                  # Watch errors only
unity-cli watch --status            # Watch editor state changes

Deploy

unity-cli deploy [PROJECT_PATH]     # Inject AgentBridge C# tools into project
unity-cli deploy --check            # Only report, don't deploy

Raw Passthrough

unity-cli raw <tool> '<json>'       # Send raw command to Unity bridge

Sub-Command Groups

unity-cli uses a docker-style sub-command structure for domain-specific operations.

ui — UI Hierarchy & Interaction

unity-cli ui tree [-d 3]            # Show runtime UI hierarchy tree
unity-cli ui tree -s "search"       # Search specific nodes
unity-cli ui elements               # List visible UI elements
unity-cli ui elements --grep "btn"  # Filter elements by name
unity-cli ui snapshot               # Structured snapshot (HTML visualization)
unity-cli ui click "ButtonName"     # Click a UI button by name
unity-cli ui input "FieldName" "text"  # Type text into an input field
unity-cli ui drag element dx dy     # Perform drag gesture

scene — Scene Management

unity-cli scene ls                  # List scene hierarchy
unity-cli scene active              # Show active scene name and path
unity-cli scene load "path"         # Load a scene by asset path
unity-cli scene save                # Save current scene
unity-cli scene create "name"       # Create a new empty scene
unity-cli scene screenshot          # Capture scene view screenshot

go — GameObject Operations

unity-cli go create "name"          # Create a new GameObject
unity-cli go rm "name"              # Delete a GameObject
unity-cli go cp "name"              # Duplicate a GameObject
unity-cli go mv "name" --pos 1,2,3  # Move/rotate/scale/re-parent/rename
unity-cli go inspect "name"         # List all components
unity-cli go set "name" --active 0  # Set properties (active, tag, layer)

component — Component Management

unity-cli component add "obj" Type  # Add a component
unity-cli component rm "obj" Type   # Remove a component
unity-cli component set "obj" ...   # Set component property

asset — Asset Management

unity-cli asset ls "Assets/..."     # Search assets
unity-cli asset info "path"         # Get asset metadata
unity-cli asset create "path"       # Create a new asset
unity-cli asset rm "path"           # Delete an asset
unity-cli asset cp "src" "dst"      # Duplicate an asset
unity-cli asset mv "src" "dst"      # Move/rename an asset
unity-cli asset mkdir "path"        # Create a folder

material — Material Management

unity-cli material create "name"    # Create a material
unity-cli material info "path"      # Get material info
unity-cli material set "path" ...   # Set shader property
unity-cli material color "path" ... # Set material color
unity-cli material assign "obj" ... # Assign material to renderer

prefab — Prefab Management

unity-cli prefab info "path"        # Get prefab metadata
unity-cli prefab tree "path"        # Show prefab hierarchy
unity-cli prefab create "obj"       # Create prefab from scene object
unity-cli prefab preview "path"     # Capture prefab screenshot

anim — Animation Control

unity-cli anim play "obj"           # Play animation
unity-cli anim stop "obj"           # Stop animation
unity-cli anim info "obj"           # Get animator info

texture — Texture Generation

unity-cli texture create "path"     # Create a texture
unity-cli texture noise "path"      # Create noise texture
unity-cli texture gradient "path"   # Create gradient texture

shader — Shader Management

unity-cli shader create "path"      # Create a shader file
unity-cli shader read "path"        # Read shader source
unity-cli shader rm "path"          # Delete a shader

vfx — VFX / Particle System

unity-cli vfx info "obj"            # Get VFX component info
unity-cli vfx set "obj" ...         # Set VFX properties
unity-cli vfx play "obj"            # Play particle system
unity-cli vfx stop "obj"            # Stop particle system

so — ScriptableObject

unity-cli so create "path" Type     # Create a ScriptableObject
unity-cli so modify "path" ...      # Modify a ScriptableObject

Project-Specific Extensions

unity-cli is project-agnostic by default. All game-specific Lua expressions, GM commands, and runtime queries are configured through a hooks.yaml file in the data/ directory.

Setup

cp data/example_hooks.yaml data/hooks.yaml
# Edit hooks.yaml with your project's Lua API

What hooks.yaml configures

Hook Purpose Example placeholder
gm_execute Execute a GM/debug command YourGmSystem:Execute('{cmd}')
restart Restart the game YourApp:Restart()
enter_module Navigate to a feature by name YourEntranceSystem:Enter('{module}')
simulate_key Simulate a key press YourInputSystem:SimulateKey("{key}")
account_info Fetch runtime account fields preamble + field expressions
fast_login Auto-login setup PlayerPrefs writes
top_escape_view Get top popup name (for map goto) ViewManager query
try_close_top Soft-close top popup TryClose call
force_close_top Force-close top popup Close call
last_error Get last error message LogManager query
blocking_popups Popup name substrings to auto-dismiss ["season", "notice", ...]
init_commands GM commands for new account setup ["UnlockAll", "SkipTutorial"]
error_popup_name Error popup GameObject name "error_canvas"
ui_root Root UI GameObject path "Canvas"
canvas_names Canvas names for fingerprinting ["MainCanvas", ...]

State navigation & fingerprinting

The map command group uses a project_roadmap.yaml to define game states (login, lobby, shop, etc.), detection rules, and transitions. Fingerprints (project_fingerprints.yaml) are captured snapshots used for state detection.

unity-cli map ls                    # List defined states
unity-cli map detect                # Detect current state
unity-cli map goto main_hub         # Auto-navigate to a state
unity-cli map snapshot login        # Capture fingerprint for a state

Data files (gitignored, project-specific)

File Purpose
data/hooks.yaml Project Lua hooks (copy from example_hooks.yaml)
data/project_roadmap.yaml State graph definition
data/project_fingerprints.yaml Captured UI fingerprints
data/project_accounts.yaml Saved test accounts

These files contain project-specific data and are excluded from version control by .gitignore.

Environment Variables

Variable Default Description
UNITY_BRIDGE_HOST 127.0.0.1 AgentBridge server host
UNITY_BRIDGE_PORT 18820 AgentBridge server port
UNITY_BRIDGE_TIMEOUT 30 Command timeout (seconds)
UNITY_EXE (auto-detect) Path to Unity.exe

How It Works

unity-cli sends HTTP requests directly to the AgentBridge HTTP server running inside Unity Editor (http://host:port/). AgentBridge is a [InitializeOnLoad] C# class that starts an HTTP listener on port 18820. No external MCP server or Node.js process needed — the bridge runs entirely within Unity.

Commands are dispatched to a CommandRegistry which routes them to the appropriate handler (hierarchy queries, asset operations, etc.). The connection is stateless — if Unity restarts, just unity-cli wait and retry.

Actionable Errors

When commands fail, unity-cli diagnoses the root cause and suggests fixes:

Error: Cannot connect to AgentBridge at 127.0.0.1:18820
Hint: Unity Editor not running or AgentBridge not installed. Use: unity-cli deploy
Error: Bridge timeout after 30s
Hint: Unity is compiling scripts. Wait and retry: unity-cli wait

License

MIT