Unity-Technologies/UnityCsReference · error · InvalidOperationException

The texture has not yet finished importing. This most likely

Error message

The texture has not yet finished importing. This most likely means this method was called in an AssetPostprocessor.OnPreprocessAsset callback.

What it means

TextureImporter.GetSourceTextureWidthAndHeight returns the source image dimensions, but the native SourceTextureInformation is populated lazily during import and reports width == -1 until the texture has actually been decoded. Calling before that point throws InvalidOperationException to prevent callers from acting on the -1 sentinel. The message names the most common cause: invoking it inside AssetPostprocessor.OnPreprocessAsset, which runs before the image is read.

Source

Thrown at Editor/Mono/AssetPipeline/TextureImporter.bindings.cs:404

        [System.Obsolete("Use spritePixelsPerUnit property instead.")]
        public extern float spritePixelsToUnits { get; set; }

        public extern Vector2 spritePivot { get; set; }
        public extern Vector4 spriteBorder { get; set; }

        internal void GetWidthAndHeight(ref int width, ref int height)
        {
            var info = GetSourceTextureInformation();
            width = info.width;
            height = info.height;
        }

        public void GetSourceTextureWidthAndHeight(out int width, out int height)
        {
            var info = GetSourceTextureInformation();
            if (info.width == -1)
                throw new InvalidOperationException("The texture has not yet finished importing. This most likely means this method was called in an AssetPostprocessor.OnPreprocessAsset callback.");

            width = info.width;
            height = info.height;
        }

        internal bool IsSourceTextureHDR()
        {
            return GetSourceTextureInformation().hdr;
        }

        private extern SourceTextureInformation GetSourceTextureInformation();

        [FreeFunction("IsCompressedETCTextureFormat")]
        internal static extern  bool IsTextureFormatETC1Compression(TextureFormat fmt);

        [FreeFunction("IsBuildTargetETC")]
        internal static extern  bool IsETC1SupportedByBuildTarget(BuildTarget target);

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Move the call to a hook that runs after the image is decoded, e.g. AssetPostprocessor.OnPostprocessTexture, or call it after AssetDatabase.ImportAsset completes.
  2. Guard with info.width == -1 detection by first reading via the same condition the API uses, or use an API valid at your pipeline stage.
  3. If you only need dimensions for settings logic, defer that logic to post-import and cache the result.

Example fix

// before (inside OnPreprocessAsset)
importer.GetSourceTextureWidthAndHeight(out int w, out int h);

// after (defer to post-import hook)
public void OnPostprocessTexture(Texture2D tex) {
    int w = tex.width, h = tex.height; // already decoded
    // ...use dimensions here
}
Defensive patterns

Strategy: validation

Validate before calling

// Only valid after the source has been decoded.
public void OnPostprocessTexture(Texture2D tex) {
    int w = tex.width, h = tex.height;
    // use dimensions here instead of GetSourceTextureWidthAndHeight
}

Try / catch

try {
    importer.GetSourceTextureWidthAndHeight(out int w, out int h);
} catch (System.InvalidOperationException) {
    // texture not decoded yet; defer to OnPostprocessTexture
}

Prevention

When it happens

Trigger: Calling GetSourceTextureWidthAndHeight inside OnPreprocessAsset; calling on a freshly created/modified importer whose import has not yet run; calling synchronously right after SetSourceTextureInformation changes without re-importing.

Common situations: Trying to read source dimensions to decide compression settings during pre-import; invoking importer methods from a different pipeline hook than intended; editor scripts that touch the importer outside the import flow.

Related errors


AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13). Data as JSON: /api/errors/2f1ba713852fca3d. Report an issue: GitHub.