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.