Search Unity

Is it always good to have one unity update

Discussion in 'Scripting' started by Crispried, Aug 20, 2017.

  1. Crispried

    Crispried

    Joined:
    May 23, 2016
    Posts:
    24
    Good day!
    Recently I decided to use something like update manager, which puts all "custom updates" in one Unity update. So, the performance of this was really great, scene, which gave around 100 fps presented 500-700 (large fluctuations). It seems that everything should be really nice, but how surprised I was when on android I've got troubles with performance. Surely, I increased fps to 60 (because it's 30 for phones by default), but anyway in profiler I've got huge spikes (5-10 fps). But it's not all, I had some spawn logic which based on time. And I used deltaTime to make this logic the same for all the platforms. It looks like
    Code (CSharp):
    1. private float spawnRate = 3f;     // how quickly we want spawn objects
    2. private float timeSinceLastSpawn = 0.0f;
    3.  
    4. public override void UpdateMe()
    5. {
    6.     timeSinceLastSpawn += Time.deltaTime;
    7.     if (timeSinceLastSpawn >= spawnRate)
    8.     {
    9.         timeSinceLastSpawn = 0f;
    10.         // some logic to spawn object
    11.     }
    12. }
    And what do you think? Everything great in editor, everything great with build for Windows and WebGL build. But objects wasn't spawned every spawnRate time on Android devices! They spawned really randomly (the distance between them are random). And the only why it could be is different Time.deltaTime! But when I changed UpdateMe on Unity Update the problem was gone.

    Despite the assertion that Unity scripts works on one thread I think that after compiling for android device (at least for android, sadly I can't check how it would work on IOS) there are some optimization like executing every MonoBehaviour in own thread (because mobile devices, unlike computers, have a large number of cores with a low frequency). If it is so it means that it's much better to have many small updates instead one large, even despite on every update has a lot of additional actions what are bad for performance.

    So what do you think about it?
     
  2. Scabbage

    Scabbage

    Joined:
    Dec 11, 2014
    Posts:
    268
    I think you should just use Coroutines with a while loop and WaitForSeconds in them.
     
  3. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,448
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    I highly doubt this is the case! Putting code that interacts with the engine on a different thread would be a huge gain for Unity, and it would be madness if they had solved that problem, but only used the solution on mobile!

    How big is the discrepancy here? Are we talking more than a single frame? In that case, something really fishy is going on. If your UpdateMe is getting called several times each frame, that's easy to check for:

    Code (csharp):
    1. private int lastUpdateFrame = -1;
    2.  
    3. void UpdateMe() {
    4.     if(Time.frameCount == lastUpdateFrame) {
    5.         Debug.LogError("More than one update on the same frame!");
    6.     }
    7.     else if(lastUpdateFrame != -1 && Time.frameCount != lastUpdateFrame + 1) {
    8.         Debug.LogError("More than one frame since last update!"):
    9.     }
    10.  
    11.     lastUpdateFrame = Time.frameCount;
    12. }
     
    Kiwasi likes this.