简体   繁体   English

GetAssetPreview 总是返回 null

[英]GetAssetPreview always returns null

I have a small unity editor script that gets asset previews from unity and saves them to a folder.我有一个小的统一编辑器脚本,可以从统一获取资产预览并将它们保存到一个文件夹中。 It works well for my machine but it doesn't work on another machine that has the same project.它适用于我的机器,但它不适用于具有相同项目的另一台机器。 Here is the code I use:这是我使用的代码:

GameObject[] objs = Resources.LoadAll<GameObject>("");
foreach (GameObject obj in objs)
{
    Texture2D previewImage = null;
    while (previewImage == null)
    {
        previewImage = AssetPreview.GetAssetPreview(obj);
        Thread.Sleep(100);
    }
    System.IO.File.WriteAllBytes(previewImage.name + ".png", previewImage.EncodeToPNG());
}

The code stays in an infinite loop where it says previewImage == null .代码停留在一个无限循环中,它说previewImage == null Any ideas ?有任何想法吗 ?

I know this is an old post, but I have been struggling with the same issue and what seems to work really well for me is the following code:我知道这是一篇旧帖子,但我一直在努力解决同样的问题,并且对我来说似乎非常有效的是以下代码:

AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(prefab), ImportAssetOptions.ForceUpdate);

This will cause a bit of lag for large asset databases, but not too much.这会对大型资产数据库造成一些滞后,但不会太多。

Another way of solving this issue is to use:解决此问题的另一种方法是使用:

EditorUtility.SetDirty(prefab);

This however is sooo much slower...然而,这太慢了......

Check both and see which one fits your needs, but I would go with the first one.检查两者,看看哪一个适合您的需求,但我会选择第一个。

EditorUtility.SetDirty(prefab);

Worked for me!为我工作! Thanks!谢谢! For textures it returns whole atlas, but with 3d model worked good.对于纹理,它返回整个图集,但使用 3d 模型效果很好。 I needed a couple of hacks to prevent application failure:我需要一些技巧来防止应用程序失败:

  1. Max tries counter.麦克斯尝试反击。

  2. Sprites dictionary for not calling SetDirty() every time.每次不调用 SetDirty() 的 Sprites 字典。

     public static Texture2D GetPreviewForced(Object obj) { if (obj == null) return null; int instanceId = obj.GetInstanceID(); if (_itemsPreviews.ContainsKey(obj)) { return _itemsPreviews[obj]; } else { if (Application.isPlaying) EditorUtility.SetDirty(obj); Texture2D result = AssetPreview.GetAssetPreview(obj); int tries = 1000; while (AssetPreview.IsLoadingAssetPreview(instanceId) && tries > 0) { tries--; } if (tries != 0) _itemsPreviews.Add(obj, result); return result; } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM