Take a screenshot in unity (with transparent background)
Against the Tide » Devlog
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
Against the Tide
Tower Defense + Match 3
Status | In development |
Author | Against the Tide |
Genre | Strategy |
Tags | Fantasy, Level Editor, match3, Mouse only, realtime, Real time strategy, Singleplayer, Tower Defense |
Languages | English |
More posts
- New game page layoutJan 24, 2021
- 0.150.0 Released & DiscordJan 15, 2021
- 0.100.0 released!Jan 14, 2021
- It's awhile, but I'm still working!Jan 10, 2021
- Almost done with loadoutsJan 02, 2021
- Working on the loadout screensJan 02, 2021
- Finally! Play and upload levels online!Dec 30, 2020
- How to implement "wait at least this long" in Unity/C#Dec 28, 2020
- Level Editor is live!Dec 27, 2020
Comments
Log in with itch.io to leave a comment.
Will this Capture the ui as well, and if not, is there a way to make it do so?