Custom texture importer for automatically generating sprites not working

So I was looking for a way to automatically generate a spritesheet from an imported texture. And I found this thread with an example on how to do so. However when trying it out myself using the InternalSpriteUtility.GenerateGridSpriteRectangles() - Method It generates my sprites but the sprites are 22x32 instead of 32x32 as in the metadata spritesheet.

In the console the SpriteMetaData rects are 32x32 in the spritesheet, but when postprocessing the sprites they’re 22x32.

Any help is appreciated!

public class OverWorldSpriteAnimationImporter : AssetPostprocessor
{
    static string animationFolder = "Assets/Overworlds/Overworld Animations";
   
    public void OnPreprocessTexture()
    {
        if (assetPath.Contains("Overworlds/Spritesheets"))
        {
            if (!AssetDatabase.IsValidFolder(animationFolder))
            {
                Debug.Log("Created folder: " + animationFolder);
                AssetDatabase.CreateFolder("Assets/Overworlds", "Overworld Animations");
            }
            Debug.Log("OnPreprocesstexture overwriting defaults...");

            TextureImporter importer = assetImporter as TextureImporter;
            importer.textureType = TextureImporterType.Sprite;

            importer.spriteImportMode = SpriteImportMode.Multiple;
            importer.spritePixelsPerUnit = 32;
            importer.alphaSource = TextureImporterAlphaSource.FromInput;
            importer.wrapMode = TextureWrapMode.Repeat;
           

            importer.mipmapEnabled = true;
            importer.spriteBorder = new Vector2(32, 32);
           

            importer.textureCompression = TextureImporterCompression.Uncompressed;
        }
    }

    public void OnPostprocessTexture(Texture2D texture)
    {
        if (assetPath.Contains("Overworlds/Spritesheets"))
        {
            TextureImporter importer = assetImporter as TextureImporter;
            if (importer.spriteImportMode != SpriteImportMode.Multiple)
                return;

            Debug.Log("OnPostprocessTexture generating sprites...");

            Vector2 spriteSize = new Vector2(32, 32);
            Rect[] rects = InternalSpriteUtility.GenerateGridSpriteRectangles(texture, Vector2.zero, spriteSize, Vector2.zero);
            List<SpriteMetaData> metas = new List<SpriteMetaData>();
            string fileNameNoExtension = Path.GetFileNameWithoutExtension(assetPath);


            for (int i = 0; i < rects.Length; i++)
            {
                SpriteMetaData meta = new SpriteMetaData();
                meta.rect = rects[i];
                meta.name = fileNameNoExtension + "_" + i;
                metas.Add(meta);
            }

           
            importer.spritesheet = metas.ToArray();

            Debug.Log("Meta rects:");
            for (int i = 0; i < importer.spritesheet.Length; i++)
            {
                Debug.Log(importer.spritesheet[i].rect);
            }
           
           
        }
    }

    public void OnPprocessSprites(Texture2D texture, Sprite[] sprites)
    {
        Debug.Log("Sprite rects:");
        for (int i = 0; i < sprites.Length; i++)
        {
            Debug.Log(sprites[i].rect);
        }
    }

I’ve been messing with the InternalSpriteUtility.GenerateGridSpriteRectangles() method since yesterday and found some issues myself. Most of the time it was working fine, but sometimes it wouldnt. After a lot of head-scratching I figured out that the texture’s Max-Size (default 2048), was actually smaller than the source texture’s original dimentions, which was 2560x1568.
Therefore, the texture was already being scaled down and caused my target of 32x32 to fail, since the height wasnt even divisable by 32 anymore.
I tried calculating the ratio between source and imported texture-resolution and passing that in, but that didnt work. The only solution (which is very slow), was to detect texture-resolutions that are larger than the default Max-Size and double the max-size, apply and then try to slice again.
Here is my approach:

private static void ToMultipleBySize(string path, int width, int height)
{
var texture = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
if (AssetImporter.GetAtPath(path) is not TextureImporter importer)
return;

importer.GetSourceTextureWidthAndHeight(out var sourceWidth, out var sourceHeight);
if (sourceWidth > texture.width || sourceHeight > texture.height)
{
Debug.Log($"Current: [{texture.width}x{texture.height}]");
Debug.Log($"Source: [{sourceWidth}x{sourceHeight}]");
Debug.Log($"CHANGED MAX-SIZE! {importer.maxTextureSize} -> {importer.maxTextureSize * 2}");
// VERY SLOW :(
importer.maxTextureSize *= 2; 
importer.SaveAndReimport();
AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
}

importer.isReadable = true;
importer.spritePixelsPerUnit = width;
importer.textureType = TextureImporterType.Sprite;
importer.spriteImportMode = texture.width == width && texture.height == height 
? SpriteImportMode.Single 
: SpriteImportMode.Multiple;

var textureSettings = new TextureImporterSettings();
importer.ReadTextureSettings(textureSettings);
textureSettings.spriteMeshType = SpriteMeshType.Tight;
importer.SetTextureSettings(textureSettings);

var rects = InternalSpriteUtility.GenerateGridSpriteRectangles(texture, Vector2.zero, new Vector2(width, height), Vector2.zero, false);
var count = 0;
importer.spritesheet= rects.Select(rect => new SpriteMetaData
{
pivot = new Vector2(0.5f, 0.5f),
alignment = (int)SpriteAlignment.Center,
rect = rect,
name = $"{texture.name}_{count++}"
}).ToArray();
importer.SaveAndReimport();

AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
}