Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Created April 7, 2024 09:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unitycoder/b740b9db92e454d83ade8528d5e1f382 to your computer and use it in GitHub Desktop.
Save unitycoder/b740b9db92e454d83ade8528d5e1f382 to your computer and use it in GitHub Desktop.
Save Thumbnails for Scenes (unity editor script)
using System.IO;
using UnityEditor;
using UnityEngine;
namespace UnityLibrary
{
[CustomEditor(typeof(SceneAsset))]
public class CustomThumbnail : Editor
{
public override bool HasPreviewGUI() => true;
public override void OnPreviewGUI(Rect r, GUIStyle background)
{
if (target == null) return;
GUI.Label(r, (target as SceneAsset).name);
var scenePath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(target));
var sceneName = Path.GetFileNameWithoutExtension((target as SceneAsset).name);
// check if scene thumbnail exists (NOTE: this is a hidden file)
var path = Path.Combine(scenePath, "." + sceneName + ".png");
if (File.Exists(path))
{
Texture2D texture = new Texture2D(2, 2);
texture.LoadImage(File.ReadAllBytes(path));
GUI.DrawTexture(r, texture, ScaleMode.ScaleToFit);
}
else
{
//GUI.Label(r, "\n\nNo thumbnail found");
var fileSize = File.Exists(scenePath) ? new FileInfo(scenePath).Length.ToString() : "???";
var lastModified = File.Exists(scenePath) ? File.GetLastWriteTime(scenePath) : System.DateTime.Now;
GUI.Label(r, "\n\nSize: " + fileSize + "\nLast modified: " + lastModified);
}
}
}
}
using UnityEditor;
using UnityEngine;
using System.IO;
namespace UnityLibrary
{
[InitializeOnLoad]
static class SceneThumbnail
{
static bool saveAsHiddenFile = true;
static SceneThumbnail()
{
UnityEditor.SceneManagement.EditorSceneManager.sceneSaved += OnSceneSaved;
}
// https://forum.unity.com/threads/editorscenemanager-scenesaved-example-no-code-examples-in-the-docs.488861/
static void OnSceneSaved(UnityEngine.SceneManagement.Scene scene)
{
var sceneName = System.IO.Path.GetFileNameWithoutExtension(scene.path);
// grab scene camera (or main camera if want to?)
var camera = SceneView.lastActiveSceneView.camera;
if (camera == null)
{
Debug.LogWarning("No SceneView.lastActiveSceneView.camera found..");
return;
}
// render the camera to a texture
var texture = new Texture2D(camera.pixelWidth, camera.pixelHeight);
var renderTexture = new RenderTexture(camera.pixelWidth, camera.pixelHeight, 24);
camera.targetTexture = renderTexture;
camera.Render();
RenderTexture.active = renderTexture;
texture.ReadPixels(new Rect(0, 0, camera.pixelWidth, camera.pixelHeight), 0, 0);
texture.Apply();
camera.targetTexture = null;
RenderTexture.active = null;
Object.DestroyImmediate(renderTexture);
// save the texture to a file
var path = Path.Combine(Path.GetDirectoryName(scene.path), (saveAsHiddenFile ? "." : "") + sceneName + ".png");
File.WriteAllBytes(path, texture.EncodeToPNG());
//Debug.LogFormat("Saved thumbnail to '{0}'", path);
AssetDatabase.Refresh();
}
}
}
@unitycoder
Copy link
Author

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