- Home /
How Can I take a "edited" screenshot in a 2D game?
I wrote this code following a tutorial, my question is how can I include in the screenshot only the sprite and the background
public class ScreenshotsHandler : MonoBehaviour
{
private static ScreenshotsHandler instance;
private Camera myCamera;
private bool takeScreenshotOnNextFrame;
private void Awake(){
instance = this;
myCamera = gameObject.GetComponent<Camera>();
}
private void OnPostRender() {
if (takeScreenshotOnNextFrame) {
takeScreenshotOnNextFrame = false;
RenderTexture renderTexture = myCamera.targetTexture;
Texture2D renderResult = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.ARGB32,false);
Rect rect = new Rect(0,0,renderTexture.width,renderTexture.height);
renderResult.ReadPixels(rect, 0, 0);
byte[] byteArray = renderResult.EncodeToPNG();
System.IO.File.WriteAllBytes(Application.dataPath + "/Screenshot.png",byteArray);
Debug.Log("Screenshot salvato!");
RenderTexture.ReleaseTemporary(renderTexture);
myCamera.targetTexture = null;
}
}
private void TakeScreenshot(int width, int height){
myCamera.targetTexture = RenderTexture.GetTemporary(width, height, 16);
takeScreenshotOnNextFrame = true;
}
public static void TakeSCreenshot_Static (int width, int height){
instance.TakeScreenshot(width,height);
}
private void start (){
}
private void Update(){
if (Input.GetKeyDown(KeyCode.Insert)) {
ScreenshotsHandler.TakeSCreenshot_Static (1920, 1080);
}
}
}
Answer by Happeloy · Feb 10, 2020 at 10:18 PM
You can achieve this by using a second camera for the screenshot, that is set to only see the layers that you want to include in the screenshot.
Set the layers of the objects to something other than default, and then set the culling mask of your screenshot camera to only see those layers.
Your answer
Follow this Question
Related Questions
How to make a stickman not dismember itself 0 Answers
How to blur 2D background sprites? 0 Answers
moving player top down,movement in unity 2d 0 Answers
Destroy a gameObject in the scene if instantiate the same gameObject 0 Answers
My 2D Player Can't Move (2d Photon Game) :(,My Player Don't Move (2d Character Controller 0 Answers