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 /
  • Help Room /
avatar image
0
Question by Lazybones94 · Oct 19, 2015 at 12:13 PM · cameratexture2dscreenshot

Image saved to gallery is black

Hello everyone. In my app i want to make a picture and save it to gallery for android and iOS. As far as I need to capture a specific part of the screen and save only specific objects, I am using the second camera to capture what I need. Following code shows how I do it:

     private Texture2D TakeSnapshot()
     {
 //this is for background so the empty space under the objects is not just black or something
         snapshotBackground.transform.position = new Vector3(0, 0, 1);
 //read the texture from camera and save it in my Texture2D object
         Texture2D snapShot = new Texture2D(Screen.width, Screen.height, TextureFormat.ARGB32, false);
         RenderTexture snapShotRT = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.ARGB32);
         RenderTexture.active = snapShotRT;
         lineRenderingCamera.targetTexture = snapShotRT;
         lineRenderingCamera.Render();
 
         snapShot.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
         snapShot.Apply();
 //release
         RenderTexture.active = null;
         lineRenderingCamera.targetTexture = null;
         //move the background away so it cannot be seen anymore
         snapshotBackground.transform.position = new Vector3(0, 50, 0);
 
         return snapShot;
     }

Afterwards I save this snapshot to the asset folder when I am in the editor, and everything is just fine - it saves exactly what I want it to save. But when I test it on any android device - the returned Texture2D is just black, without any screenshots, and I have no idea why. I tried to read pixels from the main camera(thought that on device camera coordinates somehow differ), but still, just a fully black texture. Please, help!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Lazybones94 · Oct 19, 2015 at 01:41 PM

And again I solved the problem by myself, lol, sorry for disturbing, guys. In case someone experienced the same problem - I just needed to convert the method into IEnumerator and use WaitForEndOfFrame before using RenderTexture.

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 PlasmaByte · Oct 12, 2017 at 10:20 PM 0
Share

Hey there, I'm having what sounds like the same problem. Could you share your solution?

$$anonymous$$y current code looks like this:

     IEnumerator TakePhoto(string filename){
         // We should only read the screen buffer after rendering is complete
         yield return new WaitForEndOfFrame(); 
 
         Texture2D photo = new Texture2D(webCamTexture.width, webCamTexture.height);
         photo.SetPixels(webCamTexture.GetPixels());
         photo.Apply();
 
         //Encode to a JPG
         byte[] bytes = photo.EncodeToJPG();
 
         //Write out the JPG
         System.IO.File.WriteAllBytes( filename , bytes);
     }

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

38 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

Related Questions

ScreenCapture.CaptureScreenshotAsTexture() is making a milky white tinted screenshot. 1 Answer

how can i store a screenshot into android mobile? 0 Answers

Wrong Coloring (Sometimes) when capturing "Screen"-shot 1 Answer

Zooming in to a certain area of UI 0 Answers

Taking a Screenshot and then displaying the Screenshot in game 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