- Home /
Is there a way to make a Texture2D screenshot that ignores the canvas?
So far I've tried this by making a secondary camera that doesn't render the UI and putting the screenshot code in there, but I think because the main camera shows the UI, the texture2D shows it as well. Is there a way to have Unity make a texture2D of the screen without the existing canvas showing? Here's the script I have so far:
public class MapScreenshot : MonoBehaviour
{
public Texture2D mapScreen;
public IEnumerator MapShot()
{
yield return new WaitForEndOfFrame();
mapScreen = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
mapScreen.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
mapScreen.Apply();
//Took the shot
}
}
Answer by smnerat · Jul 23, 2016 at 10:27 PM
Is there a reason you can't just turn off the UI and then turn it back on?
EDIT: As it turns out, no, it's not that simple. The solution I came up with calls for the use of a second camera. You create the second camera and copy the settings from your main camera to the new one (I may have missed a couple of settings, but you should get the idea).
Pay attention to is the layer mask section. My UI is on layer 5, so I first set the new camera to only render layer 5, and then I invert it so everything besides layer 5 is rendered.
Finally, I take my snapshot similar to the way you did.
private Camera newCam;
private Texture2D mapScreen;
private RenderTexture mapScreenRT;
void Start () {
GameObject g = new GameObject ();
g.AddComponent<Camera> ();
g.transform.parent = Camera.main.transform;
g.transform.localPosition = Vector3.zero;
g.transform.localRotation = Quaternion.Euler (Vector3.zero);
g.SetActive (false);
newCam = g.GetComponent<Camera> ();
newCam.enabled = false;
mapScreenRT = new RenderTexture (Screen.width, Screen.height, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Default);
}
void TakeMapShot(){
StartCoroutine("MapShot");
}
public IEnumerator MapShot() {
newCam.aspect = Camera.main.aspect;
newCam.fieldOfView = Camera.main.fieldOfView;
newCam.orthographic = Camera.main.orthographic;
newCam.orthographicSize = Camera.main.orthographicSize;
newCam.backgroundColor = Camera.main.backgroundColor;
newCam.cullingMask = 1 << 5;
newCam.cullingMask = ~newCam.cullingMask;
yield return new WaitForEndOfFrame();
newCam.targetTexture = mapScreenRT;
newCam.Render ();
mapScreen = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
mapScreen.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
mapScreen.Apply();
newCam.targetTexture = null;
}
Thank you for the response! It would work, but wouldn't it also cause the UI to flicker for the user?
I updated the code to fix that. It's pretty much the same thing, but I moved the canvas.enabled = false; to occur after the end of the frame, ins$$anonymous$$d of in the Take$$anonymous$$apShot() function. Now it won't flicker. I generally only take screen shots to be used by myself, so I usually don't worry about it, but the flickering is understandably something a user doesn't want to see.
Thanks for sticking with this, but I just got a chance to try it out and the texture it makes includes the canvas. I think what's happening is that the screenshot is happening before the canvas is actually off. Any idea why?
Answer by SunnyChow · Jul 25, 2016 at 04:15 AM
I assume there is only one camera (there will be more steps if more than one camera)
Camera.main.targetTexture = (A RenderTexture);
Camera.main.Render();
Texture2D image = new Texture2D( (That RenderTexture's size) );
RenderTexture.active = (That RenderTexture);
image.ReadPixels(new Rect(0, 0, (That RenderTexture's size) ), 0, 0);
image.Apply();
RenderTexture.active = null;
It's complicated but it's flexible
$$anonymous$$aybe I'm confused, but how does that take the screenshot through the canvas?
Answer by Bubsavvy · Apr 30, 2019 at 03:22 AM
Unfortunately, there is not a way to do this at all as far as I know. Its relatively frustrating I feel your pain. The most useful mode to have your Canvas element is "Screen Overlay", yet Unity does not at all provide a way to mask the Canvas in this mode even with the culling mask layer on the camera set to ignore UI Layers. It seems like something as small as this should have a solution, but clearly now the only way to do this effectively is what @smnerat has provided or a variation of it without "ScreenOverlay", or temporarily disable the canvas and just deal with the single frame of no UI. Very frustrating... :(