CPP-WINAPI   12
Unity code
Guest on 18th September 2023 01:59:43 AM


  1. using UnityEngine;
  2. using UnityEditor;
  3.  
  4. public class ExportAssetPreview : MonoBehaviour
  5. {
  6.     [MenuItem("Assets/Export Asset Preview")]
  7.     private static void ExportSelectedAssetPreview()
  8.     {
  9.         Object selectedAsset = Selection.activeObject;
  10.         if (selectedAsset == null)
  11.         {
  12.             Debug.Log("No asset selected.");
  13.             return;
  14.         }
  15.  
  16.         Texture2D assetPreview = AssetPreview.GetAssetPreview(selectedAsset);
  17.         if (assetPreview == null)
  18.         {
  19.             Debug.Log("Failed to get asset preview.");
  20.             return;
  21.         }
  22.  
  23.         byte[] imageData = assetPreview.EncodeToPNG();
  24.         string savePath = EditorUtility.SaveFilePanel("Save Asset Preview", "", "Preview.png", "png");
  25.         if (savePath.Length != 0)
  26.         {
  27.             System.IO.File.WriteAllBytes(savePath, imageData);
  28.             Debug.Log("Asset preview exported successfully!");
  29.         }
  30.     }
  31. }
  32.  
  33. Sure! Here's a script that you can use in Unity to export the AssetPreview.GetAssetPreview of a selected asset to an image using EncodeToPNG. It includes the MenuItem attribute for easy access in the Unity Editor
  34. This script provides a menu item under Assets in the Unity Editor. When you select an asset and click on Export Asset Preview, it will attempt to extract the asset's preview image. If successful, it will prompt you to choose a save location, and then save the image as a PNG file.
  35.  
  36. Please note that this script should be placed inside an Editor folder for it to work correctly

Raw Paste

Login or Register to edit or fork this paste. It's free.