Skip to main content
Glama

export_to_unity

Export AI-generated 2D assets directly to Unity projects by specifying asset name, type, and folder structure for immediate use in game development.

Instructions

Export a generated asset directly to a Unity project.

Args:
    image_base64: Base64 encoded image to export
    asset_name: Name for the asset file (without extension)
    asset_type: Unity folder type (Sprites, Textures, UI, etc.)
    subfolder: Optional subfolder within the asset type folder

Returns:
    JSON with export status and file path

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
image_base64Yes
asset_nameYes
asset_typeNoSprites
subfolderNo

Implementation Reference

  • The main handler function for the 'export_to_unity' tool, decorated with @mcp.tool() for registration. It decodes the base64 image, validates the Unity assets directory from env var UNITY_ASSETS_DIR, ensures the target directory exists, writes the PNG file, and returns JSON with success status and file path.
    async def export_to_unity(
        image_base64: str,
        asset_name: str,
        asset_type: str = "Sprites",
        subfolder: str = ""
    ) -> str:
        """Export a generated asset directly to a Unity project.
        
        Args:
            image_base64: Base64 encoded image to export
            asset_name: Name for the asset file (without extension)
            asset_type: Unity folder type (Sprites, Textures, UI, etc.)
            subfolder: Optional subfolder within the asset type folder
        
        Returns:
            JSON with export status and file path
        """
        if not UNITY_ASSETS_DIR:
            return json.dumps({
                "success": False,
                "error": "UNITY_ASSETS_DIR environment variable not set"
            }, indent=2)
        
        unity_path = Path(UNITY_ASSETS_DIR)
        if not unity_path.exists():
            return json.dumps({
                "success": False,
                "error": f"Unity assets directory not found: {UNITY_ASSETS_DIR}"
            }, indent=2)
        
        # Build target path
        target_dir = unity_path / asset_type
        if subfolder:
            target_dir = target_dir / subfolder
        
        # Validate path is within allowed roots
        if ALLOW_WRITE_ROOTS and ALLOW_WRITE_ROOTS[0]:
            if not validate_path(str(target_dir), ALLOW_WRITE_ROOTS):
                return json.dumps({
                    "success": False,
                    "error": "Target path not in allowed write roots"
                }, indent=2)
        
        ensure_directory(target_dir)
        
        import base64
        image_bytes = base64.b64decode(image_base64)
        
        file_path = target_dir / f"{asset_name}.png"
        file_path.write_bytes(image_bytes)
        
        return json.dumps({
            "success": True,
            "file_path": str(file_path),
            "asset_name": asset_name,
            "asset_type": asset_type
        }, indent=2)

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/tuannguyen14/ComfyAI-MCP-GameAssets'

If you have feedback or need assistance with the MCP directory API, please join our Discord server