CoplayDev/unity-mcp · warning · IOException

Could not generate a unique screenshot filename for '{fullPa

Error message

Could not generate a unique screenshot filename for '{fullPath}'.

What it means

After capturing a screenshot, the utility searches for a non-existing filename by appending '-1' through '-9999' to the base name in the target directory. If all 9999 candidates already exist on disk, it gives up and throws IOException. This bounds disk usage from repeated captures to the same path.

Source

Thrown at MCPForUnity/Editor/Helpers/EditorWindowScreenshotUtility.cs:416

        }

        private static string EnsureUnique(string fullPath)
        {
            if (!File.Exists(fullPath))
                return fullPath;

            string directory = Path.GetDirectoryName(fullPath) ?? string.Empty;
            string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fullPath);
            string extension = Path.GetExtension(fullPath);

            for (int i = 1; i < 10000; i++)
            {
                string candidate = Path.Combine(directory, $"{fileNameWithoutExtension}-{i}{extension}");
                if (!File.Exists(candidate))
                    return candidate;
            }

            throw new IOException($"Could not generate a unique screenshot filename for '{fullPath}'.");
        }

        private static void DestroyTexture(Texture2D texture)
        {
            if (texture == null)
                return;

            UnityEngine.Object.DestroyImmediate(texture);
        }
    }
}

View on GitHub (pinned to c21bf496bc)

Solutions

  1. Use a unique base filename or output directory per capture (e.g. embed a timestamp or GUID in the base name).
  2. Clear or rotate old screenshots from the target directory before capturing.
  3. Point the screenshot output path at a fresh/empty folder.

Example fix

// before
captureScreenshot(basePath: "Assets/Shots/scene.png"); // fixed name, collides after many runs

// after
string stamp = DateTime.Now.ToString("yyyyMMdd_HHmmss_fff");
captureScreenshot(basePath: $"Assets/Shots/scene_{stamp}.png");
Defensive patterns

Strategy: validation

Validate before calling

// Before capturing, ensure the target dir has headroom for new candidates.
string dir = Path.GetDirectoryName(fullPath);
string baseName = Path.GetFileNameWithoutExtension(fullPath);
string ext = Path.GetExtension(fullPath);
int used = Directory.Exists(dir)
    ? Directory.GetFiles(dir, $"{baseName}-*{ext}").Length
    : 0;
if (used >= 9999)
    throw new InvalidOperationException($"Screenshot slot exhausted in {dir}; clean up or use a unique name.");

Try / catch

try { return CaptureScreenshot(path); }
catch (IOException ex) when (ex.Message.Contains("unique screenshot filename"))
{
    // Retry once with a timestamped base name in a fresh directory.
    string stamp = DateTime.Now.ToString("yyyyMMdd_HHmmss_fff");
    string alt = Path.Combine(Path.GetDirectoryName(path), $"shot_{stamp}{Path.GetExtension(path)}");
    return CaptureScreenshot(alt);
}

Prevention

When it happens

Trigger: Calling the screenshot capture API thousands of times to the same directory with the same base filename, or pointing the output path at a folder already holding name-1.png through name-9999.png. The loop iterates 'for (int i = 1; i < 10000; i++)' and returns the first non-existent candidate; reaching 10000 means every one existed.

Common situations: Automated/looping screenshot capture with no cleanup; test suites that hammer the same path; a stale output directory that was never cleared; long-running editor sessions accumulating screenshots.

Related errors


AI-assisted analysis of CoplayDev/unity-mcp@c21bf496bc (2026-08-13). Data as JSON: /api/errors/52c7b81d610cb1b6. Report an issue: GitHub.