Generating asset previews

Quite a few versions back I created an editor browser tool, which created thumbnails of all assets that i tagged with a particular script. It worked great, until it didn’t.

I even filed a bug about it, and the Unity team told me they were aware of what was going on, and would try to get around to it.

Curious as to how, if at all, anyone is able to do this these days?

Here is my code to handle the previews

First I figure out how many previews I need to make like so:

private void InitContent()
        {
             // Set the ScrollList
            _items = GetAssetsWithScript<My_Items>(_path);
            _categorizedItems = new Dictionary<My_Items.Category, List<MY_Items>>();
            _previews = new Dictionary<MY_Items, Texture2D>();
            // Init the Dictionary
            foreach (MY_Items.Category category in _categories)
            {
                _categorizedItems.Add(category, new List<MY_Items>());
            }
            // Assign items to each category
            foreach (MY_Items item in _items)
            {
                _categorizedItems[item.category].Add(item);
            }

         
        }

public static List<T> GetAssetsWithScript<T>(string path) where T : MonoBehaviour
        {
            T tmp;
            string assetPath;
            GameObject asset;
            List<T> assetList = new List<T>();
            string[] guids = AssetDatabase.FindAssets("t:Prefab", new string[] { path });
            for (int i = 0; i < guids.Length; i++)
            {
                assetPath = AssetDatabase.GUIDToAssetPath(guids[i]);
                asset = AssetDatabase.LoadAssetAtPath(assetPath, typeof(GameObject)) as GameObject;
                tmp = asset.GetComponent<T>();
                if (tmp != null)
                {
                    assetList.Add(tmp);
                }
            }
            return assetList;
        }

And finally, handling the actual generation

private void GeneratePreviews()
        {
            AssetPreview.SetPreviewTextureCacheSize(1024);

            foreach (MY_Item item in _items)
            {
                if (!_previews.ContainsKey(item))
                {

                    Texture2D preview = AssetPreview.GetAssetPreview(item.gameObject);

                    if (preview != null)
                    {
                        _previews.Add(item, preview);
                    }
                }
            }
        }

Preview always returns null, and “item.gameObject” is always valid.

Like I said, this used to work just fine, then one day it stopped, and it has never come back. Would love some help here from the Unity dev team, or anyone honestly who has this type of thing working.

So basically you suspect GetAssetPreview doesn’t work properly?
One way to be sure of that is to isolate it and test it out alone: why don’t you try the following in an editor script

void OnGUI() {
if (Selection.gameObjects.Length > 0) {
GUILayout.Box( AssetPreview.GetAssetPreview( Selection.gameObjects[0] ), GUILayout.Height(50) ) }
}
This code works for me.
If this works for you, then implement back you GeneratePreviews() code, bit by bit until it stop working again, there you will have found what is responsable (That is a good way to go when you have absolutely no idea of what is going on)

2 Likes

Thanks, I’ll keep messing with it, just wanted to make sure that I wasn’t missing something obvious.

I tied your code into a test button, and nothing happens. I assume I should see a dialog appear somewhere with the preview embedded.

Just for curiosity sake, here is my code

 #region Test Preview
 if (GUILayout.Button("Test Preview", GUILayout.Width(160), GUILayout.MinHeight(60)))
 {
      var go = Selection.activeGameObject;
      if (go == null)
      {
             if (EditorUtility.DisplayDialog("Heads Up", "Gotta select something to preview", "OK"))
                 return;
      }

      GUILayout.Box( AssetPreview.GetAssetPreview( go ), GUILayout.Height(50) ) }
}
#endregion

I’ve even tried storing the Texture2D coming form the AssetPreview, and I can see that there is a valid texture being formed, but it just doesn’t show up any where.

So I got it working, it was actually a different piece of code hat was causing it to not generate…of course.

I know it is an old post, but what was your solution to fix it? (having the same issue here)