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. Dismiss Notice

Question `AssetPreview.GetMiniThumbnail` returns Prefab icon, not the prefab image shown in Project window.

Discussion in 'Scripting' started by morima123, Oct 3, 2023.

  1. morima123

    morima123

    Joined:
    Feb 2, 2022
    Posts:
    5
    Hello, I was wondering why `AssetPreview.GetMiniThumbnail` is not giving me the same images of prefabs as the project window?

    I am using the code below to save prafab thumbnails under a specific directory.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6. using System.IO;
    7.  
    8. public class PointThumbnailGenerator : EditorWindow
    9. {
    10.     UnityEngine.Object searchDirectory;
    11.     List<GameObject> objList = new List<GameObject>();
    12.     string dirPath = "Assets/Captures/"; // output dir
    13.     int width = 100; // image width
    14.     int height = 100; // image hegiht
    15.  
    16.     [MenuItem("Window/PointThumbnailGenerator")]
    17.     static void ShowWindow()
    18.     {
    19.         GetWindow(typeof(PointThumbnailGenerator));
    20.     }
    21.  
    22.     void OnGUI()
    23.     {
    24.         GUILayout.BeginHorizontal();
    25.         GUILayout.Label("Search Directory : ", GUILayout.Width(110));
    26.         searchDirectory = EditorGUILayout.ObjectField(searchDirectory, typeof(UnityEngine.Object), true);
    27.         GUILayout.EndHorizontal();
    28.         EditorGUILayout.Space();
    29.  
    30.         GUILayout.BeginHorizontal();
    31.         GUILayout.Label("Save directory : ", GUILayout.Width(110));
    32.         dirPath = EditorGUILayout.TextField(dirPath);
    33.         GUILayout.EndHorizontal();
    34.         EditorGUILayout.Space();
    35.  
    36.         GUILayout.BeginHorizontal();
    37.         GUILayout.Label("Width : ", GUILayout.Width(110));
    38.         width = EditorGUILayout.IntField(width);
    39.         GUILayout.EndHorizontal();
    40.         EditorGUILayout.Space();
    41.  
    42.         GUILayout.BeginHorizontal();
    43.         GUILayout.Label("Height : ", GUILayout.Width(110));
    44.         height = EditorGUILayout.IntField(height);
    45.         GUILayout.EndHorizontal();
    46.         EditorGUILayout.Space();
    47.  
    48.         if (GUILayout.Button(new GUIContent("Capture")))
    49.         {
    50.             if (searchDirectory == null) return;
    51.  
    52.             if (!Directory.Exists(dirPath))
    53.             {
    54.                 Directory.CreateDirectory(dirPath);
    55.             }
    56.  
    57.             objList.Clear();
    58.  
    59.             string replaceDirectoryPath = AssetDatabase.GetAssetPath(searchDirectory);
    60.             string[] filePaths = Directory.GetFiles(replaceDirectoryPath, "*.*");
    61.             foreach (string filePath in filePaths)
    62.             {
    63.                 GameObject obj = AssetDatabase.LoadAssetAtPath(filePath, typeof(GameObject)) as GameObject;
    64.                 if (obj != null)
    65.                 {
    66.                     objList.Add(obj);
    67.                 }
    68.             }
    69.  
    70.             foreach (GameObject obj in objList)
    71.             {
    72.                 Debug.Log("OBJ :  " + obj.name);
    73.                 Capture(obj);
    74.             }
    75.         }
    76.     }
    77.  
    78.     void Capture(GameObject obj)
    79.     {
    80.         Texture2D thumbnail = AssetPreview.GetMiniThumbnail(obj);
    81.         if (thumbnail != null)
    82.         {
    83.             // Create a new readable texture and set its pixels from the existing texture
    84.             Texture2D readableTexture = new Texture2D(thumbnail.width, thumbnail.height);
    85.             RenderTexture rt = RenderTexture.GetTemporary(thumbnail.width, thumbnail.height);
    86.             Graphics.Blit(thumbnail, rt);
    87.             RenderTexture previous = RenderTexture.active;
    88.             RenderTexture.active = rt;
    89.             readableTexture.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
    90.             readableTexture.Apply();
    91.             RenderTexture.active = previous;
    92.             RenderTexture.ReleaseTemporary(rt);
    93.  
    94.             // Encode to PNG
    95.             byte[] bytes = readableTexture.EncodeToPNG();
    96.  
    97.             // Save the PNG
    98.             File.WriteAllBytes($"{dirPath}{obj.name}.png", bytes);
    99.  
    100.             // Optionally, destroy the copy to free memory
    101.             DestroyImmediate(readableTexture);
    102.         }
    103.     }
    104. }
    105.  
    The images I want to save are like the ones in the first image attached but I only get the images of light blue cube (prefab icon) with the code above.

    What am I doing wrong?
    AssetPreview.GetMiniThumbnail says "Returns the thumbnail for an object (like the ones you see in the project view)." so it should return the image I see in the project view?
     

    Attached Files:

  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    But this is the 2D forum so it's clearly not the place to discuss this.

    I'll move your post for you.
     
    morima123 likes this.
  3. morima123

    morima123

    Joined:
    Feb 2, 2022
    Posts:
    5
    I'm sorry that I posted in the wrong place and thank you for moving this post here.

    Btw, do you happen to know why this issue (about AssetPreview.GetMiniThumbnail) is happening?
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    Unfortunately I'm a 2D physics dev and know nothing about this feature, sorry. You're more likely to get an answer here though.
     
  5. morima123

    morima123

    Joined:
    Feb 2, 2022
    Posts:
    5
    I understand! Thank you for your reply :)
     
  6. morima123

    morima123

    Joined:
    Feb 2, 2022
    Posts:
    5
    Well I figured out how to save the image of prefab shown in Project window in a different way.

    I just switched
    Texture2D thumbnail = AssetPreview.GetMiniThumbnail(obj);

    to
    Texture2D thumbnail = AssetPreview.GetAssetPreview(obj);
    .

    Everything else in the code is the same I posted above and this works fine. (Although images are a bit dark)

    But I still don't understand why GetMiniThumbnail doesn't work... If anyone knows please let me know.