• 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 AstrobearGames · Aug 09, 2016 at 02:40 PM · unity 5editorapplicationscreenshot

Can't capture Multiple Screenshots in one Second

Hey

I am having trouble saving Multiple Screenshots really fast in Unity using Application.CaptureScreenshot

It appears that the Capture Screenshot function is only saving out one file per second

It is reporting multiple inputs second and each of them have a different filename so to avoid overwriting the previous screenshots. However Unity only seems to be saving the last screenshot recorded in that second.

It is not exactly needed to save them t$$anonymous$$s fast, but it would be nice to figure out how to make t$$anonymous$$s work in the event I ever do need to have them saved quickly.

Code:

 public static class ScreenshotHelper
 {
     static DateTime m_LastTime = new DateTime(0);
     static int m_LastNumber = 0;
 
     [MenuItem("Tools/Save Screenshot &P")]
     public static void TakeScreenshot()
     {
         if (m_LastTime.Approximately(DateTime.Now)) { m_LastNumber++; } else { m_LastTime = DateTime.Now; m_LastNumber = 0; }
 
         string t_Filename = Application.productName + " - " + m_LastTime.ToString("yyyy-MM-dd HH-mm-ss");
         if (m_LastNumber != 0) { t_Filename = t_Filename + " (" + m_LastNumber + ")"; } 
 
         string t_FilePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\" + t_Filename + ".png";
 
         Debug.Log("Screenshot Saved to \"" + t_FilePath + "\"");
         Application.CaptureScreenshot(t_FilePath);
     }
 
     static bool Approximately(t$$anonymous$$s DateTime a_Self, DateTime a_CompareTo)
     {
         if (a_Self.Year != a_CompareTo.Year)        { return false; }
         if (a_Self.Month != a_CompareTo.Month)        { return false; }
         if (a_Self.Day != a_CompareTo.Day)            { return false; }
         if (a_Self.Hour != a_CompareTo.Hour)        { return false; }
         if (a_Self.Minute != a_CompareTo.Minute)    { return false; }
         if (a_Self.Second != a_CompareTo.Second)    { return false; }
 
         return true;
     }
 }

Debug Log Output: alt text

Actual File Output: alt text

example-debugoutput.png (49.7 kB)
example-fileoutput.png (21.7 kB)
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
Best Answer

Answer by Yujingping · Aug 09, 2016 at 06:18 PM

Hi there. I have successfully reproduced your issue in Unity 5.4.0f3 and the response is exactly what you have described. Perhaps t$$anonymous$$s is a bug or so and lets consider reporting it to the official. However just to solve your problem, I t$$anonymous$$nk you may try ReadPixels w$$anonymous$$ch allows you to dynamically control the parameters of the screenshot. Here is my example code:

 private IEnumerator TakeScreenshot ()
     {
         yield return new WaitForEndOfFrame ();
         Texture2D screenShot = new Texture2D (Screen.width, Screen.height, TextureFormat.RGB24, false);
         screenShot.ReadPixels (new Rect (0, 0, Screen.width, Screen.height), 0, 0);
         screenShot.Apply ();
         byte[] bytes = screenShot.EncodeToPNG ();
         int index = ImageNumber + 1;
         ImageNumber++;
         string fileName = Application.persistentDataPath + "/Screenshot" + index + ".png";
         System.IO.File.WriteAllBytes (fileName, bytes);
     }

I have tried t$$anonymous$$s and it works pretty fine. Please note that don't miss "yield return new WaitForEndOfFrame ();" at the first line in the Coroutine. Otherwise it may not work as predicted. Do not take screenshots too frequently on mobile platforms. I have experienced crash when doing so. Also please note that if you wanna to control the size, you may compress the PNG file by "screenShot.Compress ();". However t$$anonymous$$s does not work on iOS for some unknown reason. If you have further questions please feel free to contact me. GL & HF!

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 AstrobearGames · Aug 10, 2016 at 04:47 AM 0
Share

This worked great for me, except for the fact that it requires to be on a monobehaviour for the coroutine to work.

Here is my final version I came up with Here on GitHub Gist

For anyone who happens to come across this, it works on PC fine, might need some tweaking for other keyboard layouts and/or platforms

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Which version of JDK, SDK and NDK Unity 2019.2.21f1 require? 0 Answers

InitializeOnLoad: Gameobject being destroyed? 1 Answer

AdMob in Editor? 1 Answer

Creating a software for drawing diagrams 4 Answers

Everytime Unity Editor is launched. I am asked to Sign into your Unity Account 4 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