• 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
Question by Dheinamar · Jul 17, 2019 at 12:59 PM · yieldscreenshotasyncasynchronousscreencapture

Take screenshot and use it as texture using ScreenCapture

I need to take screenshot and hold it in memory so later I can serialise it in save file with all the other information I need to save. We're using Unity 2019.3.0a8 and as documentation says for taking screenshots there is class ScreenCapture for w$$anonymous$$ch I wasn't able to find any information except for Unity Scripting API. So I have two questions.

First, as documentation says I need to wait till the end of frame rendering and the proper way of doing so is (taken from ScreenCapture.CaptureScreenshotAsTexture)

IEnumerator TakeScreenshot() { yield return new WaitForEndOfFrame(); var texture = ScreenCapture.CaptureScreenshotAsTexture(); // do somet$$anonymous$$ng with texture // cleanup Object.Destroy(texture); }

However if I'm using yield return, I can't have neither return value for the method nor ref parameter thus I can't use the screenshot inside the method in w$$anonymous$$ch I call StartCoroutine(TakeScreenshot());. I was t$$anonymous$$nking of using async/await but it allows me to wait only for some amount of seconds and not exactly till the end of frame. How can I wait for end of frame rendering without using yield?

Second question here.

Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by kala-hari · Jul 17, 2019 at 06:53 PM

One way to solve the issue with returning values from Coroutines is passing a Callback to the Coroutine method.

The Callback can be called after the screenshot is taken, and receive the result as a Parameter.

For example:

  void HandleScreenshot()
  {
      StartCoroutine(TakeScreenshot((result) => {
          FurtherProcessing(result);
      });
  
  }
  
  IEnumerator TakeScreenShot(System.Action<ResultType> callback)
  {
      yield return new WaitForEndOfFrame();
      var result = new ResultType(ScreenCapture.CaptureScreenshotAsTexture());
      callback(result);
  }

Don't forget to destroy the texture aftwards.

Comment
Dheinamar
Dragate
Apaksi

People who like this

3 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 Dheinamar · Jul 28, 2019 at 01:29 PM 0
Share

I've just added all further processing into TakeScreenshot. I just don't know why didn't I do this in first place. Anyway, thank you for the help.

avatar image kala-hari · Jul 29, 2019 at 07:42 AM 0
Share

Glad I could help :)

avatar image

Answer by WookieWookie · May 06, 2022 at 08:49 PM

I get t$$anonymous$$s error that is a known bug but only has 2 upvotes:

Metal: GrabIntoRenderTexture mismatched grab pass: 80 / 70

Comment

People who like this

0 Show 0 · 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.

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

114 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 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 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 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 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

Asynchronous Level Load Progress on iPhone 1 Answer

StartCoroutine and Yield 0 Answers

Is it possible to create a Sprite at runtime async? 0 Answers

Creating a fade out and in transition for loading. Functions not executing in order. 1 Answer

await creates error with StreamReader 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