Capturing high-resolution screenshots from specific cameras requiers careful implementation of render textrues and file output operations.
Core Script:
CameraSnapshot.cs
using UnityEngine;
using System.IO;
public class CameraSnapshot : MonoBehaviour {
public enum ResolutionMode {
CameraPixelDimensions,
DisplayResolution,
CustomDimensions
}
public Camera snapshotCamera;
public ResolutionMode resolutionMode = ResolutionMode.CameraPixelDimensions;
public Vector2 customResolution;
public string storageDirectory = "StreamingAssets/";
public string outputFilename = "camera_snapshot.png";
#if UNITY_EDITOR
private void Reset() {
snapshotCamera = GetComponent<Camera>();
customResolution = new Vector2(Screen.currentResolution.width, Screen.currentResolution.height);
}
#endif
public void TakeAndSaveSnapshot() {
Vector2 dimensions = customResolution;
if (resolutionMode == ResolutionMode.CameraPixelDimensions) {
dimensions = new Vector2(snapshotCamera.pixelWidth, snapshotCamera.pixelHeight);
} else if (resolutionMode == ResolutionMode.DisplayResolution) {
dimensions = new Vector2(Screen.currentResolution.width, Screen.currentResolution.height);
}
string fullPath = Application.dataPath + "/" + storageDirectory + outputFilename;
SaveTextureToFile(fullPath, CaptureCameraView(snapshotCamera, (int)dimensions.x, (int)dimensions.y));
}
public static Texture2D CaptureCameraView(Camera cam) {
return CaptureCameraView(cam, Screen.width, Screen.height);
}
public static Texture2D CaptureCameraView(Camera cam, int width, int height) {
RenderTexture renderTex = new RenderTexture(width, height, 0);
renderTex.depth = 24;
renderTex.antiAliasing = 8;
cam.targetTexture = renderTex;
cam.RenderDontRestore();
RenderTexture.active = renderTex;
Texture2D image = new Texture2D(width, height, TextureFormat.ARGB32, false, true);
Rect captureRegion = new Rect(0, 0, width, height);
image.ReadPixels(captureRegion, 0, 0);
image.filterMode = FilterMode.Point;
image.Apply();
cam.targetTexture = null;
RenderTexture.active = null;
Destroy(renderTex);
return image;
}
public static void SaveTextureToFile(string path, Texture2D texture) {
File.WriteAllBytes(path, texture.EncodeToPNG());
#if UNITY_EDITOR
Debug.Log("Screenshot saved to: " + path);
#endif
}
}
Editor Integration:
CameraSnapshotEditor.cs
using UnityEditor;
using UnityEngine;
[CanEditMultipleObjects]
[CustomEditor(typeof(CameraSnapshot))]
public class CameraSnapshotEditor : Editor {
public override void OnInspectorGUI() {
CameraSnapshot component = (CameraSnapshot)target;
int currentSelection = (int)component.resolutionMode;
EditorGUI.BeginChangeCheck();
DrawInspectorField("snapshotCamera", "Target Camera");
string[] resolutionOptions = new string[] { "Camera Resolution", "Screen Resolution", "Custom Resolution"};
currentSelection = EditorGUILayout.Popup("Resolution Mode", currentSelection, resolutionOptions, GUILayout.ExpandWidth(true));
component.resolutionMode = (CameraSnapshot.ResolutionMode)currentSelection;
if (component.resolutionMode == CameraSnapshot.ResolutionMode.CustomDimensions) {
DrawInspectorField("customResolution", "Resolution");
EditorGUILayout.HelpBox("Maintain proper aspect ratio to prevent visual distortion in captured images.", MessageType.Info);
}
DrawInspectorField("storageDirectory", "Save Directory");
DrawInspectorField("outputFilename", "File Name");
bool capturePressed = GUILayout.Button("Capture Screenshot", GUILayout.ExpandWidth(true));
if (capturePressed) component.TakeAndSaveSnapshot();
if (EditorGUI.EndChangeCheck()) serializedObject.ApplyModifiedProperties();
}
private void DrawInspectorField(string propertyName, string displayLabel) {
EditorGUILayout.PropertyField(serializedObject.FindProperty(propertyName), new GUIContent(displayLabel), true);
}
}