- Home /
Running a function in the background
This is probably a super simple answer but due to my ignorance I'm not sure where to start. My code takes a series of pictures with a delay between pictures.
//Do some GUI stuff
yield WaitForSeconds(.7);
webCamPic1 = WebCamManager.TakePicture();
//Do some more GUI stuff
yield WaitForSeconds(.4);
webCamPic2 = WebCamManager.TakePicture();
//Do some more GUI stuff
yield WaitForSeconds(.4);
webCamPic3 = WebCamManager.TakePicture();
//Complete... show captured pictures
I noticed specifically on my iPad, that the TakePicture function (which just returns a Texture2D with a still shot from the WebCam) really slows down this process so this process takes longer than the WaitForSeconds. I want this to happen in the background and let the commands between continue to run and not have this freeze my game. The code in between is timed specifically for an effect and this delay is ruining it.
How would I go about tackling this?
You should not put any long process or algroythm in the OnGUI() function if that's what you did.
What you are asking for is called asynchronous processing. Unity doesn't do Async very well, but you can do it with your own C# class. Just make sure it doesn't touch anything unity owned.
Your answer
![](https://koobas.hobune.stream/wayback/20220613150717im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Interrupting a long process: progress watching? 1 Answer
How can I get the current location using Input.Location from the background worker? 0 Answers
Limit rate that background thread works at 1 Answer
Background Networking Activity and Unity Run Loop 0 Answers
Why sprite are semi transparent 4 Answers