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 Ben Blaut · May 24, 2012 at 03:31 PM · graphicsvideothreadsrecording

Threading Application.CaptureScreenshot()

I know that the Unity API does not play nice with threading, as evidenced by the errors that were returned when I tried and the comments throughout this site. However, while looking around I saw in this question that someone claimed to be able to thread Application.CaptureScreenshot, among other capture methods. For the others, I understand that he was threading the actual encoding after the Unity method was called. However, I fail to see how he could make Application.CaptureScreenshot work. When I currently use Application.CaptureScreenshot without slowing down game time (with Time.captureFramerate), it runs at about 6 fps regardless of CPU power (tested on multiple) due to the amount of work it's doing writing to disk. In the link, he claims to have been able to make it run at 30+ fps using threading. Am I missing how this is possible, or is his claim a lot more complicated than he's making it sound?

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
1

Answer by tomka · May 25, 2012 at 07:33 AM

I have had success threading a texture2d capture, I haven't tried threading CaptureScreenshot but I have had success getting pixel data out of a Texture2D. Note: the following solution still affects your framerate, but if you can optimise it by

  • reducing the area of of the area of the screen you capture

  • scaling down the captured texture2d to reduce bytes written to disk

You could try doing something similar with Application.CaptureScreenshot() but I'm not sure how that will behave with threads.

 Thread workerThread;
 SaveFile worker;

 void Start()
 {
    Texture2D texture = new Texture(Screen.width, Screen.height);
    texture.ReadPixels(new Rect(0f, 0f, Screen.width, Screen.height);
    byte[] dataToSave = texture.EncodeToPNG();
    worker = new Worker(dataToSave);
    workerThread = new Thread(new Thread.ThreadStart(worker.SaveData));
 }

 class SaveFile
 {
     byte[] data;
 
     public SaveFile(byte[] data)
     {
         this.data = new byte[data.Length];
         data.CopyTo(this.data, 0);
     }

     public void SaveData()
     {
         System.IO.File.WriteAllBytes("screenshot.png", data);
     }
 }
Comment
Add comment · Show 2 · 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 whydoidoit · May 25, 2012 at 08:40 AM 0
Share

You could also lower the priority of the saving thread to improve the impact on the frame rate.

avatar image Ben Blaut · May 29, 2012 at 01:05 PM 0
Share

I was able to get this integrated into my project, but it actually resulted in worse fps than a straightforward Application.CaptureScreenshot(). It dropped to about 2 fps with this. Were you able to get a frame rate that wasn't atrocious with a decently sized video with this method, or did it only work for small captures?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Screenshot Movie with Audio Capture 1 Answer

Capture Screen Script 0 Answers

Record video on iPhone or Android? 6 Answers

Creating a video from a set of frames 0 Answers

Recording Output from a Camera 2 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