How to set thumbnail of assets from a script?

How would I like to know how to set an asset’s texture from an editor script. Basically, I made my own rendering engine in DOTS and want to preview the prefabs with my own image and not the default blue cube. I tried

public override Texture2D RenderStaticPreview(string assetPath, UnityEngine.Object[] subAssets, int width, int height)
    {
        Debug.Log("Rendered Statics");
        ConvertRenderer renderer = target as ConvertRenderer;

        if (renderer == null || renderer.sprite == null)
            return null;

        Texture2D cache = new Texture2D(width, height);
        EditorUtility.CopySerialized(AssetPreview.GetAssetPreview(renderer.sprite), cache);
        return cache;
    }

Doesn’t seem to do anything. If you need a reference for what I am trying to do, there is an asset called “AssetIcons” that does what I am talking about.

Thanks!

RenderStaticPreview only works for ScriptableObjects, not for gameObjects with a component (there could be more possible thumbnails coming from different scripts that way)

[CustomEditor(typeof(GameObject))]
public class ChangeObjectThumbEditor : Editor
{
public Texture2D texture;

    public void OnEnable()
    {
        texture = Resources.Load<Texture2D>("DesertHeart");
    }

    public override Texture2D RenderStaticPreview(string assetPath, UnityEngine.Object[] subAssets, int width, int height)
    {
        Debug.Log("Rendered Statics");
        GameObject renderer = target as GameObject;

        if (renderer == null || texture == null)
            return null;

        Texture2D cache = new Texture2D(width, height);
        EditorUtility.CopySerialized(AssetPreview.GetAssetPreview(texture), cache);
        return cache;
    }
}

Here is the code that allows you to change the thumbnail. It’s extremely primitive, but should give you an idea of how to proceed.