CoplayDev/unity-mcp · error · ArgumentException

Native width should be larger than 0

Error message

Native width should be larger than 0

What it means

Thrown by CustomPreviewGenerator.Validate() (CustomPreviewGenerator.cs:39) when CustomPreviewGenerationSettings.NativeWidth <= 0. NativeWidth is the native render resolution (before downscale) passed to SceneScreenshotterSettings.NativeWidth for material/model/prefab screenshots. A non-positive native width makes the offscreen render undefined. Runs before GenerateImpl(), outside its try/catch — an uncaught ArgumentException.

Source

Thrown at TestProjects/AssetStoreUploads/Packages/com.unity.asset-store-tools/Editor/Previews/Scripts/Generators/CustomPreviewGenerator.cs:39

        {
            _customSettings = settings;
        }

        protected override void Validate()
        {
            base.Validate();

            if (_customSettings.Width <= 0)
                throw new ArgumentException("Width should be larger than 0");

            if (_customSettings.Height <= 0)
                throw new ArgumentException("Height should be larger than 0");

            if (_customSettings.Depth <= 0)
                throw new ArgumentException("Depth should be larger than 0");

            if (_customSettings.NativeWidth <= 0)
                throw new ArgumentException("Native width should be larger than 0");

            if (_customSettings.NativeHeight <= 0)
                throw new ArgumentException("Native height should be larger than 0");
        }

        protected override async Task<PreviewGenerationResult> GenerateImpl()
        {
            var result = new PreviewGenerationResult()
            {
                GenerationType = _customSettings.GenerationType
            };

            OnProgressChanged?.Invoke(0f);

            var generatedPreviews = new List<PreviewMetadata>();
            var existingPreviews = GetExistingPreviews();
            var generators = CreateGenerators(existingPreviews);

View on GitHub (pinned to c21bf496bc)

Solutions

  1. Set CustomPreviewGenerationSettings.NativeWidth to a positive value (e.g. 1024) before Generate().
  2. Set NativeWidth/NativeHeight together with Width/Height/Depth at construction time.
  3. Check the Native Width field in the Preview Generator window before generating.

Example fix

// before
var settings = new CustomPreviewGenerationSettings { Width = 256, Height = 128, Depth = 100 /* NativeWidth missing */ };
await new CustomPreviewGenerator(settings).Generate(); // throws

// after
var settings = new CustomPreviewGenerationSettings { Width = 256, Height = 128, Depth = 100, NativeWidth = 1024 };
await new CustomPreviewGenerator(settings).Generate();
Defensive patterns

Strategy: validation

Validate before calling

var s = settings as CustomPreviewGenerationSettings;
if (s != null && s.NativeWidth <= 0)
    throw new InvalidOperationException("CustomPreviewGenerationSettings.NativeWidth must be > 0 before generating previews.");

Type guard

static bool HasValidNativeWidth(CustomPreviewGenerationSettings s) => s != null && s.NativeWidth > 0;

Try / catch

try { await generator.Generate(); }
catch (ArgumentException ex) when (ex.Message.Contains("Native width")) { /* report invalid native width */ }

Prevention

When it happens

Trigger: Generate() called with NativeWidth unset (default 0) or <= 0. Arises when settings are built without the native-resolution fields, or when only audio/texture previews are wanted but the required NativeWidth is left at default.

Common situations: Initializer that sets Width/Height but omits NativeWidth; confusion between Width (output) and NativeWidth (offscreen render size) leading to the native fields being skipped.

Related errors


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