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

(SOLVED) Huge FPS Drop on Android Game

Discussion in 'Android' started by NickCanz, Dec 4, 2020.

  1. NickCanz

    NickCanz

    Joined:
    Nov 3, 2020
    Posts:
    16
    Hi I have a problem with my 3D Game (Sorry for my bad english). It is inspired to Space Invaders and after more time the fps go down from 60 to 4. I tried to deactivate some GameObjects but i don't had any results. I try to deactivate IsKinematic on the player and on the enemy but i have the same problem. Can you help me?
     
  2. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,495
    This could be due to thermal throttling (look up "Adaptive Performance" for more info, even if you're not using that package you can still apply some of the techniques and will learn useful stuff in the course of research), though 60 to 4 is quite a jump.. so another possibility to consider - since you're mentioning disabling GameObjects - you could be spawning content and not removing it again (via Destroy) so the scene continues to grow indefinitely.

    If you're not pooling already you might also look up "object pooling".. it's a better approach than just Instantiate/Destroy, because you recycle your objects, so it takes less memory, has better performance and no GC allocation aside from warming up the pool (pre-Instantiating the maximum number you will need, or at least 1 to eliminate any stutter that can occur the first time you're instantiating one). For example if you have at most 10 enemies on the screen at once, the same GameObject of an enemy goes back into the pool when it's hit, then instead of calling Instantiate again for another enemy, you re-initialize that object (i.e. create something like an "Initialize" function, where you'd call SetActive(true), reset whatever states, health and/or armor back to max and so on).
     
    Onragca likes this.
  3. NickCanz

    NickCanz

    Joined:
    Nov 3, 2020
    Posts:
    16
    Yeah i used Instantiate/Destroy but i dont think that the problem is this. I tried to investigate more and i can see that the problem happened when i press the shoot button and the enemy have spawned. I tried to disable the Enemy Spawner but i have the same problem. So you have other soluctions apart from the pooling?
     
  4. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,495
    You'll need to post more details and related code. It'll be difficult getting help without more to go on - i.e. is the enemy simple (like just a quad using a shared material), or..? Are there particle systems/effects triggered when this happens? Environment complexity/interaction? Did you follow a specific tutorial/example project or did you write it from scratch? Does this happen in the editor, the build, both? Does the framerate drop only happen for a short period of time, or does it continue to be bad once it happens? Are there errors in the console when it happens? You mentioned it happens when you press the shoot button and the enemy has spawned - but that the issue still happens when you disable the Enemy Spawner - which implies that's unrelated to the issue.

    The first thing to do is figure out exactly the steps you can take to reproduce the problem consistently, then explain this in detail along with related code. Also, mobile development is tricky where performance is concerned in general, if you're just getting started, it might be better to create something simple just on your desktop/laptop until you're familiar with debugging, using the profiler, etc. I'll watch the thread, if I can offer any further suggestions based on what you post I'll have more to say, otherwise hopefully someone else here will be able to help more :)
     
  5. NickCanz

    NickCanz

    Joined:
    Nov 3, 2020
    Posts:
    16
    Yeah you are right. So i here there is a video of the moment when the FPS drops down and if you want i can give you the project or the codes or anything you want!;).
    Watch the video and than ask me what do you want to see and i will do screenshots or other videos.



    In the editor the problem doesn't appear so i think that some settings of unity for the mobile devices are wrong. I don't have any idea for the cause because other times i don't have this problem and i can run the game without any lag. I hope that someone can help me!
     
    Last edited: Dec 4, 2020
  6. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,495
    Lookin' good (aside from the lag) - okay what you might start with is..
    1. Go to Window->Analysis->Profiler, then enter Play Mode.
    2. Select "Memory" Profiler Module (click on the left side list if it's open, or open it first from top left dropdown),
    3. Click "Take Sample Playmode" above the panel at the bottom (assuming layout is same as my version).
    It has "Scene Memory" listed there after it's done (which can take a while depending how complex the scene is), look at the number of objects. Unpause the game and let it go ahead to where it's getting laggy. Click "Take Sample Playmode" again and look at the number of objects in the scene. If it's drastically higher, there's most likely a problem somewhere of calling Instantiate without a corresponding Destroy.

    See if that's related to the problem (or ruling it out, if the number of objects in Scene Memory isn't significantly different).
     
  7. NickCanz

    NickCanz

    Joined:
    Nov 3, 2020
    Posts:
    16
    I tried to analize the memory but when the game is not operative the Scene Memory use 0.6MB and after the moment, when in the phone there is the lag, the scene memory use 0.7MB. The GameObjects after the start were 202 and before they were 207. So i dont think that the problem is here but maybe i'm wrong.
    I will post an image when i reach the round 4 because something appen in the memory graphic!
     
  8. NickCanz

    NickCanz

    Joined:
    Nov 3, 2020
    Posts:
    16
    Here is the photo
     

    Attached Files:

    • Lag.png
      Lag.png
      File size:
      239 KB
      Views:
      755
  9. NickCanz

    NickCanz

    Joined:
    Nov 3, 2020
    Posts:
    16
    You can see a the parameters after and before the elimination of all the enemies
     
  10. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,495
    Look closely at CPU Usage tab of profiler and what's taking so long each frame when it's lagging. You just click in the graph while it's playing once it starts getting slow and scrub back and forth between frames that have no lag, comparing with frames that do. Something should stand out. Beyond that, the yellow line dropping like that is GC - which is definitely a cause of stutter/hitching on low-end platforms like mobile, but that should only be momentary vs. the sustained decrease you're seeing. The red spikes are GC allocations - so to get rid of those what you want to do is set up the objects being instantiated during those frames for pooling asap (under the assumption that's where the allocation is coming from).
     
  11. NickCanz

    NickCanz

    Joined:
    Nov 3, 2020
    Posts:
    16
    I analized the CPU Usage and i saw that the Physic and the Instantiate part of the game dropped the fps from 60 to 30. So i think that the problem is this part of the game. You spoke about something for optimize the instantiate part of the game, so can you show me a tutorial or something like that? Do you know also how to optimize the Physics part?

    There are two photos with the CPU Usage with and without the Physics part
     

    Attached Files:

  12. NickCanz

    NickCanz

    Joined:
    Nov 3, 2020
    Posts:
    16
    Also how i can disable the VSync on the mobile graphic?
     
  13. NickCanz

    NickCanz

    Joined:
    Nov 3, 2020
    Posts:
    16
    I used this code:
    Code (CSharp):
    1. if(Application.isMobilePlatform)
    2.     QualitySettings.vSyncCount = 0;
    I saw it on Internet. Do you think is good?
     
  14. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,495
    There are a number of threads with solutions for object pooling - I have a system of my own set up that's optimized for my specific project (i.e. not generic, not minimal), so you're better off looking around for something here because you can tailor that to yours / take an approach that you feel would be best. As for physics, you can tinker with the settings in Project Settings->Physics and maybe increase the value of your fixed timestep in Time but really if there's a point in your game where Instantiate and physics are going crazy for a sustained period, there's a good chance implementing a pooling system could resolve the underlying issue ..otherwise you might spend a lot of time tracking a specific bug down that would disappear in the course of refactoring your project to use a pooling system to manage instantiation.

    It might also be helpful to mention that you could create a stat panel somewhere on your UI (or elsewhere) displaying how many are in each pool, how many are currently in use etc. Personally I like doing stats on UI because it doesn't matter if it's in the editor or a build or which platform, you can always see it. Just watching stats like that can even reveal the source of the problem, like if it's flickering between numbers rapidly or scrolling up like crazy or far more are in use than expected or whatever else.
     
  15. NickCanz

    NickCanz

    Joined:
    Nov 3, 2020
    Posts:
    16
    Yeah i was thinking about a mini profiler panel on my game too. For now i set Fixed TimeStep to 0.065 and the CPU Usage show that the fps of the game are between 200 and 50 and this is good i think. I saw that the graph show another parameter that is 'Other' and in the Unity documentation that it contains the Editor and PlayerLoop. I will test the game with this settings and i hope that the bug will expired but i think that i need to remake that part of Instantiate and Destroy. If you have other solutions I will be happy to try them, but for now you give me a lot of tips and I really want to say thanks to you!

    I also set all the lights to Baked. Do you think that it is a good idea?
     
    Last edited: Dec 5, 2020
    adamgolden likes this.
  16. NickCanz

    NickCanz

    Joined:
    Nov 3, 2020
    Posts:
    16
    I tested the game and the lag appeared. I deactivated many GameObjects before the second test such as the GameController (which has a lot of scripts inside), Canvas and also the Player! The result? THE BUG HAS APPEARED AGAIN !!! I don't really know what to do!:(:(:(
     
  17. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,495
    I don't have much experience with lighting yet - if it looks okay and works I'm happy. Probably always start with Baked and just change to Mixed if it's not working properly, I'm pretty sure you have to use Mixed or Realtime for lights that move (or have parents that move).

    As to your post above, I would suggest tutorials for the Profiler and Frame Debugger, for your version of the editor if possible. Any time there's lag, the cause is discoverable (as far as I know), but this isn't the only time you will have an issue like this - so even if someone can help you right now, next time you won't be able to deduce and solve the next problem yourself and it will be just as frustrating. The best advice I can give you is to invest some time into learning these tools. You can see exactly which function is taking a long time, and from there you will know what to look more closely at for issues and do your best to optimize.
     
  18. NickCanz

    NickCanz

    Joined:
    Nov 3, 2020
    Posts:
    16
    Thanks a lot but the problem is very complicated in fact in another test the only object that is active is the camera and my in-game stats (Xiaomi phones have a panel that can show the FPS) show that after some time the FPS go down to 1.

    The problem is deeper and more complicated than expected. I also tried to move all the objects in another scene and the result is the same.
     
  19. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,058
    You’ve shown a screenshot of the profiler as timeline. Could you create a development build with a deep profiler? Then show it as a detailed list instead of a timeline.
    Make a screenshot of the start when it is not lagging. Then when it starts lagging one and one at its most laggy part.

    wondering if it is really the garbage collection or some other problem.
    Could you also check if generational garbage collection is on? Not sure out of mind if this was an option in 2019.4 as I’m typing from my phone
     
  20. NickCanz

    NickCanz

    Joined:
    Nov 3, 2020
    Posts:
    16
    I'm new in Unity. I don't know what is a development build:D
     
  21. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,058
    Ctrl + Shift + B to open up the build settings, with Android selected you see at the right quite some options.
    Development Build toggle is one of them. It allows a build to be debugged. Also a profiler being connected to it.
    Which there should be an Autoconnect Profiler and Deep Profiling toggle.
    Don't forget to turn on the Deep Profile in the profiler window itself either.

    That way you can use the profiler with your phone and look what is really troublesome.
    Depending on the network, it should auto connect when you run the game on your phone. If it doesn't you can connect it manually by providing the IP of your phone.

    Once you have the profiler hooked take a screenshot of CPU profiling in Hierarchy mode instead of Timeline
     
    Last edited: Dec 5, 2020
  22. NickCanz

    NickCanz

    Joined:
    Nov 3, 2020
    Posts:
    16
    I need to connect my Android Device to the computer?
     
  23. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,058
    Yeah, else you can’t build to your phone.
    Unless you download the apk from somewhere else. Which is a lot more hassle than just connecting it with a usb and pressing build and run
     
  24. NickCanz

    NickCanz

    Joined:
    Nov 3, 2020
    Posts:
    16
    So at the end i found the solution. I create a new project and then i re-create all the things in my previous project. Now i have 60 FPS at high graphic and without any problem. So i was right in fact the problem isn't the Profiler or the Optimization but i think that the problem was in the Settings of Unity. Thank you for your help, i learned a lot of things about Unity, the Profiler and the Optimization. Thank you again!