Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jjesh · Jul 21, 2016 at 02:16 PM · c#camerauicanvasscreenshot

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
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2

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;
     }
 
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jjesh · Jul 25, 2016 at 03:28 AM 0
Share

Thank you for the response! It would work, but wouldn't it also cause the UI to flicker for the user?

avatar image smnerat · Jul 25, 2016 at 05:43 AM 0
Share

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.

avatar image jjesh smnerat · Jul 27, 2016 at 07:17 PM 0
Share

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?

avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jjesh · Jul 28, 2016 at 03:16 PM 0
Share

$$anonymous$$aybe I'm confused, but how does that take the screenshot through the canvas?

avatar image
0

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... :(

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

204 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Scene Is Missing A Full Screen Camera? 2 Answers

UI Minimap 0 Answers

Screen Space - Camera to Screen Space - Overlay switch issue 0 Answers

How can I display text so that it can be see on the Vive Headset? 0 Answers

World Space canvas on top of "everything"? In 2019 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges