[SpriteMeshPro] Creating TMP_SpriteAsset through code. Material Issue.

Hello. I'm trying creating TMP_SpriteAsset by code, and I'm facing only one problem with my code. The material field shows "Type mismatch".

9452018--1327178--upload_2023-11-5_22-49-54.png

The code to create this asset file is from this function:

    private void GenerateTMPSprite(string spriteAssetPath)
    {
        Sprite spritesheet = AssetDatabase.LoadAssetAtPath<Sprite>(spriteAssetPath);
        Sprite[] sprites = AssetDatabase.LoadAllAssetsAtPath(spriteAssetPath)
            .OfType<Sprite>().ToArray();

        TMP_SpriteAsset spriteAsset = CreateInstance<TMP_SpriteAsset>();
        spriteAsset.spriteSheet = spritesheet.texture;

        spriteAsset.spriteInfoList = new List<TMP_Sprite>();
        spriteAsset.material = new Material(Shader.Find("TextMeshPro/Sprite"));


        for (int i = 0; i < sprites.Length; i++)
        {
            Sprite sprite = sprites[i];
            TMP_Sprite spriteData = new()
            {
                id = i,
                sprite = sprite,
                name = sprite.name,
                x = sprite.textureRect.x,
                y = sprite.textureRect.y,
                width = sprite.textureRect.width,
                height = sprite.textureRect.height,
                xOffset = sprite.textureRectOffset.x,
                yOffset = sprite.textureRectOffset.y,
                xAdvance = sprite.textureRectOffset.x,
                pivot = new Vector2(0.5f, 0.5f),
                hashCode = TMP_TextUtilities.GetSimpleHashCode(sprite.name),
            };

            spriteAsset.spriteInfoList.Add(spriteData);
        }


        spriteAsset.UpdateLookupTables();

        for(uint i = 0; i < spriteAsset.spriteCharacterTable.Count; i++)
        {
            var spriteCharacter = spriteAsset.spriteCharacterTable[(int)i];
            spriteCharacter.glyphIndex = i;
        }

        var assetPath = Path.Combine(Path.GetDirectoryName(spriteAssetPath), Path.GetFileNameWithoutExtension(spriteAssetPath) + ".asset");
        AssetDatabase.CreateAsset(spriteAsset, assetPath);
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
    }

Nevermind. I figured out checking the source code. For future viewers:

        static void AddDefaultMaterial(SpriteAsset spriteAsset)
        {
            Shader shader = Shader.Find("TextMeshPro/Sprite");
            Material material = new Material(shader);
            material.SetTexture(ShaderUtilities.ID_MainTex, spriteAsset.spriteSheet);

            spriteAsset.material = material;
            material.name = spriteAsset.name + " Material";
            AssetDatabase.AddObjectToAsset(material, spriteAsset);
        }