
unity mcp
通过TCP连接控制Unity场景的简单强大工具,支持外部程序化操作。
Repository Info
About This Server
通过TCP连接控制Unity场景的简单强大工具,支持外部程序化操作。
Model Context Protocol (MCP) - This server can be integrated with AI applications to provide additional context and capabilities, enabling enhanced AI interactions and functionality.
Documentation
Unity MCP (Model Control Protocol)
A simple yet powerful protocol for controlling Unity scenes through a TCP connection. This open-source project enables external applications to create, manipulate, and control Unity objects programmatically.
Features
- Create and manipulate 3D objects in Unity scenes
- Apply materials and configure lighting
- Create detailed characters with customization options
- Add particle effects and physics properties
- Control cameras and audio
- Execute custom C# code in Unity
- Simple JSON-RPC communication protocol
Components
- UnityMCPServer.cs - Unity component that creates a TCP server and handles commands
- unity_mcp_client.py - Python client for communicating with the Unity server
- Examples - Demo scenes showing different features and capabilities
Detailed Setup Guide
Unity Setup
- Create a new Unity project or open your existing project
- Copy the
unitymcpserver.csfile into your project's Assets folder- You can create a new folder like
Assets/Scripts/to keep things organized
- You can create a new folder like
- In your Unity scene, create an empty GameObject
- GameObject → Create Empty
- Rename it to "MCPServer"
- Attach the
UnityMCPServercomponent to the GameObject- Select the GameObject in the Hierarchy
- Click "Add Component" in the Inspector
- Type "UnityMCPServer" and select it
- Configure settings in the Inspector (optional)
- Port: Default is 8080, change if needed
- Make sure this GameObject is included in all scenes where you want to use MCP
- Consider making it a Prefab for easy reuse
- You can also use DontDestroyOnLoad to keep it persistent between scenes
Important: Starting Unity Before Using MCP
The UnityMCPServer only accepts connections when Unity is in Play mode:
- Open your Unity project with the UnityMCPServer component attached
- Press the Play button to enter Play mode
- Now the server is active and accepting connections
- Keep Unity open and in Play mode while using Claude or your Python client
Python Client Setup
-
Ensure you have Python 3.6+ installed
-
Copy the
unity_mcp_client.pyfile to your project -
Install required packages:
pip install uuid -
For a proper installation, you can use the included setup.py:
pip install -e .Note: Before publishing to PyPI or sharing, be sure to update the URL in setup.py to your actual repository URL.
Connecting with Claude or Other AI Tools
When working with Claude or other AI assistants that use the Unity MCP:
- Start your Unity project and enter Play mode first
- Make sure the "MCPServer" GameObject is in your scene
- Keep Unity running while interacting with the AI
- The AI will connect to Unity through the MCP protocol
- If the connection fails, check that:
- Unity is in Play mode
- The MCPServer component is active
- The port settings match (default: 8080)
- No firewall is blocking the connection
Usage
Using the Python Client
from unity_mcp_client import UnityMCPClient, Colors
# Create client connection
client = UnityMCPClient(host="localhost", port=8080)
# Create a simple cube
cube = client.create_object("CUBE", "MyCube", [0, 1, 0])
# Apply a material
client.set_material("MyCube", color=Colors.BLUE)
# Add physics
client.add_rigidbody("MyCube", mass=2.0)
# Create a light
client.create_light("point", [5, 5, 5], intensity=2.0, color=Colors.rgb(255, 220, 150))
# Create a character
client.create_improved_character("human", [0, 0, 3], "Formal")
Full Scene Creation Example
Here's how to create a complete scene:
from unity_mcp_client import UnityMCPClient, Colors, CharacterPresets
# Create client
client = UnityMCPClient()
# Create environment
floor = client.create_object("PLANE", "Floor", [0, 0, 0], [0, 0, 0], [20, 1, 20])
client.set_material("Floor", "FloorMaterial", Colors.rgb(30, 30, 30))
# Create walls
wall1 = client.create_object("CUBE", "Wall1", [0, 2, 10], [0, 0, 0], [20, 4, 0.5])
client.set_material("Wall1", color=Colors.rgb(200, 200, 200))
wall2 = client.create_object("CUBE", "Wall2", [0, 2, -10], [0, 0, 0], [20, 4, 0.5])
client.set_material("Wall2", color=Colors.rgb(200, 200, 200))
# Lighting
main_light = client.create_light("directional", [5, 10, 5], 1.0, Colors.rgb(255, 245, 230))
accent_light = client.create_light("point", [-5, 2, 5], 2.0, Colors.rgb(255, 150, 100))
# Create characters
character = client.create_improved_character("human", [0, 0, 0], "Casual")
# Set up camera
camera = client.create_camera("mainCamera", [0, 5, -10], None, 60)
client.set_active_camera("mainCamera")
Running the Included Examples
We've included ready-to-use examples in the examples/ directory:
- Make sure Unity is running with the UnityMCPServer active
- Run an example script:
python examples/basic_scene.py - Watch as the scene is created in real-time in Unity
Troubleshooting
Unity Side
- Server not starting: Make sure the UnityMCPServer component is attached to an active GameObject
- Connection refused: Check if Unity is in Play mode
- Objects not appearing: Check your scene's camera position, objects might be created out of view
Python Side
- Connection error: Make sure Unity is running and in Play mode
- Name conflicts: If creating objects fails, the name might already be taken. Use unique names
- Parameter errors: Check the parameter types and formats (arrays for positions, etc.)
API Reference
Basic Objects
create_object(obj_type, name, location, rotation, scale)- Create a 3D primitivemodify_object(name, location, rotation, scale, visible)- Update object propertiesdelete_object(name)- Remove an object from the sceneset_material(object_name, material_name, color)- Apply a material to an object
Characters
create_improved_character(character_type, position, outfit_type, has_weapon, weapon_type, ...)- Create a detailed character
Environment
create_terrain(width, length, height, heightmap)- Create a terraincreate_water(width, length, height)- Create a water surfacecreate_skybox(sky_type, color)- Create a skybox
Lighting & Effects
create_light(light_type, position, intensity, color, ...)- Create a lightcreate_particle_system(effect_type, position, scale, ...)- Create particle effects
Physics
add_rigidbody(object_name, mass, use_gravity)- Add physics to an objectapply_force(object_name, force, mode)- Apply physics forces
Camera & Audio
create_camera(camera_type, position, target, field_of_view, ...)- Create a cameraset_active_camera(camera_name)- Set the active cameraplay_sound(sound_type, position, volume)- Play a sound effectcreate_audio_source(object_name, audio_type, loop, volume, ...)- Add an audio source
Utilities
get_scene_info()- Get information about the current sceneget_object_info(object_name)- Get information about a specific objectexecute_unity_code(code)- Execute custom C# code in Unity
Helper Classes
Colors
Predefined colors and utilities for working with colors:
# Using predefined colors
Colors.RED # [1.0, 0.0, 0.0, 1.0]
Colors.BLUE # [0.0, 0.0, 1.0, 1.0]
Colors.GREEN # [0.0, 1.0, 0.0, 1.0]
# Using RGB (0-255 range)
Colors.rgb(255, 100, 50) # Converts to [1.0, 0.39, 0.19, 1.0]
# Interpolating between colors
Colors.lerp(Colors.RED, Colors.BLUE, 0.5) # Purple: [0.5, 0.0, 0.5, 1.0]
Character Presets
Predefined character configurations:
# Using a preset
gunman = CharacterPresets.GUNMAN.copy()
gunman["position"] = [0, 0, 5]
client.create_improved_character(**gunman)
Material Presets
Predefined material configurations:
# Using a material preset
metal = MaterialPresets.METAL.copy()
metal["albedoColor"] = Colors.GOLD
# Use with advanced material API
Using with LLMs (Like Claude)
When using UnityMCP with Large Language Models (LLMs) like Claude:
- Start Unity and make sure the UnityMCPServer is active (in Play mode)
- In your conversation with the LLM, explain that Unity is running with the MCP server
- The LLM can generate Python code using the unity_mcp_client to create scenes
- Execute the generated code to see the results in Unity
- Share feedback with the LLM to refine the scene
Example workflow:
- Ask Claude: "Create a simple village scene in Unity"
- Claude generates Python code using unity_mcp_client
- Run the code while Unity is in Play mode
- Tell Claude: "The houses are too large, make them smaller"
- Claude generates updated code that modifies the scene
Screenshots and Documentation
Note to Repo Owner: Consider adding screenshots of examples created with UnityMCP to make the README more visual. You can add images to the
images/directory and reference them in this README with Markdown syntax:!Description
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Quick Start
Clone the repository
git clone https://github.com/tanisss46/unity-mcpInstall dependencies
cd unity-mcp
npm installFollow the documentation
Check the repository's README.md file for specific installation and usage instructions.
Repository Details
Recommended MCP Servers
Discord MCP
Enable AI assistants to seamlessly interact with Discord servers, channels, and messages.
Knit MCP
Connect AI agents to 200+ SaaS applications and automate workflows.
Apify MCP Server
Deploy and interact with Apify actors for web scraping and data extraction.
BrowserStack MCP
BrowserStack MCP Server for automated testing across multiple browsers.
Zapier MCP
A Zapier server that provides automation capabilities for various apps.