Search Unity

2D massive stutters on iOS device caused by swapping Overhead and Device.Present

Discussion in 'iOS and tvOS' started by AndrewK, Feb 16, 2014.

  1. AndrewK

    AndrewK

    Joined:
    Sep 14, 2012
    Posts:
    38
    I am trying to make 2d platform game on mobile device, but it is impossible using that version of Unity to make it on iPhone4.

    To debug this I've used Unity 2d official demo and stripped it from everything except simple background and hero player, Of course there is some bug with animator which in use with rigidbody causes massive stuttering, and Unity follow camera script should use LateUpdate instead of FixedUpdate which are in official demo, so I changed it.

    Unfortunately every few seconds (which is visible in profiler) I see that Unity changing Overhead process to Graphics.PresentAndSyncProcess which I do not underrstand because in scene is nothing changing, and as these processes changes between each other there is massive spikes 165ms of Overhead!!! which causes dramatically stutters on iPhone4.

    The project is that simple (even in web player that stutters are visible):
    https://dl.dropboxusercontent.com/u/46028967/WWW/WWW.html

    Is it some weird internal Unity bug? Cause it makes impossible to make anything on iPhone4, maybe on other devices too.
    Can someone help me with that? I cant find anything related to this problem and it is making Unity completelty useless to me in 2d projects.

    here is the profiler in one of those moments:

    $Zrzut ekranu 2014-02-16 o 13.59.28.png

    $Zrzut ekranu 2014-02-16 o 13.59.59.png

    $Zrzut ekranu 2014-02-16 o 14.00.27.png

    $Zrzut ekranu 2014-02-16 o 14.00.34.png


    Here is the project if someone wants to look at:
    https://dl.dropboxusercontent.com/u/46028967/2DTest.zip
     
  2. AndrewK

    AndrewK

    Joined:
    Sep 14, 2012
    Posts:
    38
    Can someone help me with that?
     
  3. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Can I see it with Alloc being highlighted as opposed to time ms? I suspect it's collecting garbage at that point causing the stutter. Look around for Alloc and scrub back and forth over time, see which ones pop up to the top.
     
  4. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    guiText.text = "Score: " + score;
    guiText.text = guiCopy.guiText.text;

    Using gameObject.name or .tag will also trigger spikes. Work around all these issues.

    Start caching components. Stop using .GetComponent or such during Update loops. Put them in variables instead.

    Don't allocate new arrays or anything while game is running.

    This alone will create tonnes of garbage and spikes. Put simply, you can't do it every frame. Either only do string manipulation when it changes or once every half second or so, otherwise it'll spike like a bastard and often.

    This is because strings in C# are immutable, so it has to create a new string each time, which it then later discards causing memory to be allocated and then freed by the garbage collector.

    http://forum.unity3d.com/threads/22...edback/page2?p=1521902&viewfull=1#post1521902

    This sort of thing can be used to completely bypass garbage collection for text. I'm not on Unity 4.3 yet so I can't test your code. I am just looking through it for little issues.
     
  5. AndrewK

    AndrewK

    Joined:
    Sep 14, 2012
    Posts:
    38
    Thank you, but I am not allocating anything in update functions.
    One and only thing that is goimng on in Update is that in CameraFollow script:

    Code (csharp):
    1.  
    2.         void TrackPlayer ()
    3.         {
    4.                 // By default the target x and y coordinates of the camera are it's current x and y coordinates.
    5.                 float targetX = transform.position.x;
    6.                 float targetY = transform.position.y;
    7.  
    8.                 // If the player has moved beyond the x margin...
    9.                 if(CheckXMargin())
    10.                         // ... the target x coordinate should be a Lerp between the camera's current x position and the player's current x position.
    11.                         targetX = Mathf.Lerp(transform.position.x, player.position.x+7f, xSmooth * Time.deltaTime);
    12.  
    13.                 // If the player has moved beyond the y margin...
    14.                 if(CheckYMargin())
    15.                         // ... the target y coordinate should be a Lerp between the camera's current y position and the player's current y position.
    16.                         targetY = Mathf.Lerp(transform.position.y, player.position.y, ySmooth * Time.deltaTime);
    17.  
    18.                 // The target x and y coordinates should not be larger than the maximum or smaller than the minimum.
    19.                 targetX = Mathf.Clamp(targetX, minXAndY.x, maxXAndY.x);
    20.                 targetY = Mathf.Clamp(targetY, minXAndY.y, maxXAndY.y);
    21.  
    22.                 // Set the camera's position to the target position with the same z component.
    23.                 transform.position = new Vector3(targetX, targetY, transform.position.z);
    24.         }
    25.  
    I even disabled FPS counter when you said about guiText.
    GC Alloc says that there is nothing allocated if I am correctly understand that profiler screens:

    $Zrzut ekranu 2014-02-18 o 01.13.23.png

    $Zrzut ekranu 2014-02-18 o 01.13.11.png
     
  6. atmuc

    atmuc

    Joined:
    Feb 28, 2011
    Posts:
    1,166
    can you record a video of your iphone screen and share it here?
     
  7. AndrewK

    AndrewK

    Joined:
    Sep 14, 2012
    Posts:
    38
    Last edited: Feb 18, 2014
  8. atmuc

    atmuc

    Joined:
    Feb 28, 2011
    Posts:
    1,166
    i think problem is about background(clouds). what is your unity and ios version? i will check your project with my iphone 4. have you tried it with non development build?
     
  9. AndrewK

    AndrewK

    Joined:
    Sep 14, 2012
    Posts:
    38
    I do not think that problem is in the clouds textures. I have other project with few tiny textures, and there is the same problem.

    My Unity version is 4.3.4f1 (last version), but tried it also with the previous one (4.3.3), and there is no difference.
    My iOS version is also last one - 7.0.4

    I have tried with non development, yes, but there is no defference.
     
  10. rextr09

    rextr09

    Joined:
    Dec 22, 2011
    Posts:
    416
    I hope you will find a solution to this. I'm tired of repeating stuttering issues since 4.x. I have even submitted a bug report. But they don't "see" it.
    I wish I can go back to 3.5.7.
     
  11. Emirk

    Emirk

    Joined:
    Nov 26, 2012
    Posts:
    17
    I have the exact same problem. Same Unity version and everything. I'd very much appreciate if someone could explain the cause of this.
     
  12. atmuc

    atmuc

    Joined:
    Feb 28, 2011
    Posts:
    1,166
    can you try Time.smoothDeltaTime instead of Time.deltaTime? if it does not work try to get average of the last 30 deltaTime and use it instead of Time.deltaTime.
     
  13. sledgeman

    sledgeman

    Joined:
    Jun 23, 2014
    Posts:
    389
    From Lylek23
    May 19, 2014 02:45
    I have this problem even in the simplest of scenes on my Android, using Unity version 4.3.4.

    I found that if you go to Edit > Project Settings > Time and lower the Maximum Allowed Timestep it prevents this stuttering. Until this bug is addressed, it may be a decent temporary fix. Hope it helps.
    Fixed Timestep: 0.02 / Maximum Allowed Timestep: 0.02 / Time Scale: 1