Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 temptest123 · Jul 22, 2013 at 07:01 AM · editoropenglreadpixels

ReadPixels works on device (iPad) but not in the editor?

the following code, when run on iPad, works and saves the images I expect to see

 void OnPostRender ()     
 {
     //save image from unity for testing. The camera that renders is called explicitely using Camera.Render
     
     Texture2D texture = new Texture2D(_renderSize,_renderSize,TextureFormat.ARGB32,false);
     //setting a test color to see if saving works correctly and if ReadPixels changes the color
     Color32[] colors = new Color32[_renderSize*_renderSize];
     for(int i=0; i<_renderSize*_renderSize; i++){
         colors[i] = new Color32(0,127,0,255);    
     }
     texture.SetPixels32(colors);
     texture.ReadPixels(new Rect(0,0,_renderSize,_renderSize),0,0);
     texture.Apply();
     byte[] bytes = texture.EncodeToPNG();
         
     System.String path = Application.dataPath.Substring(0,Application.dataPath.LastIndexOf('/'));
     path = path.Substring(0,path.LastIndexOf('/')) + "/Documents/UnityImage"+_count.ToString ()+".png";
     Debug.Log("Unity: saving image to "+path);
     System.IO.File.WriteAllBytes(path, bytes);

}

when running it in the editor though, the images saved to disk are pure green (0,127,0,255), meaning that ReadPixels seems to do nothing. I'm not getting any error - any idea what could cause this behaviour?

note: I explicitely want to read from the framebuffer and not use a renderTexture. This is because I use the framebuffer to pass some procedural renderings to the XCode project which reads it from the frameBuffer. The above code is intended for debugging when run in the editor - but unfortunately it does not work in the editor, which means I need to test each change on the device

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
1

Answer by Bessu · Dec 02, 2015 at 12:12 PM

To my experience it's impossible to trust ReadPixels on the editor. For example ReadPixels in my code on windows works perfect on editor but not on a device with the same resolution. Or the same code won't work on mac editor but still works on ipad with the same resolution as the editor.

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
avatar image
0

Answer by leo-dom · Jul 22, 2013 at 08:31 AM

Call texture.Apply() before ReadPixels().

Maybe Apply() will apply the cached SetPixels32() changes, so it will override the ReadPixels().

And it is a implementation-relate problem, the best practice is call Apply just after SetPixels calls.

PS: This is just my guess, because your code does work in My Unity Editor.

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
avatar image
0

Answer by wibble82 · Dec 02, 2015 at 12:42 PM

I would hazard a guess that you are calling 'read pixels' before calling 'apply'.

Until you have called 'apply', the effects of any pixel setting are not written to the texture. Exactly where they're stored temporarily will depend on the platform implementation.

I suspect you are seeing a difference in the undefined behaviour of 'what happens if I read pixels before calling apply'.

The fix is probably to simply ensure your texture changes have been applied before trying to read it.

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

18 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

Related Questions

Forcing OpenGL in Windows Editor 1 Answer

ReadPixels to capture GameView in EditMode and "Pixels out of bounds" 3 Answers

Is possible to force my game to run in a spesific OpenGL version? 0 Answers

How to change Unity Editor graphics back-end 1 Answer

Using ReadPixels to capture GL calls 1 Answer


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