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 kmeboe · Sep 28, 2012 at 01:15 AM · c#iosscreenshotthumbnails

How Can I Generate a Thumbnail from the Screen?

Hi folks,

I'm working on generating a thumbnail from a screen capture (to give users a visual image for file browsing). So far, my code works to generate a full-size screenshot:

     IEnumerator TakeEpicScreenshot()
     {
         yield return new WaitForEndOfFrame();
         
         // create a texture to pass to encoding
         Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
  
         // put buffer into texture
         texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
 
     yield return 0;
 
     texture.Apply();
 
         yield return 0;
  
         byte[] bytes = texture.EncodeToPNG();
  
         // save the image
     string imagePath = "amazingPath.png";
     File.WriteAllBytes(imagePath, bytes);
   }

This part works perfectly. The problem is that this image is far larger than I want to store. Is there a way for me to shrink the image down so that it fits in a smaller image (think: StretchBlt from Windows)?

I'm also somewhat concerned with the performance hit: it would be ideal to generate the screenshot at the proper size in the first place, but at this point I'll take what I can get.

Any pointers are welcome.

Thanks,

Kevin

Edit: I should also mention that I'm developing for iOS, in case that is pertinent.

Comment
Add comment · Show 5
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 Fattie · Sep 28, 2012 at 07:10 AM 1
Share

just for the record, there's a prime31 plugin that completely does all that in one line (thank goodness). I'm very lazy so I just use that. those guys are not perfect but in many situations it's just better to use their stuff.

avatar image kmeboe · Sep 28, 2012 at 05:16 PM 0
Share

Are you talking about the iOS Etcetera plugin? I'm happy to make a purchase, but I want to make sure that what I buy will really do what I'm looking for.

If it works, create an answer so I can give you mad propz!

avatar image Fattie · Sep 28, 2012 at 05:19 PM 0
Share

dude i THIN$$anonymous$$ it's the Etcetera plugin (i've used it for exactly that purpose .. with the camera you know?) - but they have so many plugins ... suggest, just ask them, their pretty nice

avatar image kmeboe · Sep 28, 2012 at 05:25 PM 0
Share

I'll hit them up; thanks again.

avatar image Fattie · Sep 28, 2012 at 05:27 PM 0
Share

yeah I'd hate to tell you a WRONG FACT .. rock on

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by kmeboe · Oct 02, 2012 at 05:54 PM

So I chatted with Mike from Prime31 -- nice guy. He informed me that Unity has a built-in way to do this, using the RenderTexture class. What I ended up doing, essentially, is the following:

  1. Create a new camera, and set if up in the scene with the thumbnail view I want

  2. Create a new Render Texture, and add it as the camera's Target Texture.

  3. Render my screenshot using the ReadPixels approach above, but using my render texture as the source instead of the screen (by setting RenderTexture.active to my render texture before calling ReadPixels()).

That's basically it. See here for more info on RenderTexture (including detailed steps for some of the above): http://docs.unity3d.com/Documentation/Components/class-RenderTexture.html

Thanks again to Fattie for pointing me in the right direction...+1.

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 jasonjoh · Jul 08, 2016 at 06:47 AM

Thank you for this code. It really helped me. Here is my updated version. First create a Render Texture and attach it to your camera under Target Texture then drag your camera onto the script. Press 'e' to generate a screenshot.

 public Camera cam;
 
 void Update()
 {
     if (Input.GetKeyDown("e"))
     {
         Debug.Log("Saving Screen Shot");
         StartCoroutine(CreateLayerThumbnail());  
     }
         
 }
 
 IEnumerator CreateLayerThumbnail()
 {
     yield return new WaitForEndOfFrame();
 
     // create a texture to pass to encoding
     Texture2D texture = new Texture2D(cam.targetTexture.width, cam.targetTexture.height, TextureFormat.RGB24, false);
 
     // Initialize and render
     cam.Render();
     RenderTexture.active = cam.targetTexture;
 
     // put buffer into texture
     texture.ReadPixels(new Rect(0, 0, cam.targetTexture.width, cam.targetTexture.height), 0, 0);
 
     yield return 0;
 
     texture.Apply();
 
     yield return 0;
 
     byte[] bytes = texture.EncodeToPNG();
 
     // save the image
     string imagePath = "amazingPath.png";
     File.WriteAllBytes(FileManager.DATA_DIRECTORY + Path.DirectorySeparatorChar + imagePath, bytes);
 
     //Tell unity to delete the texture, by default it seems to keep hold of it and memory crashes will occur after too many screenshots.
     DestroyObject(texture);
 }

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

12 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Audio: how do I immediately play a new audioclip without delay(code included)? 2 Answers

How to create a damped harmonic oscillation? 1 Answer

[SOLVED] How to make a mesh with mobile/bumped diffuse shader transparent? 1 Answer

Destroy Object On Collision? 3 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