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
1
Question by jjennings1990 · May 02, 2014 at 06:53 PM · textureoptimizationscreenshotbytes

Memory issues when taking Screenshots ?

Hello Guys i am having memory issues when we create a screenshot in unity, encode the given texture to bytes and clear the texture from memory . I found and used this code for shrinking the texture size (http://wiki.unity3d.com/index.php/TextureScale) and it's worked well but it seems like whenever we create a screenshot in memory the textures are never cleared from resources or memory .I am using the following code , to take and store the screen shot as bytes, and then upon saving, saving the raw byte data to the system file .

IEnumerator takeScreenshot(){

     //*** if byte array already has values  null it out
     if(screenGrabBytes != null ){

         screenGrabBytes = null;

         screenGrabBytes = new byte[0];
     }

     yield return new WaitForEndOfFrame();
     //*** set initial screensho dimensions
     int width = Screen.width;
     int height = Screen.height;
     
     Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
     // Read screen contents into the texture
     tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);


     yield return  .5f;

     // we are going to divide the textures size by an eighth of the original screenshots size 
     TextureScale.Bilinear(tex,width/4,height/4);
        
     //*** here we take the reduced texture  and encode it to bytes
     screenGrabBytes = tex.EncodeToPNG(); 

     //**** here we null out the texture we use for screenshot creation
     tex = null;

     //*** we return the initial texture to a 1 by 1 pixel image
     tex = new Texture2D(0,0);

     //*** here we de-allocate any screenshots or existing data still in the project
     Resources.UnloadUnusedAssets();
 }


Does anyone have any recommendations on how I might be able to optimize this screenshot grabbing process or see where my code may be bloating unnecessarily.

the next thing I was going to try is destroying the texture that we create , since I don't think I am ever deleting it and after grabbing the byte data it's unnecessary .

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 ThePunisher · May 02, 2014 at 10:58 PM

So first off, don't call Resources.UnloadUnusedAssets in this case, it is the cause of your lag spike. Instead just simply save the raw byte data to your file and set the texture reference and the byte array to null. This should release the memory when the garbage collector gets to it.

I used that script myself to downscale screenshots to use as thumbnails for the save/load menu and I remember there being some other quirk. I'll take a look at my code and let you know if there's any other things to watch out for.

Comment
Add comment · Show 4 · 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 jjennings1990 · May 05, 2014 at 07:18 PM 0
Share

Thanks for the help, this actually re$$anonymous$$ded me that maybe I should restructure my code a bit as well , ins$$anonymous$$d of nulling out the byte array at the beginning I'll just null it out at the end so we can clear data isn't just sitting around taking up memory unnecessarily . definitely helpful and something my $$anonymous$$ should be ok with , thanks !

avatar image ThePunisher · May 14, 2014 at 07:43 PM 0
Share

Can you close the question or mark it as an answer if it helped you solve your problem?

avatar image mtusan · May 20, 2016 at 10:10 AM 0
Share

Hi Punisher, I'm using the same algorythm "TextureScale.Bilinear" (http://wiki.unity3d.com/index.php/TextureScale) to downscale screenshots to thumbs and i have narrowed down my memory issues to this algorythm. Any chance you remember what was the problem with this script and how you fixed it?

avatar image jijieciprian · Oct 06, 2016 at 02:46 PM 0
Share

@ThePunisher on windows phone tex = null; tex = new Texture2D(0,0);not working, any ideea any solution?

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

23 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

Related Questions

File.ReadAllBytes does not work on mac ? 1 Answer

Is it bad to have this many materials? 2 Answers

Changing objects texture causes high draw call count! 1 Answer

Colors are wrong in in-game screenshot 1 Answer

Assigning UV Map to model at runtime 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