{"record":{"id":"b814dea019875a32","repo":"Unity-Technologies/UnityCsReference","slug":"may-only-be-called-in-onpostprocesstexture","errorCode":null,"errorMessage":"May only be called in OnPostProcessTexture","messagePattern":"May only be called in OnPostProcessTexture","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/AssetPipeline/TextureImporter.bindings.cs","lineNumber":428,"sourceCode":"        internal bool IsSourceTextureHDR()\n        {\n            return GetSourceTextureInformation().hdr;\n        }\n\n        private extern SourceTextureInformation GetSourceTextureInformation();\n\n        [FreeFunction(\"IsCompressedETCTextureFormat\")]\n        internal static extern  bool IsTextureFormatETC1Compression(TextureFormat fmt);\n\n        [FreeFunction(\"IsBuildTargetETC\")]\n        internal static extern  bool IsETC1SupportedByBuildTarget(BuildTarget target);\n\n        // Does textures source image have alpha channel.\n        public bool DoesSourceTextureHaveAlpha()\n        {\n            var info = GetSourceTextureInformation();\n            if (info.width == -1)\n                throw new ArgumentException(\"May only be called in OnPostProcessTexture\");\n\n            return info.containsAlpha;\n        }\n\n        // Does textures source image have RGB channels.\n        [System.Obsolete(\"DoesSourceTextureHaveColor always returns true in Unity.\")]\n        public bool DoesSourceTextureHaveColor()\n        {\n            return true;\n        }\n\n        // Which type of texture are we dealing with here\n        public extern TextureImporterType textureType { get; set; }\n        public extern TextureImporterShape textureShape { get; set; }\n\n        // Read texture settings into [[TextureImporterSettings]] class.\n        public void ReadTextureSettings(TextureImporterSettings dest)\n        {","sourceCodeStart":410,"sourceCodeEnd":446,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/AssetPipeline/TextureImporter.bindings.cs#L410-L446","documentation":"TextureImporter.DoesSourceTextureHaveAlpha reads SourceTextureInformation.containsAlpha, but like the dimensions API it is only valid once the source has been decoded. The native side reports width == -1 before that, and the method throws ArgumentException (not InvalidOperationException) with a hint that the only valid call site is OnPostProcessTexture. The misleading exception type is a known quirk of this particular guard.","triggerScenarios":"Calling DoesSourceTextureHaveAlpha from OnPreprocessAsset or any code path before the texture is decoded; calling on an importer that has not finished importing.","commonSituations":"Pre-import logic trying to pick alpha-handling settings from source alpha; editor scripts querying alpha outside the postprocess hook.","solutions":["Call DoesSourceTextureHaveAlpha only from AssetPostprocessor.OnPostprocessTexture (or after import finishes).","In pre-import hooks, derive alpha expectations from the importer settings or format instead of the source image.","If you need source alpha earlier, decode the file yourself or restructure so the decision is made post-import."],"exampleFix":"// before (pre-import)\nbool alpha = importer.DoesSourceTextureHaveAlpha();\n\n// after (post-import hook)\npublic void OnPostprocessTexture(Texture2D tex) {\n    bool alpha = tex.format == TextureFormat.Alpha8 || /* ... */;\n    // or call importer.DoesSourceTextureHaveAlpha() here, where it is valid\n}","handlingStrategy":"validation","validationCode":"// Only valid inside OnPostprocessTexture.\npublic void OnPostprocessTexture(Texture2D tex) {\n    bool alpha = (tex.format == TextureFormat.Alpha8) || /* format check */;\n    // use 'alpha' instead of importer.DoesSourceTextureHaveAlpha()\n}","typeGuard":null,"tryCatchPattern":"try {\n    bool alpha = importer.DoesSourceTextureHaveAlpha();\n} catch (System.ArgumentException) {\n    // not yet decoded; defer to OnPostprocessTexture\n}","preventionTips":["Reserve DoesSourceTextureHaveAlpha for OnPostprocessTexture.","Make alpha-dependent decisions from importer settings or the decoded Texture2D.","Document which importer queries are valid at each pipeline hook."],"tags":["unity","texture-importer","pipeline-timing","argument-validation"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}