Getting None or Missing Texture from AssetPreview.GetAssetPreview

I have a strange problem. The first few objects of my gameobject array which contains the asset previews from the specified folder return none or a missing texture. And every second time building the scene the textures from Element 10 to 20 are none or missing and the textures that were none before are assigned properly.

My code:

using UnityEngine;
using System.Collections;
using UnityEditor;

public class Items : MonoBehaviour
	public GameObject[] item;	
	public Texture2D[] itemIcon;  
	void Start ()
		itemIcon = new Texture2D[item.Length];
		for(int i = 0; i < item.Length; i++)
			itemIcon _= AssetPreview.GetAssetPreview(item*);*_

I’m using Unity 4.
Sorry for my english :stuck_out_tongue:


GetAssetPreview is really running asynchronously in the background, and if the preview you want is not available when you ask for it you will get a null instead of a texture.

You can work around this by polling the preview multiple times:

int counter = 0;
Texture2D thumbnail = null;
while (thumbnail == null && counter < 75)
    thumbnail= AssetPreview.GetAssetPreview(o);
    counter ++;

However this does give a lag time of as much as 1 second for each preview. On my machine I rarely see more than 30 or 45 ms but disk access, processor speed, and other hidden stuff will all affect that. Use with caution.

After several hours of research I found a way to do it:
Run the while loop until the texture is not null and then continue. Here’s a small example

Texture2D bTexture =  null;
	bTexture = AssetPreview.GetAssetPreview(go);
if(bTexture != null)
	bTexture.mipMapBias = -1.5f;
	byte[] data = bTexture.EncodeToPNG();
	string pathAndName =".png";
	File.WriteAllBytes(pathAndName, data);

For 2021, I think the best way to do this is probably set off a Coroutine to just query until an image is found. In my code I’m looking to set the thumbnail of some canvas objects but they aren’t setting.

I did this and it seems to work.

IEnumerator SetThumbnail()
    Texture2D thumbnail = null;
    while (thumbnail == null)
        thumbnail = AssetPreview.GetAssetPreview(myPrefab);
        yield return new WaitForSeconds(.5f);
    var sprite = Sprite.Create(thumbnail, new Rect(0.0f, 0.0f, thumbnail.width, thumbnail.height), new Vector2(0.5f, 0.5f), 100.0f);

In my Start() method I just fire it off using:

StartCoroutine( SetThumbnail() );

I found that the texture from GetAssetPreview() is not always available.
I’m copying it to a local texture once I get it.

Texture2D tx = AssetPreview.GetAssetPreview(myPrefab);
if (tx == null)
	_tex = null;
	_tex = new Texture2D(tx.width,tx.height);

Coroutine is definitely better than a thread-blocking while loop.

However, if you aren’t in unity Object-land, and are instead working with UI Toolkit, I’d suggest using the Scheduler. This has worked for me.

Image thumbnail;
schedule.Execute(() =>
    var preview = AssetPreview.GetAssetPreview(asset);
    if (preview != null)
        _thumbnail.image = preview;
.Until(() => !AssetPreview.IsLoadingAssetPreview(asset.GetInstanceID()));

There’s room for optimisations, and you should likely ensure the task is stopped if it cannot be completed for whatever reason.

Also note that if you are trying to display many thumbnails simultaneously, you may run into the cache limit. See Unity - Scripting API: AssetPreview.SetPreviewTextureCacheSize