- Home /
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!
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.
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

Follow this Question
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