Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

AssetPreview.GetAssetPreview [Textures Destroyed]

Discussion in 'Immediate Mode GUI (IMGUI)' started by Martin_Gonzalez, Jul 21, 2016.

  1. Martin_Gonzalez

    Martin_Gonzalez

    Joined:
    Mar 25, 2012
    Posts:
    361
    Ok, so i have this code that is doing some really weird stuff.

    Code (CSharp):
    1.         if(_categoryIndexTemp != _categoryIndex)
    2.         {
    3.             _previewTextures = new List<Texture>();
    4.             Debug.Log("Prefabs Qty: " + filteredPrefabs.Length);
    5.             for (int j = 0; j < filteredPrefabs.Length; j++)
    6.             {
    7.                 Texture text = null;
    8.  
    9.                 while (text == null)
    10.                 {
    11.                     text = AssetPreview.GetAssetPreview(filteredPrefabs[j]);
    12.                 }
    13.  
    14.                 _previewTextures.Add(text);
    15.                
    16.                 Debug.Log(_previewTextures[0]);
    17.             }
    18.             Debug.Log("Textures Qty: " + _previewTextures.Count);
    19.             _categoryIndexTemp = _categoryIndex;
    20.         }
    What is bassically doing is:
    -When i change the category of my enum i need to take all the prefabs preview of a category that are already filtered in filteredPrefabs.
    -I'm testing with some prefabs [137] and everything is loaded fine, 137 items none of them null.
    -As you can see i go item by item getting the preview adn also look that i have a debug of the item number [0]

    Why? That item is a UnityTexture2D and when the for reaches the item 50 item[0] turns to null and so on the other items [1] = null, [2]= null, and the last items of the List remain Texture2D.

    Is there any chance the reference are being replaced?

    The prefabs are the same but in different folders, i mean in a folder i have a cube and a sphere so i duplicate that folder x times.

    Help! :D
     
  2. shawn

    shawn

    Unity Technologies

    Joined:
    Aug 4, 2007
    Posts:
    552
    So this is working as designed, albeit a bit unexpected (the docs should be clearer about this).

    See the answers page for more discussion on the topic.

    Essentially, this API is taking responsibility for managing the loading/unloading of these asset previews. Creating a texture copy every time GetAssetPreview is called is asking for trouble, so instead it returns a cached texture. By default the cache size is 50. You can change the cache size with AssetPreview.SetPreviewTextureCacheSize.

    I think it might make sense for us to revisit this API to make it clearer and easier to work with, but not super high on our backlog right now. Hope that helps!
     
    mikelortega likes this.
  3. Martin_Gonzalez

    Martin_Gonzalez

    Joined:
    Mar 25, 2012
    Posts:
    361
    Thanks a lot! Can i have any problem setting the cache to a lerger number? for example 500?
     
  4. shawn

    shawn

    Unity Technologies

    Joined:
    Aug 4, 2007
    Posts:
    552
    Really only memory implications. Each AssetPreview Texture2D is 128x128 (ARGB32 or RGB24, depending on if the preview requires alpha) in Unity right now. This will likely be increased in the near future to 256x256 (or maybe even 512x512), to account for higher dpi monitors.