Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Speeding up random generation... or hiding it away?

Discussion in 'Scripting' started by ProjectCryken, Oct 6, 2014.

  1. ProjectCryken

    ProjectCryken

    Joined:
    Jul 25, 2012
    Posts:
    41
    I am randomly generating a bunch of spaceships the moment I spawn them in. This essentially consists of a LOAD of while loops that run through each segment and does a bunch of stuff. It all works great... except it takes 2-3 seconds on my beefy computer to run through. This is a problem when I need to spawn them in realtime, as it results a no new frames being rendered during this time. Is there a way to do all this intensive stuff while still updating frames... and then enable the spaceship? So when I spawn the ship, it will take a few seconds to appear, but there will be minimal performance drop during this time.

    The random generation is about as streamlined as I can get it and will only get slower as I add new stages.

    Thanks,
    Dan
     
  2. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Since there's no details (don't even know if it's sprites or 3D) then can't help. It's such an amazingly vague question.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,501
    One useful pattern in Unity3D is to make the creation function into a coroutine that yields in between each "piece" of the spaceship's construction, and upon completion it calls a separate System.Action to say "hey, here's what I made, it's ready to use."

    Depending on how heavy each chunk is, it might just result in adequate performance for you.

    (Obviously, if at all possible, pre-load everything before the level starts, but I will assume that this isn't possible in your current design.)

    Kurt
     
  4. ProjectCryken

    ProjectCryken

    Joined:
    Jul 25, 2012
    Posts:
    41
    The algorithms I'm actually doing shouldn't be relevant, nor should what I'm rendering at the end of it. The intensive part is actually generating the raw data, which I want to do over time to prevent slow down. Its just a bunch of loops in functions called on Start.

    Each chunk of code is pretty heavy, even with delays in between each loop, the framerate is crippled.
     
  5. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,077
    Coroutine?
     
  6. ProjectCryken

    ProjectCryken

    Joined:
    Jul 25, 2012
    Posts:
    41
    Put everything into a coroutine and yielded for a frame at appropriate points. Works great, except it takes twice as long for someone playing at 30fps than someone at 60fps. Not a big deal though.
     
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    1) You can use Time.deltaTime to make it take the same amount of time on different framerates. The better solution, though, is:
    2) Pregenerate the ships; have gameObject.active set to false; and turn them on all at the same time.
     
    TonyLi likes this.
  8. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,077
    Thanks. I wasn't asking what a coroutine was, I was just suggesting it as a possibility. ;)
     
  9. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    This is a really good suggestion. Pregenerate everything you can get away with. Nothing is ever truly fully random, and in practice it only needs to be different enough to seem random to the player.