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 Lunaria · Feb 26, 2015 at 12:29 PM · coroutineasynchronous

How to plot the script asynchronously?

Hello, developers

I'm going on my project which webcam shows view and captures its image when some frames reaches.

But there's a problem. When capturing process goes on, showing process stops, and when capturing is done, showing also restarts. So it looks so oddly.

So, I thought it would be done with threading or async. But Unity doesn't support multi-threading programming, nor async.

Instead of those, many people say to use coroutine.

I know how to use coroutine, but I don't know how to apply to my case.

 void TakeScreenShot()
 {
     Texture2D snap = new Texture2D(1280, 720, TextureFormat.RGB565, false);
     snap.SetPixels(back.GetPixels());
     snap.Apply ();

     bmpz = snap.EncodeToPNG ();
     System.IO.MemoryStream ms = new System.IO.MemoryStream (bmpz);
     ms.Seek (0, System.IO.SeekOrigin.Begin);
     
     Image _bmp = Bitmap.FromStream (ms);
     _bmp.Save (_SavePath + "Image.bmp", ImageFormat.Bmp);
     ms.Close ();
 }
 void Start () {
         
         WebCamDevice[] devices = WebCamTexture.devices;
 
 
         WebCamTexture back = new WebCamTexture (devices[0].name, 1280, 720);
         back.Play();
         renderer.material.mainTexture = back;
 
     } . . .
 void Update(){
     if ((frameCounter % 150 == 0){
    TakeScreenShot(); << **problem begins**
     }
     . . .

}

or is it possible that switch main thread to background thread, while screenshot thread to main thread?

Any suggestion would be my help! Thanks for reading!

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 steakpinball · Feb 26, 2015 at 04:39 PM

The problem you are experiencing is that a frame is taking a long time to complete its code compared to the others. Threads can be used inn Unity, they just can't access the Unity API. If all you do is do something without using Unity classes. you can run it on another thread. Coroutines might be useful in this case. They aren't true concurrency as they run on the main thread with your other code. The main use of coroutines is they can be paused to allow the game loop to continue at a good pace and prevent the stuttering effect you are experiencing. The yield statement in a coroutine pauses your method. The thing you return determines when it will resume. Texture2D.SetPixels, Texture2D.Apply, Texture.EncodeToPNG, and Image.Save all potentially take a "long" time. These things can be broken up to take place across several frames. yield return null pauses your method until the next frame. Here is your method as a coroutine.

 IEnumerator TakeScreenShot()
 {
      Texture2D snap = new Texture2D(1280, 720, TextureFormat.RGB565, false);
      snap.SetPixels(back.GetPixels());
      yield return null; // This one may not be needed depending on how fast SetPixels/GetPixels runs
      
      snap.Apply ();
      yield return null;
  
      bmpz = snap.EncodeToPNG ();
      yield return null;
      
      System.IO.MemoryStream ms = new System.IO.MemoryStream (bmpz);
      ms.Seek (0, System.IO.SeekOrigin.Begin);
      
      Image _bmp = Bitmap.FromStream (ms);
      _bmp.Save (_SavePath + "Image.bmp", ImageFormat.Bmp);
      ms.Close ();
 }

Start it by StartCoroutine(TakeScreenshot());

Be careful bmpz isn't modified while the coroutine is paused since it isn't a local variable.

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 Lunaria · Feb 28, 2015 at 02:33 AM 0
Share

Thanks brianturner, and sorry for late. I've tried this way. Yes, the delay was reduced few frame, but it still delayed. I think, - as you said - this function always makes few delays.

Thanks for kind answers!

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

20 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

Related Questions

StartCoroutine and Yield 0 Answers

Unity interthread communication. 1 Answer

How to load scene when async.progress is 0.9? 0 Answers

How to make functions async? 2 Answers

Async parse.com query not working on Android/iOS 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