Search Unity

is there a way to split frame time for different tasks

Discussion in 'Scripting' started by pretender, Apr 17, 2019.

  1. pretender

    pretender

    Joined:
    Mar 6, 2010
    Posts:
    865
    for example 3/4 of frame time to camera processing and 1/4 for something else, i suppose i should use coroutines? any ideas? thanks~!
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,634
    Why would you want to do that?
     
  3. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    676
    If you can get all the work you need to do completed in a single frame, I would guess there's no reason to allocate portions of that time. On the other hand, if you can't get everything done, then I can see how you might want to assign priorities that apportion more time to one task than to another. Coroutines are one option, but there are others. Which one will work best for you depends (as things always do) on the specifics of what you are trying to.

    For example, you could have a coroutine for camera processing that ran three out of every four times it was called, and another coroutine for something else that ran one out of every four times it was called. On average, that would dedicate three-fourths of your time to camera processing and one-fourth to something else. This would almost certainly be a bad idea, though, as coroutines are called synchronously, meaning that every time a coroutine yielded (assuming it did so within one frame time), you'd end up wasting time you could be using doing some kind of processing.

    Another option is to write multithreaded code. That's complicated and, while threads can be prioritized, prioritizing them in a continuous-loop context doesn't always do what you expect (a high priority thread can prevent a lower priority thread from ever running, for example).

    Tell us a bit more about what you are doing. Maybe we can be more specific.
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If I was trying to limit how long some code in a loop is allowed to run I'd probably monitor how long I've spent using DateTime.Now.Ticks.