Search Unity

Generating lots of data through coroutines

Discussion in 'Scripting' started by Velcrohead, Mar 3, 2015.

  1. Velcrohead

    Velcrohead

    Joined:
    Apr 26, 2014
    Posts:
    72
    Hi guys,

    Apparently I've been under a bit of an illusion regarding co-routines, so I've decided to try and get to grips with them.. However, I'm still a bit lost.

    In my game I'm trying to generate some data at runtime, but due to the amount of it it's causing a very slight stuttering during play. Not the end of the world, but something I'd like to address early on.

    I'm hoping that I'd be able to spread this generation out over a number of frames, but even with my playing around with coroutines I'm still getting the same issues.

    My code is basically 2 for loops that fill an array with data. How would I go about breaking this up and splitting it over multiple frames, rather than one? I would appreciate any ideas.

    Code (CSharp):
    1. for(int i = 0; i < 100; i++)
    2. {
    3. for(int j = 0; j < 100; j++)
    4. {
    5.   // Project specific code goes here
    6. }
    7. }
    I have also toyed with the 'waitforseconds' functionality, even setting it to wait a whole second before continuing on, with no luck.

    Thanks in advance,
     
  2. jpthek9

    jpthek9

    Joined:
    Nov 28, 2013
    Posts:
    944
    A coroutine is simply something that runs after the update function. Every time you yield, you're waiting until the next fraim. When you do WaitforSeconds (x), every update frame the elapsed time is checked against x. If the elapsed time is more than x, continue on. If not, yield for another frame. Basically, the coroutine allows you to spread a task out so if you're generating 10000 rocks, you can generate 10 a frame which is a lot more frame-rate friendly than generating all those rocks at one time.
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,224
    Sounds like something that should use threads, not coroutines.

    --Eric
     
  4. Velcrohead

    Velcrohead

    Joined:
    Apr 26, 2014
    Posts:
    72
    Yeah that's kinda what I was thinking.

    I've changed the code to be

    Code (CSharp):
    1. for(int i = 0; i < 100; i++)
    2. {
    3. for(int j = 0; j < 100; j++)
    4. {
    5.   // Project specific code goes here
    6. }
    7. yield return new WaitForSeconds(0.1f);
    8. }
    But I'm still getting the framerate drop every time (as if it was a normal function). Any ideas?

    I was under the impression we didn't have access to threads in Unity?
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,224
    Sure we do, just not the Unity API. But since the question specifically says "fill an array with data", that seems to have nothing to do with the Unity API.

    --Eric
     
  6. Velcrohead

    Velcrohead

    Joined:
    Apr 26, 2014
    Posts:
    72
    Okay yeah, it did say that. My bad. It does actually do Unity specific stuff too, I hadn't realised I was being that specific. There's also mesh generation going on, and I'm looking for a way to split the load over multiple frames, rather than one.
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,224
    You can generate the mesh stuff in threads (since it's just data in arrays), then when it's done signal the main thread to assign the mesh.

    --Eric
     
unityunity