AssetPreview.GetMiniTypeThumbnail not working for custom scripts

Hi, I’m developing a Unity extension. I want to display custom script icons in the component list I created.

AssetPreview.GetMiniTypeThumbnail is working perfect for built-in unity icons such as Camera but it’s returning an empty icon for custom scripts which have custom icons.

This displays a button with camera icon:

GUILayout.Button(AssetPreview.GetMiniTypeThumbnail(typeof(Camera)), GUILayout.ExpandWidth(false));

This displays an empty button :

GUILayout.Button(AssetPreview.GetMiniTypeThumbnail(typeof(AutoLinker)), GUILayout.ExpandWidth(false));

How can I display the icon for custom scripts?

This may be a post from 2014, but also after running into this issue, I have come to a solution!

As mentioned by @Xtro the base AssetPreview.GetMiniTypeThumbnail function fails to return a Texture2D for custom scripts, and will only return a blank page icon. To correctly pull the icon from a custom script as @oscarAbraham has written, you need to get the asset/asset path of the custom script.

You can find the asset via the following code:

Type scriptType = typeof(CustomScript);
string[] scriptAsset = AssetDatabase.FindAssets(scriptType.Name + " t:MonoScript");
string scriptPath = AssetDatabase.GUIDToAssetPath(scriptAsset[0]);
UnityEngine.Object scriptObject = AssetDatabase.LoadAssetAtPath(scriptPath, typeof(UnityEngine.Object));

Then grab the thumbnail via the GetMiniThumbnail function:

Texture2D scriptIconTexture2D = AssetPreview.GetMiniThumbnail(scriptObject);

And if you what to use this in a image’s Sprite, you can then create a sprite with the Texture2D as such:

Sprite iconSprite = Sprite.Create(scriptIconTexture2D , new Rect(0, 0, scriptIconTexture2D .width, scriptIconTexture2D .height), new Vector2(0.5f, 0.5f));

Of course note that getting the thumbnail icon of Scriptable Objects is a different process where you modify the FindAsset search filter to “t:CustomScriptableObjectClass”, where all Scriptable Object asset GUIDs created from your CustomScriptableObjectClass will be return, and a foreach loop and be iterated through to get each of their paths and then their assets to pull their icons from.

Hope this helps future google searchers!