Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Problems with AssetPreview.GetAssetPreview

Discussion in 'Scripting' started by numberkruncher, May 12, 2013.

  1. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    Hey guys

    I have been experimenting with AssetPreview.GetAssetPreview and am confused as to why the following script does not work as I had expected. Instead of gradually caching previews as they are loaded, previews simply vanish. Previews that have been loaded and displayed are actually becoming null which naturally forces them to get loaded and displayed again.

    Here is the script:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4.  
    5. public class TestPreviewWindow : EditorWindow {
    6.  
    7.     [MenuItem("Window/Test Preview")]
    8.     private static void ShowWindow() {
    9.         GetWindow<TestPreviewWindow>("Test Preview");
    10.     }
    11.  
    12.     private GameObject[] prefabs;
    13.     private Texture2D[] prefabPreviews;
    14.  
    15.     private void OnEnable() {
    16.         prefabs = Resources.FindObjectsOfTypeAll(typeof(GameObject)) as GameObject[];
    17.         prefabPreviews = new Texture2D[prefabs.Length];
    18.     }
    19.  
    20.     private Vector2 _scrolling;
    21.  
    22.     public void OnGUI() {
    23.         int columns = Mathf.FloorToInt(position.width / 128);
    24.  
    25.         _scrolling = GUILayout.BeginScrollView(_scrolling);
    26.         {
    27.             GUILayout.BeginHorizontal();
    28.  
    29.             for (int i = 0; i < prefabs.Length; ++i) {
    30.                 if (i % columns == 0) {
    31.                     GUILayout.EndHorizontal();
    32.                     GUILayout.BeginHorizontal();
    33.                 }
    34.  
    35.                 if (prefabPreviews[i] == null) {
    36.                     prefabPreviews[i] = AssetPreview.GetAssetPreview(prefabs[i]);
    37.                     if (AssetPreview.IsLoadingAssetPreview(prefabs[i].GetInstanceID())) {
    38.                         Repaint();
    39.                         GUILayout.Box("Loading...", GUILayout.Width(128), GUILayout.Height(128));
    40.                     }
    41.                     else {
    42.                         GUILayout.Box("?", GUILayout.Width(128), GUILayout.Height(128));
    43.                     }
    44.                 }
    45.                 else {
    46.                     GUILayout.Box(prefabPreviews[i], GUILayout.Width(128), GUILayout.Height(128));
    47.                 }
    48.             }
    49.  
    50.             GUILayout.EndHorizontal();
    51.         }
    52.         GUILayout.EndScrollView();
    53.     }
    54.  
    55. }
    56.  
    Here is the problem:


    To reproduce the problem:
    • Load AngryBots example project in Unity 4.
    • Ensure that a large number of prefab assets have been loaded by selecting them in the inspector. There is probably an easier way to quickly load all prefabs, but I took the lowtech route to demonstrate in the above video.
    • Add the above editor script and select Window|Test Preview.
    • Scroll up/down to observe oddness.
    Any ideas as to why this doesn't work as expected? Is anybody from Unity able to confirm how the preview functions are supposed to work?

    Many thanks :)
     
  2. hololabs

    hololabs

    Joined:
    Nov 15, 2012
    Posts:
    20
  3. DrDigg0R

    DrDigg0R

    Joined:
    Apr 7, 2013
    Posts:
    30
    Hmm, seems like the problem is still not fixed :(

    Any news here?
     
  4. turbanov

    turbanov

    Joined:
    Dec 22, 2014
    Posts:
    59
    As a notice, I have found that if you set a custom indicator for an asset, it doesn't load the preview.
     

    Attached Files:

  5. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    oldish thread, but you need to use the Editor class

    Code (csharp):
    1.  
    2. Editor
    3.  
    4.  
     
  6. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    The cause of the flicking is that Unity has a small cache size which is shared amongst all editor windows when using their API. Recently they have added an API to customize this; but I still ran into issues when using previews in multiple editor windows.

    AssetPreview.SetPreviewTextureCacheSize

    I have since rolled my own asset preview API which avoids all of these issues and provides a lot more control over how the previews are rendered and at which size they are rendered.
     
    mcbauer likes this.