How do you take a screenshot of the game view without external sources like Snipping Tool or Lightshot, like to take a screenshot with the resolution i configured in my Game View window. Like i want to take a 4k screenshot for my desktop, store page or share with friends.
How to take a screenshot of the game view in Unity
Asked Answered
Unity has a screenshot tool already. It's called Recorder and doesn't require any coding.
- In Unity, go to the Window menu, then click on Package Manager
- By default, Packages might be set to "In Project". Select "Unity Registry" instead
- Type "Recorder" in the search box
- Select the Recorder and click Install in the lower right corner of the window
- That's about all you need to get everything set up and hopefully the options make sense. The main thing to be aware of that setting "Recording Mode" to "Single" will take a single screenshot (with F10)
that's nice! didnt knew about that ty –
Hylotheism
It's suprisingly easy, at the end you capture a screenshot off everything you see in the game view, if you want to dont show the ui, just disable the canvas for it.
private void Update(){
if(Input.GetMouseButtonDown(0)){ // capture screen shot on left mouse button down
string folderPath = "Assets/Screenshots/"; // the path of your project folder
if (!System.IO.Directory.Exists(folderPath)) // if this path does not exist yet
System.IO.Directory.CreateDirectory(folderPath); // it will get created
var screenshotName =
"Screenshot_" +
System.DateTime.Now.ToString("dd-MM-yyyy-HH-mm-ss") + // puts the current time right into the screenshot name
".png"; // put youre favorite data format here
ScreenCapture.CaptureScreenshot(System.IO.Path.Combine(folderPath, screenshotName),2); // takes the sceenshot, the "2" is for the scaled resolution, you can put this to 600 but it will take really long to scale the image up
Debug.Log(folderPath + screenshotName); // You get instant feedback in the console
}
}
Thanks! I made it a menu item with shortcut because I only need it in the Unity editor. –
Asternal
@Asternal haha, thats the first time someone trusted my code more than I do. Glad it works –
Hylotheism
Here (this is my own website) is a video showing how I use this script, that will take a screenshot at multiple specific screen sizes. Put that on a gameObject in the scene, make sure the class name and file name of the script are the same
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class screenShot : MonoBehaviour
{
// Singleton instance for easy access from other scripts
public static screenShot instance;
// Flag to enable/disable screenshot functionality
public bool takeScreenshots = false;
// Default location to save screenshots
//Windows//
public string screenshotLocation = "C:\\Users\\yourName\\Desktop\\screenshots";
//Mac//
//public string screenshotLocation = "/Users/yourName/Desktop/screenshots";
// Array of resolutions to cycle through when taking screenshots
public string[] resolutions;//640x480 etc;
// Private variables for tracking screen dimensions and screenshot process
private int width;
private int height;
private int screenN;
private bool scalingScreen = false;
private bool takingScreens = false;
private float waitF;
// Singleton pattern to ensure only one instance exists
void Awake()
{
if (instance)
{
// Destroy duplicate instances
Destroy(gameObject);
}
else
{
// Set the instance to this object and persist across scenes
instance = this;
DontDestroyOnLoad(gameObject);
}
}
// Set screen resolution based on the provided string
public void setXY(string _res)//separated ints with x//
{
// Split the resolution string into width and height
string[] splitNs = _res.Split('x');
width = int.Parse(splitNs[0]);
height = int.Parse(splitNs[1]);
// Set the screen resolution
Screen.SetResolution(width, height, false);
}
// Update is called once per frame
void Update()
{
// Check if screenshot functionality is enabled
if (takeScreenshots)
{
// Start taking screenshots when right mouse button is pressed
if (!takingScreens)
{
if (Input.GetMouseButtonDown(1))
{
// Play a sound effect
//soundManager.sound.playSFX(26);
// Initialize variables for taking screenshots
screenN = 0;
scalingScreen = true;
takingScreens = true;
Time.timeScale = 0f;
}
}
// Scale the screen resolution
else if (scalingScreen)
{
if (Time.realtimeSinceStartup > waitF)
{
// Set the screen resolution and update wait time
setXY(resolutions[screenN]);//resolution will not update until the next frame//
waitF = Time.realtimeSinceStartup + 2f;
scalingScreen = false;// Stop scaling
}
}
//take screenshots//
else
{
if(Time.realtimeSinceStartup > waitF)
{
// Capture screenshot
takeScreenshot(screenN);
// Check if more screens to capture
if (screenN + 1 < resolutions.Length)
{
screenN++;
scalingScreen = true;// Start scaling for the next resolution
waitF = Time.realtimeSinceStartup + 2f;
}
//finished taking screenshots
else
{
takingScreens = false;
Time.timeScale = 1f;// Resume the game
}
}
}
}
}
// Capture screenshot with a given index in resolutions array
void takeScreenshot(int _num)
{
// Save the screenshot with a filename containing resolution and timestamp
ScreenCapture.CaptureScreenshot(screenshotLocation + "\\haunt"+ resolutions[_num]+"_"+System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + ".png");
}
}
using UnityEngine;
using System.IO;
public class ScreenShotTaker : MonoBehaviour
{
public Camera screenshotCamera; // Assign the screenshot camera in the inspector
public int screenshotWidth = 3840; // Set desired width
public int screenshotHeight = 2160; // Set desired height
public void TakeTransparentScreenshot()
{
// Step 1: Enable the screenshot camera and set its background color to transparent
screenshotCamera.gameObject.SetActive(true);
screenshotCamera.clearFlags = CameraClearFlags.SolidColor;
screenshotCamera.backgroundColor = new Color(0, 0, 0, 0); // Transparent background
// Step 2: Create a high-resolution RenderTexture with transparency
RenderTexture rt = new RenderTexture(screenshotWidth, screenshotHeight, 24, RenderTextureFormat.ARGB32);
screenshotCamera.targetTexture = rt;
// Step 3: Render the screenshot camera view to the RenderTexture
RenderTexture.active = rt;
screenshotCamera.Render();
// Step 4: Create a high-resolution Texture2D with transparency to store the rendered image
Texture2D screenShot = new Texture2D(screenshotWidth, screenshotHeight, TextureFormat.RGBA32, false);
screenShot.ReadPixels(new Rect(0, 0, screenshotWidth, screenshotHeight), 0, 0);
screenShot.Apply();
// Step 5: Save the Texture2D as a PNG file with transparency
byte[] bytes = screenShot.EncodeToPNG();
string filename = Path.Combine(Application.persistentDataPath, "TransparentScreenshot.png");
File.WriteAllBytes(filename, bytes);
// Clean up
screenshotCamera.targetTexture = null;
RenderTexture.active = null;
Destroy(rt);
// Step 6: Disable the screenshot camera
screenshotCamera.gameObject.SetActive(false);
Debug.Log("Transparent screenshot saved to: " + filename);
}
// Step 7: Convert the Texture2D to Sprite
public Sprite TextureToSprite(Texture2D texture)
{
return Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
}
}
What is
Doodle_Manager.instance.pen
? –
Harhay Please Just Comment the Doodle_Manager.Instance.pen –
Bugloss
Why is it here to begin with if it should be commented out? –
Harhay
© 2022 - 2024 — McMap. All rights reserved.