Question by
cocapasteque · Jun 02, 2017 at 09:03 AM ·
unity 5editorassetspreview
GetAssetPreview not working as intended (stack multiple assets in one)
Hi there,
I'm having trouble generating assets preview in PNG format with Unity 5.6.1p1 . I wrote this script who's working well and generates png images for my assets and saves them on my file system. The only thing that bother me is the image generated :
It seems like multiple assets from my game are being rendered, so I wonder if there is not a stream behind not being properly flushed or something like this...
Here's the script behind :
void Start ()
{
var pPath = Application.dataPath + "/../Previews/";
var folders = ResourcesLoader.GetAllFoldersInEditorObject();
Directory.CreateDirectory(pPath);
foreach (var f in folders)
{
var cPath = pPath + f + "/";
Directory.CreateDirectory(cPath);
var objs = Resources.LoadAll<GameObject>("EditorObject/" + f);
foreach (var o in objs)
{
Texture2D t = null;
var counter = 0;
while (t == null && counter < 15)
{
t = AssetPreview.GetAssetPreview(o);
counter++;
Thread.Sleep(15);
}
Debug.Log("Get preview : " + o.name);
try
{
var bytes = t.EncodeToPNG();
using (var fs = new FileStream(cPath + o.name + ".png", FileMode.Create, FileAccess.Write))
{
fs.Write(bytes, 0, bytes.Length);
}
}
catch (Exception ex)
{
Debug.LogError("Cannot create file " + cPath + o.name + ".png");
}
}
break;
}
}
(I've put a break at the end of the first loop for testing purpose)
Is there anybody experiencing the same ?
arch02.png
(25.3 kB)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612123329im_/https://answers.unity.com/themes/thub/images/avi.jpg)