Skip to content

Commit 5ddd277

Browse files
committed
lookdev: add save-current-frame button to thumb generator
Add a "Save Current Frame..." button to the thumb generator inspector that renders the main camera to a PNG at a user-chosen path, independent of the batch thumbnail flow.
1 parent b1a51bd commit 5ddd277

2 files changed

Lines changed: 57 additions & 0 deletions

File tree

‎LookDev~/Assets/Editor/ThumbGeneratorInspector.cs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ public override void OnInspectorGUI()
6060
_generator.StopProcessing();
6161
}
6262

63+
GUI.enabled = !_generator.IsProcessing;
64+
EditorGUILayout.Space();
65+
if (GUILayout.Button("Save Current Frame...")) {
66+
var path = EditorUtility.SaveFilePanel("Save Current Camera Frame",
67+
"", "frame.png", "png");
68+
if (!string.IsNullOrEmpty(path)) {
69+
_generator.SaveCurrentFrame(path);
70+
}
71+
}
72+
6373
GUI.enabled = true;
6474

6575
if (_generator.IsProcessing) {

‎LookDev~/Assets/Runtime/ThumbGenerator.cs‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
using System;
2020
using System.Collections.Generic;
21+
using System.IO;
2122
using System.Linq;
2223
using UnityEditor;
2324
using UnityEngine;
@@ -105,6 +106,52 @@ public void StopProcessing()
105106
IsProcessing = false;
106107
}
107108

109+
/// <summary>
110+
/// Renders the current view of the main camera and writes it as a PNG to
111+
/// <paramref name="path"/>. Independent of the thumbnail batch; captures whatever the
112+
/// camera currently frames.
113+
/// </summary>
114+
public void SaveCurrentFrame(string path)
115+
{
116+
if (string.IsNullOrEmpty(path)) {
117+
return;
118+
}
119+
var camera = Camera.main;
120+
if (camera == null) {
121+
Debug.LogError("Cannot save frame: no main camera found in the scene.");
122+
return;
123+
}
124+
125+
var width = camera.pixelWidth > 0 ? camera.pixelWidth : 1920;
126+
var height = camera.pixelHeight > 0 ? camera.pixelHeight : 1080;
127+
var renderTexture = new RenderTexture(width, height, 24, RenderTextureFormat.ARGB32);
128+
var previousTarget = camera.targetTexture;
129+
var previousActive = RenderTexture.active;
130+
Texture2D texture = null;
131+
try {
132+
camera.targetTexture = renderTexture;
133+
camera.Render();
134+
135+
RenderTexture.active = renderTexture;
136+
texture = new Texture2D(width, height, TextureFormat.RGBA32, false);
137+
texture.ReadPixels(new Rect(0, 0, width, height), 0, 0);
138+
texture.Apply();
139+
140+
File.WriteAllBytes(path, texture.EncodeToPNG());
141+
Debug.Log($"Saved current camera frame to {path}");
142+
} catch (Exception e) {
143+
Debug.LogError($"Failed to save camera frame: {e}");
144+
} finally {
145+
camera.targetTexture = previousTarget;
146+
RenderTexture.active = previousActive;
147+
if (texture != null) {
148+
DestroyImmediate(texture);
149+
}
150+
renderTexture.Release();
151+
DestroyImmediate(renderTexture);
152+
}
153+
}
154+
108155
private void Process(AssetMaterialCombination a)
109156
{
110157
// camera preset

0 commit comments

Comments
 (0)