- using UnityEngine;
- using UnityEditor;
- public class ExportAssetPreview : MonoBehaviour
- {
- [MenuItem("Assets/Export Asset Preview")]
- private static void ExportSelectedAssetPreview()
- {
- Object selectedAsset = Selection.activeObject;
- if (selectedAsset == null)
- {
- Debug.Log("No asset selected.");
- return;
- }
- Texture2D assetPreview = AssetPreview.GetAssetPreview(selectedAsset);
- if (assetPreview == null)
- {
- Debug.Log("Failed to get asset preview.");
- return;
- }
- byte[] imageData = assetPreview.EncodeToPNG();
- string savePath = EditorUtility.SaveFilePanel("Save Asset Preview", "", "Preview.png", "png");
- if (savePath.Length != 0)
- {
- System.IO.File.WriteAllBytes(savePath, imageData);
- Debug.Log("Asset preview exported successfully!");
- }
- }
- }
- 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
- 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.
- Please note that this script should be placed inside an Editor folder for it to work correctly
Raw Paste