hi
i want to take a screen shot with transparent background
i use this code but if my object was particles / additive it dosen’t show them or if i use antialiasing …
code :
RenderTexture rt = new RenderTexture (resWidth, resHeight, 24);
Camera.main.camera.targetTexture = rt;
Texture2D screenShot = new Texture2D (resWidth, resHeight, TextureFormat.ARGB32, false);
Camera.main.camera.Render ();
RenderTexture.active = rt;
screenShot.ReadPixels (new Rect (0, 0, resWidth, resHeight), 0, 0);
Camera.main.camera.targetTexture = null;
RenderTexture.active = null;
Destroy (rt);
byte[] bytes = screenShot.EncodeToPNG ();
//string filename = ScreenShotName (resWidth, resHeight);
string filename = string.Format ("{0}/mypic/screen_{1}x{2}_{3}.png",
Application.dataPath, resWidth, resHeight, counter);
counter = counter + 1;
System.IO.File.WriteAllBytes (filename, bytes);
The property of the camera
Clear Flags = Depth only
Code:
public static Texture2D TakeScreenShot()
{
return Screenshot ();
}
static Texture2D Screenshot() {
int resWidth = Camera.main.pixelWidth;
int resHeight = Camera.main.pixelHeight;
Camera camera = Camera.main;
RenderTexture rt = new RenderTexture(resWidth, resHeight, 32);
camera.targetTexture = rt;
Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.ARGB32, false);
camera.Render();
RenderTexture.active = rt;
screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
screenShot.Apply ();
camera.targetTexture = null;
RenderTexture.active = null; // JC: added to avoid errors
Destroy(rt);
return screenShot;
}
public static Texture2D SaveScreenshotToFile(string fileName)
{
Texture2D screenShot = Screenshot ();
byte[] bytes = screenShot.EncodeToPNG();
System.IO.File.WriteAllBytes(fileName, bytes);
return screenShot;
}
I’m not sure this is possible since when you screen shot you basically just capture what the camera is rendering, so know, you could try converting a certain color to transparency after you save the file. How you would do that I have know idea though…
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class Screenshot : MonoBehaviour
{
private void Start()
{
string filename = string.Format("Assets/Screenshots/capture_{0}.png", DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss-fff"));
if (!Directory.Exists("Assets/Screenshots"))
{
Directory.CreateDirectory("Assets/Screenshots");
}
TakeTransparentScreenshot(Camera.main, Screen.width, Screen.height, filename);
}
public static void TakeTransparentScreenshot(Camera cam, int width, int height, string savePath)
{
// Depending on your render pipeline, this may not work.
var bak_cam_targetTexture = cam.targetTexture;
var bak_cam_clearFlags = cam.clearFlags;
var bak_RenderTexture_active = RenderTexture.active;
var tex_transparent = new Texture2D(width, height, TextureFormat.ARGB32, false);
// Must use 24-bit depth buffer to be able to fill background.
var render_texture = RenderTexture.GetTemporary(width, height, 24, RenderTextureFormat.ARGB32);
var grab_area = new Rect(0, 0, width, height);
RenderTexture.active = render_texture;
cam.targetTexture = render_texture;
cam.clearFlags = CameraClearFlags.SolidColor;
// Simple: use a clear background
cam.backgroundColor = Color.clear;
cam.Render();
tex_transparent.ReadPixels(grab_area, 0, 0);
tex_transparent.Apply();
// Encode the resulting output texture to a byte array then write to the file
byte[] pngShot = ImageConversion.EncodeToPNG(tex_transparent);
File.WriteAllBytes(savePath, pngShot);
cam.clearFlags = bak_cam_clearFlags;
cam.targetTexture = bak_cam_targetTexture;
RenderTexture.active = bak_RenderTexture_active;
RenderTexture.ReleaseTemporary(render_texture);
Texture2D.Destroy(tex_transparent);
}
}