Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Editor/CommandRouter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public static object Route(PlaycallerCommand command)
case "screenshot":
return ScreenshotHandler.Handle(command);

case "screenshot_hdr":
return ScreenshotHandler.HandleHDR(command);

case "playmode":
return PlayModeHandler.Handle(command);

Expand Down
75 changes: 75 additions & 0 deletions Editor/Handlers/ScreenshotHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,81 @@ private static Texture2D ResizeTexture(Texture2D source, int targetWidth, int ta

return result;
}

/// <summary>
/// HDR 対応スクリーンショット。
/// Camera.Render() + RenderTextureFormat.DefaultHDR を使用するため、
/// HDR が有効なプロジェクトでも ScreenCapture のようにハングしない。
/// Play Mode / 非 Play Mode どちらでも動作する。
/// </summary>
public static object HandleHDR(PlaycallerCommand command)
{
try
{
int width = 0;
int height = 0;
string filename = null;

if (command.Params != null)
{
width = command.Params["width"]?.ToObject<int>() ?? 0;
height = command.Params["height"]?.ToObject<int>() ?? 0;
filename = command.Params["filename"]?.ToString();
}

Camera camera = Camera.main;
if (camera == null)
{
var cameras = Camera.allCameras;
if (cameras.Length > 0)
camera = cameras[0];
}

if (camera == null)
{
return PlaycallerResponse.Error(command.Id,
"No camera available for screenshot", "NO_CAMERA");
}

int captureWidth = width > 0 ? width : camera.pixelWidth;
int captureHeight = height > 0 ? height : camera.pixelHeight;
captureWidth = Mathf.Clamp(captureWidth, 1, 8192);
captureHeight = Mathf.Clamp(captureHeight, 1, 8192);

var renderTexture = new RenderTexture(captureWidth, captureHeight, 24, RenderTextureFormat.DefaultHDR);
var previousTarget = camera.targetTexture;

camera.targetTexture = renderTexture;
camera.Render();

RenderTexture.active = renderTexture;
var screenshot = new Texture2D(captureWidth, captureHeight, TextureFormat.RGB24, false);
screenshot.ReadPixels(new Rect(0, 0, captureWidth, captureHeight), 0, 0);
screenshot.Apply();

camera.targetTexture = previousTarget;
RenderTexture.active = null;

byte[] imageBytes = screenshot.EncodeToPNG();

UnityEngine.Object.DestroyImmediate(renderTexture);
UnityEngine.Object.DestroyImmediate(screenshot);

if (imageBytes == null || imageBytes.Length == 0)
{
return PlaycallerResponse.Error(command.Id,
"Failed to encode screenshot", "ENCODE_ERROR");
}

string filePath = SaveToFile(imageBytes, filename);
return MakeSuccessResponse(command.Id, filePath, captureWidth, captureHeight);
}
catch (Exception ex)
{
return PlaycallerResponse.Error(command.Id,
$"HDR Screenshot failed: {ex.Message}", "SCREENSHOT_HDR_ERROR");
}
}
}

/// <summary>
Expand Down
7 changes: 6 additions & 1 deletion Editor/PlaycallerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,12 @@ private static async void ProcessCommandAsync(PlaycallerCommand command)
var completed = await Task.WhenAny(asyncResponse, Task.Delay(CommandTimeoutMs));
if (completed != asyncResponse)
{
Debug.LogWarning($"[Playcaller] Command {command?.Type} timed out after {CommandTimeoutMs}ms");
var timeoutMsg = $"[Playcaller] Command {command?.Type} timed out after {CommandTimeoutMs}ms";
if (command?.Type == "screenshot")
{
timeoutMsg += "\nIf HDR is enabled in your project, try using screenshot_hdr instead.";
}
Debug.LogWarning(timeoutMsg);
var errResp = PlaycallerResponse.Error(command?.Id, "Command timed out", "TIMEOUT");
SendFramedMessage(errResp);
return;
Expand Down
37 changes: 37 additions & 0 deletions Server~/src/playcaller/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,43 @@ async def playcaller_screenshot(width: int = 0, height: int = 0, filename: str =
return f"Screenshot failed: {exc}"


# -- screenshot_hdr ---------------------------------------------------------

@mcp.tool()
async def playcaller_screenshot_hdr(width: int = 0, height: int = 0, filename: str = "") -> str:
"""Capture a screenshot using Camera.Render() with HDR-compatible RenderTexture.

Use this instead of playcaller_screenshot when the project has HDR enabled,
which can cause ScreenCapture-based methods to hang.
Works in both Play Mode and Edit Mode.

Returns the file path of the saved PNG screenshot.
width/height: optional resolution in pixels (default: camera pixel size).
filename: optional save path (relative to project root, or absolute). Default: Temp/Playcaller/Screenshots/screenshot.png.
"""
try:
params: dict[str, Any] = {"width": width, "height": height}
if filename:
params["filename"] = filename
result = await unity.send_command("screenshot_hdr", params)
unity.last_screenshot_width = result["width"]
unity.last_screenshot_height = result["height"]
unity.last_screen_width = result.get("screenWidth", result["width"])
unity.last_screen_height = result.get("screenHeight", result["height"])

file_path = result["filePath"]
return (
f"Screenshot saved: {file_path}\n"
f"Size: {result['width']}x{result['height']} "
f"(screen: {unity.last_screen_width}x{unity.last_screen_height}).\n"
f"Input coordinates for tap/drag/flick should use the screenshot image coordinate system "
f"({result['width']}x{result['height']}, top-left origin, Y-down).\n"
f"Use the Read tool to view the image file."
)
except Exception as exc:
return f"HDR Screenshot failed: {exc}"


# -- tap --------------------------------------------------------------------

@mcp.tool()
Expand Down