|
18 | 18 |
|
19 | 19 | using System; |
20 | 20 | using System.Collections.Generic; |
| 21 | +using System.IO; |
21 | 22 | using System.Linq; |
22 | 23 | using UnityEditor; |
23 | 24 | using UnityEngine; |
@@ -105,6 +106,52 @@ public void StopProcessing() |
105 | 106 | IsProcessing = false; |
106 | 107 | } |
107 | 108 |
|
| 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 | + |
108 | 155 | private void Process(AssetMaterialCombination a) |
109 | 156 | { |
110 | 157 | // camera preset |
|
0 commit comments