Take a screenshot in unity (with transparent background)


What do you do when you want to take a screenshot of something in unity? Say a 3D model you want to use as an image in an inventory system. It'd be nice if it had a transparent background, too, right? I'm not going to beat around the bush (any more). This tutorial is short and sweet. Paste the following into a new script in your assets folder named ScreenshotTool.cs. Then attach the script to a camera you want to use as your... camera. Then hit the "Take Screenshot" button and you should see an image on your desktop. Crop and import back into unity for a nice image of your 3D model.

This wasn't my code originally. I modified another guy's blog code and added the button.

// ScreenshotTool.cs
/*
 * Original: https://entitycrisis.blogspot.com/2017/02/take-unity-screenshot-with-alpha.html
 */
using System;
using System.IO;
using UnityEditor;
using UnityEngine;
[RequireComponent(typeof(Camera))]
public class ScreenshotTool : MonoBehaviour {
    [Range(1, 10)]
    [Tooltip("Specifies how many times to multiple the final image dimensions")]
    public int UpScale = 4;
    [Tooltip("Specifies to use a transparent background.\n\nNote: This may not create an image with a trasparent background if the camera is filled with objects. This basically just clears the skybox. So if you want a to capture a 3D object with a transparent background, place it in an empty scene.")]
    public bool AlphaBackground = true;
    public void SaveScreenshot() {
        string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        string filename = "SS-" + DateTime.Now.ToString("yyyy.MM.dd.HH.mm.ss") + ".png";
        File.WriteAllBytes(Path.Combine(path, filename), CreateScreenshot().EncodeToPNG());
    }
    Texture2D CreateScreenshot() {
        Camera camera = GetComponent<Camera>();
        int w = camera.pixelWidth * UpScale;
        int h = camera.pixelHeight * UpScale;
        //
        RenderTexture rt = new RenderTexture(w, h, 32);
        camera.targetTexture = rt;
        var screenShot = new Texture2D(w, h, TextureFormat.ARGB32, false);
        var clearFlags = camera.clearFlags;
        if (AlphaBackground) {
            camera.clearFlags = CameraClearFlags.SolidColor;
            camera.backgroundColor = new Color(0, 0, 0, 0);
        }
        camera.Render();
        RenderTexture.active = rt;
        screenShot.ReadPixels(new Rect(0, 0, w, h), 0, 0);
        screenShot.Apply();
        camera.targetTexture = null;
        RenderTexture.active = null;
        DestroyImmediate(rt);
        camera.clearFlags = clearFlags;
        return screenShot;
    }
}
[CustomEditor(typeof(ScreenshotTool))]
public class ScreenshotToolEditor : Editor {
    public override void OnInspectorGUI() {
        base.DrawDefaultInspector();
        if (GUILayout.Button("Take Screenshot")) {
            ScreenshotTool capture = (ScreenshotTool) target;
            capture.SaveScreenshot();
        }
    }
}

Get Against the Tide

Download NowName your own price

Comments

Log in with itch.io to leave a comment.

(1 edit)

Will this Capture the ui as well, and if not, is there a way to make it do so?