- Home /
Why do only some of my sprites generate preview images?
I'm trying to load all the sub-sprites in a 2D gridded sprite and make them available o na menu. Since there is no way to convert a sprite directly to a texture2D that I could see, I thought I'd try AssetPreview.GetAssetPreview(..)
Problem is that it's half working and half not. Some of my sprites seem to get proper preview images while others seem to be blank. I have checked and all sprites DO show up when used on screen.
This is my code
void Update () {
if (clearPreviews){
LoadSpritePreviews();
clearPreviews=false;
}
if (tilesetSprite != _oldTilesetprite){
_oldTilesetprite = tilesetSprite;
// load tiles and tielset image
string path = AssetDatabase.GetAssetPath(tilesetSprite);
Object[] rawAssets = AssetDatabase.LoadAllAssetsAtPath(path);
_tilesetImage = (Texture2D) rawAssets[0];
_sprites = new Sprite[rawAssets.Length-1];
System.Array.Copy(rawAssets,1,_sprites,0,_sprites.Length);
LoadSpritePreviews ();
}
}
private void LoadSpritePreviews(){
// laod previews
_spritePreviews = new Texture2D[_sprites.Length];
for(int i=0;i<_sprites.Length;i++){
_spritePreviews[i] = AssetPreview.GetAssetPreview(_sprites[i]);
}
}
I thought maybe it was a timing thing, that the asset load was happening asynchronously, so I added a check box i could check to force it to regenerate the previews, but I still get the same results :(
Here is a picture of my selection grid of the preview images:
This is what the sprite importer looks like in the project window, so apparently the editor is successfully creating previews for all the sprite images..
On further analysis, it looks like later calls are returning null.
I am wondering if this is a memory usage issue? Are there ways to see/tune the memory used in the editor?
Alright this is really starting to look like a memory issue. I assume its because I am loading 255 sprites.
Is there another way to get an image of them short of loading them into memory and using AssetPreview?
I seriously doubt that's a memory issue unless the sprites are massive in size. 255 is not very many.
Well, I'm not sure what else could be causing it then. Its either memory or ti$$anonymous$$g because its acting like a hiesenbug-- changing the number of previews i get changes which ones do and don't show up.
I've decided to try to use the cached icons ins$$anonymous$$d... but I can't figure out how I specify the path of a sub asset to GetCachedIcon(). Guess Ill post a new question.
You're creating and trashing a full array of textures every frame? That can't be good...
Answer by RDeluxe · Feb 14, 2016 at 05:54 PM
I know it's an old question, but I found myself with the same problem.
The trick is to change AssetPreview.SetPreviewTextureCacheSize ! Set the this cache size to your array lenght (_sprites.Length in your case), and it should work :)