Created
February 8, 2023 13:35
-
-
Save sobhanb-eth/e214ffe03caaaedc23c375a349ee861e to your computer and use it in GitHub Desktop.
Handy Unity ScreenshotTool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
using Unity.Jobs; | |
using Unity.Collections; | |
using Unity.Burst; | |
public class ScreenshotTool : EditorWindow | |
{ | |
RenderTexture renderTexture; | |
int selectedResolutionIndex; | |
int sharpness = 0; | |
int smoothness = 0; | |
[MenuItem("Tools/Take Screenshot")] | |
public static void ShowWindow() | |
{ | |
GetWindow<ScreenshotTool>("Screen Capture"); | |
} | |
private void OnGUI() | |
{ | |
GUILayout.BeginHorizontal(); | |
GUILayout.Label("Resolution:", GUILayout.Width(70)); | |
selectedResolutionIndex = | |
EditorGUILayout.Popup(selectedResolutionIndex, new string[] { "HD", "FHD", "QHD", "UHD" }); | |
GUILayout.EndHorizontal(); | |
GUILayout.BeginHorizontal(); | |
GUILayout.Label("Sharpness:", GUILayout.Width(70)); | |
sharpness = EditorGUILayout.IntSlider(sharpness, 0, 10); | |
GUILayout.EndHorizontal(); | |
GUILayout.BeginHorizontal(); | |
GUILayout.Label("Smoothness:", GUILayout.Width(70)); | |
smoothness = EditorGUILayout.IntSlider(smoothness, 0, 10); | |
GUILayout.EndHorizontal(); | |
if (GUILayout.Button("Take Screenshot")) | |
{ | |
TakeScreenshot(); | |
} | |
} | |
private void TakeScreenshot() | |
{ | |
int width = 0, height = 0; | |
switch (selectedResolutionIndex) | |
{ | |
case 0: | |
width = 1280; | |
height = 720; | |
break; | |
case 1: | |
width = 1920; | |
height = 1080; | |
break; | |
case 2: | |
width = 2560; | |
height = 1440; | |
break; | |
case 3: | |
width = 3840; | |
height = 2160; | |
break; | |
} | |
renderTexture = new RenderTexture(width, height, 24); | |
Camera.main.targetTexture = renderTexture; | |
Texture2D screenshot = new Texture2D(width, height, TextureFormat.RGBA32, false); | |
Camera.main.Render(); | |
RenderTexture.active = renderTexture; | |
screenshot.ReadPixels(new Rect(0, 0, width, height), 0, 0); | |
Camera.main.targetTexture = null; | |
RenderTexture.active = null; | |
screenshot = EnhanceScreenshot(screenshot, sharpness, smoothness); | |
byte[] bytes = screenshot.EncodeToPNG(); | |
System.IO.File.WriteAllBytes(Application.dataPath + "/Screenshot.png", bytes); | |
Debug.Log("Took screenshot!"); | |
} | |
[BurstCompile] | |
struct ScreenshotEnhanceJob : IJobParallelFor | |
{ | |
[ReadOnly] public NativeArray<Color> sourceColors; | |
public NativeArray<Color> resultColors; | |
[ReadOnly] public int sharpness; | |
[ReadOnly] public int smoothness; | |
public void Execute(int index) | |
{ | |
var color = sourceColors[index]; | |
// Apply Sharpness | |
color.r = Mathf.Clamp(color.r + (sharpness * 0.1f), 0f, 1f); | |
color.g = Mathf.Clamp(color.g + (sharpness * 0.1f), 0f, 1f); | |
color.b = Mathf.Clamp(color.b + (sharpness * 0.1f), 0f, 1f); | |
// Apply Smoothness | |
if (smoothness <= 1 || index % smoothness == 0) | |
{ | |
resultColors[index] = color; | |
} | |
else | |
{ | |
var prevIndex = index - 1; | |
if (prevIndex >= 0) | |
{ | |
var prevColor = resultColors[prevIndex]; | |
resultColors[index] = (color + prevColor) / 2; | |
} | |
else | |
{ | |
resultColors[index] = color; | |
} | |
} | |
} | |
} | |
private Texture2D EnhanceScreenshot(Texture2D screenshot, int sharpness, int smoothness) | |
{ | |
var job = new ScreenshotEnhanceJob | |
{ | |
sourceColors = new NativeArray<Color>(screenshot.GetPixels(), Allocator.TempJob), | |
resultColors = new NativeArray<Color>(screenshot.GetPixels(), Allocator.TempJob), | |
sharpness = sharpness, | |
smoothness = smoothness | |
}; | |
var handle = job.Schedule(screenshot.width * screenshot.height, 64); | |
handle.Complete(); | |
var result = new Texture2D(screenshot.width, screenshot.height, TextureFormat.RGBA32, false); | |
result.SetPixels(job.resultColors.ToArray()); | |
result.Apply(); | |
job.sourceColors.Dispose(); | |
job.resultColors.Dispose(); | |
return result; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment