Search Unity

60fps Android game without spike? It can't be with Unity.

Discussion in 'Android' started by netics, Dec 11, 2013.

  1. netics

    netics

    Joined:
    Aug 23, 2011
    Posts:
    102
    It's really weird. But it is.
    I tested with Unity 4.2 / 4.3.
    and Galaxy S2 / Galaxy S3 / Galaxy Note 3 / Optimus G.

    You can reproduce it easily, too.

    1. Make new project without any asset.
    2. Switch to android platform.
    3. Add a cube.
    Make a material.
    Change shader to GUI/TextShader for seeing color easily without lighting.
    Set this material to cube.

    4. Add a script

    Code (csharp):
    1.  
    2. public class Test : MonoBehaviour
    3. {
    4.    Material mat;
    5.  
    6.     void Start ()    {
    7.         mat = renderer.material;
    8.     }
    9.  
    10.     void Update () {
    11.         if( Time.deltaTime > 1.0f / 57 )
    12.             mat.color = new Color(1, 0, 0);
    13.         else
    14.             mat.color = new Color(0, 1, 0);
    15.     }
    16. }
    17.  
    5. Build and Run on the device.
    6. You can see the cube flickering. WTH????
    7. If you have Pro version, you can see spikes with profiler.

    WTH? Unity can't make 60fps game without spike?????
     
    Last edited: Oct 15, 2015
  2. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Try adding this line of code in your start function? I know on iOS it defaults to 30 to save on battery life.
    Code (csharp):
    1. Application.targetFrameRate = 60
     
  3. netics

    netics

    Joined:
    Aug 23, 2011
    Posts:
    102
    Thank you for replying.

    I tried already. No effect.

    ---------------------------------------------------------------------------------------------------------------------

    For more information, I attached profiler capture.

    $profiler1.png

    red is overhead

    yellow is vsync.
     
  4. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Welp... I remember doing an fps test over a year ago and I wasn't able to get a smooth 60 fps. I'll be watching this post to see what other people have to say.
     
  5. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    Does it make a difference if you don't mess with the material? Try moving the transform or something instead.

    I wouldn't expect it to make a difference, but perhaps there's something going on with what you're doing in particular, possibly even specific to devices.
     
  6. netics

    netics

    Joined:
    Aug 23, 2011
    Posts:
    102
    Cube with color is just for unity-free users.

    The most precise result is Profiler graph.

    even empty scene (nothing without camera!!) have same result.
     
  7. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    The profiler will mess with your performance, though. There's an overhead to profiling and that will be most pronounced when you have an empty scene.

    Get your game to measure and log its own framerate with no profiler, then have a look at the recorded values and see if there are still spikes. (Of course you'll need to make sure that your logging doesn't interfere with the framerate, too!)
     
  8. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    No, it is not.

    The profiler will mess with your performance, especially when there's nothing else going on. There's an overhead to profiling and that will be most pronounced when you have an empty scene.

    Get your game to measure and log its own framerate with no profiler, then have a look at the recorded values and see if there are still spikes. (Of course you'll need to make sure that your logging doesn't interfere with the framerate, too!)

    Then do the same with an simple OpenGL application and see what happens. If you get the same spike then chances are it's something on your device causing it rather than the engine. Mobile devices do generally have a bunch of other things going on.
     
  9. netics

    netics

    Joined:
    Aug 23, 2011
    Posts:
    102
    If you can't trust Unity Profiler, then see the cube color.

    It flickers.

    Changing shader constant is very trivial job in game.

    If it makes game slower than 1/60ms, it is a problem, too.
     
  10. netics

    netics

    Joined:
    Aug 23, 2011
    Posts:
    102
    For more informaiton, I attached one more image.

    $Image012.png

    Profiler count 'overhead' is everything else(sciprt / rendering / vsync / physics / every entry profiler checks).

    I executed nearly nothing in the scene. but overhead grows more and more then boom!!!(spike!!!) then again~.
     
  11. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    I agree, but the point of that last paragraph was that we don't know just from this if the issue is from Unity at all. Can you do another test app to see if it also gets the spike?

    While there are many variables involved, I'm pretty sure I've had trivial stuff on Android devices before running at a stable 60fps. It'd be nice to know what the variables are that determine this. Devices? OS versions? Unity versions? Background processes? The phase of the moon?

    Edit: That is an interesting pattern indeed, and I'm curious as to its cause.
     
  12. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    It's been over a year, but I remember doing a test on iOS with nothing in the scene except one script that in Update() put out a Debug statement if frame rate was lower than 59 fps.

    It was literally the only script I added to a blank scene. The script did nothing else.

    And I would still see the debug statement appear. Not every frame, but often enough.

    I'm not at my work computer. I will create a test and build to an iPhone 5S and take screenshots of the results when I'm back in the office.
     
  13. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    My first guess would be the frames creating objects and the GC passing over them, creating the spike. As to why it slowly build up? Fragmentation? Memory allocation? Your guess is as good as mine.

    Also, I would note that any game running above 30 FPS is hardly noticeable, if it's not a fast pace game. I can count the number of console game with a steady 60FPS on the fingers of a single hand. I've even produced one... Which was a lot of troubles, for not a lot of results.
     
  14. netics

    netics

    Joined:
    Aug 23, 2011
    Posts:
    102
    For focusing, I can say that this is not a GC problem.

    No allocation at all. so No GC.

    All the code is the one above(first post).

    And even no code at all case(empty scene) has same result.
     
  15. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    Well, the engine obviously does its own allocation. Because YOU are not doing any, doesn't mean there is none happening. The profiler alone is probably doing its share of allocation.

    Also depends how Unity manages its stack and heap.
     
  16. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Okay, so I whipped up a script and I'm running it on an iPad 4th gen. (Couldn't find an iPhone 5S lying around...)

    I made a new project.
    Added this script to the Main Camera

    That was it.

    Here is the code and the results.

    EDIT: Added screenshot
    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class FrameRateTest : MonoBehaviour {
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.         Application.targetFrameRate = 60;
    9.         slowFrames = 0;
    10.         nextDebugMessageTime = Time.time + 10;
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.         if (Time.deltaTime > 0.01694915254237f) { // slower than 59 fps
    16.             slowFrames++;
    17.         }
    18.         if (Time.time > nextDebugMessageTime) {
    19.             Debug.Log ("[" + Time.time + "] Encountered " + slowFrames + " slow frames in the last 10 seconds.");
    20.             slowFrames = 0;
    21.             nextDebugMessageTime = Time.time + 10;
    22.         }
    23.     }
    24.    
    25.     int slowFrames { get; set; }
    26.     float nextDebugMessageTime { get; set; }
    27. }
    28.  
    $Screen Shot 2013-12-10 at 10.05.07 PM.png
     

    Attached Files:

    Last edited: Dec 11, 2013
  17. netics

    netics

    Joined:
    Aug 23, 2011
    Posts:
    102
    so.............

    iOS, too.

    Now. conclusion is... "UNITY SUCKS??"
     
  18. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    I wouldn't go that far! Even though I wish I could get that full 60 fps, I'm not ready to give up this great, reasonably priced game engine that has awesome community support. Unity might not be the best engine for fast paced action games on mobile devices (What would be? UDK?), but it drastically reduces our production time and is good enough for most of the games we make.
     
  19. mangax

    mangax

    Joined:
    Jul 17, 2013
    Posts:
    336
    Hmm, so are the other engines were able to do 60 fps all time on mobiles???
     
  20. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    Why would you want 60 FPS on mobile? Any game running at 60 FPS on mobile, 3DS or PSP?
     
  21. mangax

    mangax

    Joined:
    Jul 17, 2013
    Posts:
    336
    AHHH I didn't mean that i want 60fps (but yeah who doesn't want that :) )
    but i was asking since i don't have knowledge about other engines performance on mobile, so i was wondering if this frame rate issue thingi the author is highlighting is exclusive to unity engine or it is common in other engines as well..
     
  22. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    There's cocos2d. It's a 2d only sprite engine for iOS that can do 60 fps. Granted it is much more limited than Unity.

    Fabien Sanglard
    was able to get SHMUP to 60 fps on the original iPhone. But he wrote his own engine using C OpenGL.

    I know of UDK, but never used it. I don't know if it can do 60 fps.

    If there's anything else out there worth considering, I'd like to hear about it!
     
  23. TheShane

    TheShane

    Joined:
    May 26, 2013
    Posts:
    136
    I am guessing what you're seeing is the iOS or Android kernel throttling the CPU to save power. Here's a test I did with a Nexus 7 (2012):

    $AndroidTest.jpg

    When I'm touching the screen, the device realizes I'm interacting and it keeps a solid frame rate. I generally get a solid green cube while I'm drawing circles on the screen, maybe a couple red flickers now and then.

    I did another test with 8 cubes with rigidbodies just to give it a bit more work to do, and it actually gets slightly faster. It keeps it further under the 60fps line in general although it still gets choppier when I'm not touching it.

    I think the likely explanation is that we're seeing the artifacts of the battery-performance-balancing algorithm (which prioritizes performance over battery while the user is interacting with the device). Or I have magic fingers that make my device run faster.
     
  24. Joker_54

    Joker_54

    Joined:
    Nov 24, 2013
    Posts:
    64
    No, actually iOS as well as Android ARE turning down your GPU/CPU Power, if it's in idle state. Obviously, Unity does not affect CPU Usage while not touching the screen. Atleast that's what I'm thinking.
     
  25. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    I worked on a game that got a solid 58fps average according to that wonderful wiki fps meter script running on my galaxy s3..... got a wonderful 24fps on my moto droid :/ but it was an almost dead technology by now anyway.

    Game was Ninja:feet of fury for android/ios. Was an endless running game but seemed to run pretty smooth and was made in unity 3.5.
     
  26. matthew_bell

    matthew_bell

    Joined:
    Jan 6, 2014
    Posts:
    1
    We are getting this as well, can you test to see how frequent the spikes are when you're test device is in airplane mode? The spikes seemed to line up with the WifiMonitor State changes on our end. Turning on airplane mode reduced many of them.

    Also, if you happen to have found a solution that'd be great to hear. We're getting the spikes with and without consistent touch input.
     
  27. MightyRobot

    MightyRobot

    Joined:
    Sep 25, 2013
    Posts:
    35
    Yes there is
     
  28. AndreaP

    AndreaP

    Joined:
    Dec 28, 2012
    Posts:
    50
    We are getting a smooth 60 fps on high end devices (like a 2013 Nexus 7) on our relatively simple game (not out yet... if interested check out http://www.48hstudio.com/g_avoid.php?lang=en).
    On less powerful ones, like my S3 Mini, we gave up and settled for some occasional slowdown (an average of about 55/58, depending on the level). The background activity on the phone is also a factor outside of our control (and honestly, a 0.99$ game it's not worth the extra work needed to maybe get 1-2 fps more on old/cheap devices).
     
  29. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Don't forget to watch your garbage collection - this will most certainly cause a hiccup.
     
  30. forcepusher

    forcepusher

    Joined:
    Jun 25, 2012
    Posts:
    227
    I lol'd, i get the same issue inside empty project using default settings, while touching the screen with no power saving on galaxy s3.
    Unity 4.3.4f1
    waiting for unity 5 profiler then ?
     
  31. yaapelsinko

    yaapelsinko

    Joined:
    Nov 16, 2013
    Posts:
    102
    I've made several applications with Unity for android, each of them showing same problem.

    No matter if there is a complex scene or not, it just twitches at constant rate, usually about once a second. But if you are playing with framerate limit, twitching period could change, probably because with very limited framerate (say, 30) sometimes that spike happens 'inside' application sleeping period.

    I think it could be somehow connected to some interaction with Android OS and its sensors. For example, I've exported projects into Eclipse to do some work there, and I've seen some messages from Unity in the LogCat related to accelerometer sensor.

    So maybe you must disable something in project settings or from inside application at runtime to get rid of it?
    Because it is a real problem. It isn't about high framerate, it is about smooth gameplay. No matter if there are 60 or only 20 fps, if it will be twitching constantly, even 1000fps will not make you feel it smooth.
     
    Last edited: May 21, 2014
  32. yaapelsinko

    yaapelsinko

    Joined:
    Nov 16, 2013
    Posts:
    102
  33. Carpe-Denius

    Carpe-Denius

    Joined:
    May 17, 2013
    Posts:
    842
    I don't have any mobile devices to test, but do I see it right, that, in your colored cube example, you are creating a new material on each frame?
    Does it happen if you create 2 colors at start and switch between them?

    That won't explain your empty scene problems, but the overhead in your flicker-example.
    Does your device do anything once a second? Check GPS position, search for WLAN...?
     
  34. yaapelsinko

    yaapelsinko

    Joined:
    Nov 16, 2013
    Posts:
    102
    I'm not the topic starter, but speaking for myself, I usually don't change materials at all, but twitching is still there. I did builds with 3.5.7 and 4.3.4, for Android, for Windows standalone, did run them in the Editor - behavior still the same, twitch each second. So definitely it is not some sort of performance issues on a device side. GPS and WLAN not an issue surely.
     
  35. luispedrofonseca

    luispedrofonseca

    Joined:
    Aug 29, 2012
    Posts:
    943
    This is a very concerning problem and hopefully soon someone from Unity will see this thread... Given that the current issue tracker is as good as dead!
     
  36. yaapelsinko

    yaapelsinko

    Joined:
    Nov 16, 2013
    Posts:
    102
  37. mikhail111777

    mikhail111777

    Joined:
    Feb 8, 2013
    Posts:
    84
    Guys, I think you could be doing something wrong.

    Did you know that creating a cube from editor adds a box collider without a rigidbody? As far as I can see, you are trying to move it by modifying GameObject.transform, and physics on Android can hiccup unpredictably in such cases.
     
  38. yaapelsinko

    yaapelsinko

    Joined:
    Nov 16, 2013
    Posts:
    102
    I've deleted that component. Doesn't matter. And I've created many scenes before where was no physics-related components at all.
     
  39. mikhail111777

    mikhail111777

    Joined:
    Feb 8, 2013
    Posts:
    84
    I think you are still doing something wrong. I have several games published on Google Play running without "twitches at constant rate".

    If you have visible twitches, you should also try these suggestions: use Time.smoothDeltaTime instead of Time.deltaTime; limit FPS rate to 45, because only high end Android devices can hold at 60 FPS, and in games with high amount of dynamic motion you will notice stutter, even when FPS drops slightly.

    You should double check that there is no wrong setup of rigidbodies/colliders in the scene, because it will cause random performance spikes, which sometimes can't be profiled.
     
    Last edited: May 22, 2014
  40. yaapelsinko

    yaapelsinko

    Joined:
    Nov 16, 2013
    Posts:
    102
    Okay it is just another reason for my first game project on unity will be the last one.
     
  41. madpuppet

    madpuppet

    Joined:
    Apr 17, 2012
    Posts:
    15
    Just started a 60fps arcadey game on Unity.. having trouble even keeping 60fps on PC, let along mobile tablets.
    The scene is not empty but generally only takes 8ms to render, but spikes push it over 16ms every second so my smooth character glitches as he runs across the screen !
    Seems to be internal garbage collection. I'm careful not to generate any garbage myself, but that's about all I can do...
    Are there any smooth 60fps games out there that use Unity? I'd love to see someone cracked it so I know there's light at the end of the tunnel.

    I've been doing 60fps games since the iPad#1 with no problem with my own opengl engine - eg. Train Defense on ios... but seems I can't move a box across the screen vsync'd at 60fps on unity... :(

    I have a love/hate relationship with unity - the tools are so nice, but the end result can be very frustrating. I also very expensive for me to keep supporting all the platforms that Unity supports - I'd spend all my time on the engine instead of producing games. Was ok when I only supported IOS.

    Going to keep at it though... see of there is a way to get it close to silky smooth. So far, turning off VSync seems to give the best results - or at least make the glitches less noticable even though the whole screen is wobbling from the tears...
     
  42. madpuppet

    madpuppet

    Joined:
    Apr 17, 2012
    Posts:
    15
    ok, so, I removed all the post processing so the game runs at about 150fps average with no vsync. That is just enough to run at 60fps consistently with vsync without noticable frame spikes... (Time.delta still spikes down to about 24ms once or twice every 10 seconds, but it doesn't seem to be noticable on screen).

    Not sure if there is a better solution to actually get rid of the frame spikes other than to have enough time left over to absorb the spikes... I tried GC.Collecting every frame, but that causes about 6ms every frame and still doesn't fix the frame spikes.
     
  43. Deleted User

    Deleted User

    Guest

    hm...it seems that there is still no solution! Has anyone figured out how to pass over the stuttering/spikes?
     
  44. Sempiternal

    Sempiternal

    Joined:
    Jun 18, 2014
    Posts:
    114
    Application.targetframerate = -1; //this makes it so the game is as smooth as it can possibly be, may drain battery faster as it has higher overhead. Put this in an awake function on an empty game object.

    Then in quality settings make sure that vsync is OFF!

    I get 200-500 FPS in the editor depending on which scene im working on, objects, etc. It takes a lot to go under 60 FPS in the editor. For my Note 5, the editor pretty much reflects the performance I will get on my phone. I have silky smooth framerate in my games not only on high end games but low end as well.

    Setting vsying to 1 limits the game to around 60 FPS.

    vsync to 2 will half the framerate, it will not go higher than around 30 FPS.
     
  45. nikkouskouk

    nikkouskouk

    Joined:
    Jan 23, 2015
    Posts:
    29
    Unity version 5.2.0f3
    Thats the only script i have in my scene.
    void Update () {
    transform.position = new Vector3(transform.position.x, transform.position.y + Time.deltaTime*5);
    if(transform.position.y>8)
    {
    transform.position = new Vector3(transform.position.x,-8);
    }
    }​
    attached to a gameobject with 8 child sprites.
    Running this in my lg g3 ,sasmung galaxy s devices, genymotion and bluestacks emulators.
    Although i have fixed 60 fps ,the movement is jerky.Can someone test this?
     
  46. Ali_V_Quest

    Ali_V_Quest

    Joined:
    Aug 2, 2015
    Posts:
    138
    Any updates on this ?
     
    MrEsquire likes this.
  47. UnityLighting

    UnityLighting

    Joined:
    Mar 31, 2015
    Posts:
    3,876
    Do you tested it with V-Synce Every V Blank in unity 5?
    I've tested it before and works smooth without spike
     
  48. JonPQ

    JonPQ

    Joined:
    Aug 10, 2016
    Posts:
    120
    the problem above is changing the material or material color causes unity to clone the material everytime you change it. creating lots of garbage, eventually the garbage collect cleans it up. and you get a slow down spike. Probably also explains that nice yellow and red graph way up there in the thread. You'll want to edit .sharedMaterial, instead of .material. to avoid the garbage.
     
  49. netics

    netics

    Joined:
    Aug 23, 2011
    Posts:
    102
    Read the code again.
    There is only one time initialization of material.
    Chaning material.color property don't clone material itself.
     
  50. iamsidv

    iamsidv

    Joined:
    Jan 24, 2013
    Posts:
    17
    Check once with using a shared material and add a 'f' after 57.
    More simpler way would be replacing 1/57 with 0.017543f.
    Check again if there's any increase in performance.