Skip to content

Instantly share code, notes, and snippets.

@Lonsdale201
Last active September 22, 2024 15:22
Show Gist options
  • Save Lonsdale201/e9d281c31fb6168e24431dcbf1e575fb to your computer and use it in GitHub Desktop.
Save Lonsdale201/e9d281c31fb6168e24431dcbf1e575fb to your computer and use it in GitHub Desktop.
unity - Rapid transparent Screenshot maker for prefabs and other objects
// How to use
// Create a new .cs file in the Unity with AssetPreviewScreenshot name
// Create a new folder and name it to Editor place the AssetPreviewScreenshot.cs file here!
// When done, navigate the Tools > Preview Screenshort - menu name and open it
// select your prefab material etc.. Click the Take a screenshot button
// In the preview you will see Unity in its original colour. But in the saved state it will be transparent.
// If you want to use a different color in Unity, you have to rewrite the original values in the code: #525252 (RGB value: 82, 82, 82)
// Why is colour important? Because the code explclit transforms the given colour code.
// When you look at a 3D model or material in inspector you can see what the default background color is in Unity,
// if it is a different color than the default color in the code, you have to rewrite it, otherwise the images will not be transparent.
// rewite it in the Line 79
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Diagnostics;
public class PreviewScreenshot : EditorWindow
{
private Object selectedObject;
private Object lastSelectedObject;
private Texture2D previewTexture;
private string lastSavedPath;
private bool isLoadingPreview = false;
[MenuItem("Tools/Preview Screenshot")]
public static void ShowWindow()
{
GetWindow<PreviewScreenshot>("Preview Screenshot");
}
void OnGUI()
{
GUILayout.BeginVertical();
selectedObject = EditorGUILayout.ObjectField("Select Object", selectedObject, typeof(Object), false);
if (selectedObject != lastSelectedObject)
{
isLoadingPreview = true;
previewTexture = null;
lastSelectedObject = selectedObject;
}
if (isLoadingPreview && selectedObject != null)
{
previewTexture = AssetPreview.GetAssetPreview(selectedObject);
if (previewTexture != null)
{
isLoadingPreview = false;
}
}
if (previewTexture != null)
{
GUILayout.Label("Preview:");
GUILayout.Label(previewTexture, GUILayout.Width(256), GUILayout.Height(256));
}
else if (selectedObject != null && isLoadingPreview)
{
GUILayout.Label("Loading preview...");
Repaint(); // Folyamatosan frissíti az ablakot, amíg a preview betölt
}
if (GUILayout.Button("Take Screenshot"))
{
if (selectedObject != null && previewTexture != null)
{
SavePreviewScreenshot(selectedObject);
}
else
{
UnityEngine.Debug.LogWarning("No object selected or preview not available!");
}
}
if (!string.IsNullOrEmpty(lastSavedPath) && GUILayout.Button("Open Folder"))
{
OpenFolder(lastSavedPath);
}
GUILayout.EndVertical();
}
private void SavePreviewScreenshot(Object obj)
{
if (previewTexture != null)
{
SaveTextureAsPNGWithTransparency(previewTexture, obj.name);
}
else
{
UnityEngine.Debug.LogWarning("No preview available to save.");
}
}
private void SaveTextureAsPNGWithTransparency(Texture2D texture, string fileName)
{
Texture2D transparentTexture = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);
Color[] pixels = texture.GetPixels();
for (int i = 0; i < pixels.Length; i++)
{
if (Mathf.Approximately(pixels[i].r, 82 / 255f) && Mathf.Approximately(pixels[i].g, 82 / 255f) && Mathf.Approximately(pixels[i].b, 82 / 255f))
{
pixels[i] = new Color(0, 0, 0, 0);
}
}
transparentTexture.SetPixels(pixels);
transparentTexture.Apply();
byte[] bytes = transparentTexture.EncodeToPNG();
string path = EditorUtility.SaveFilePanel("Save Screenshot", "", fileName + "_transparent.png", "png");
if (!string.IsNullOrEmpty(path))
{
File.WriteAllBytes(path, bytes);
lastSavedPath = path;
UnityEngine.Debug.Log("Screenshot saved to: " + path);
}
}
private void OpenFolder(string filePath)
{
string folderPath = Path.GetDirectoryName(filePath);
if (!string.IsNullOrEmpty(folderPath))
{
Process.Start("explorer.exe", folderPath.Replace("/", "\\"));
UnityEngine.Debug.Log("Opened folder: " + folderPath);
}
}
}
@Lonsdale201
Copy link
Author

fixed the layoutgroup error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment