Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

How to reduce CPU usage when not doing much?

Discussion in 'Editor & General Support' started by JoeStrout, Jul 24, 2015.

  1. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,026
    I'm using Unity to make a home automation app. It consists entirely of UI elements, that spend their time waiting for the user to tap something, or waiting on a response from a web request:



    So, in principle, this app isn't doing much of anything most of the time. But it still causes my fan to come on, and if I were to throw it onto an iPad (as I intend), I'm sure it would drain the battery at an alarming rate.

    Anybody have any tips on reducing the CPU usage on an app like this? I see that I can set targetFrameRate (though apparently that's ignored in the editor). Any other obvious things to try?
     
  2. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    Of course you had to do it in LCARS style. I approve :)
    (sorry for spamming your thread)
     
    JoeStrout likes this.
  3. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,103
    Why not ask the profiler for help?
    He can tell you what's eating your cpu time.
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,026
    @fffMalzbier, that's always a good first step, but it doesn't address what I was really after... to be honest, when I started typing the thread I hadn't yet found targetFrameRate, and until you put that in, all the profiler shows is that it's spending an awful lot of time doing all the things that happens on each frame (BehaviourUpdate, Camera.Render, Overhead, etc.). It doesn't tell you how to make it spend less time doing all those things, i.e., simply render fewer frames. I was wondering if there were other things like that: global settings that make a HUGE difference, rather than small changes to the code that simply make each frame go faster.
     
  5. jpease

    jpease

    Joined:
    Apr 18, 2013
    Posts:
    11
    You could try either setting vsync to every other frame in the quality settings, or running code like this in Awake to reduce the framerate Unity's targeting:
    Code (csharp):
    1. QualitySettings.vSyncCount = 0;
    2. Application.targetFrameRate = 15;
    Once Unity is able to easily reach the framerate it's trying to reach, it should start using less CPU power. To that end, optimizing your frametime can help to some extent too.

    If you can't get anything else to work to your satisfaction and you want to try something really hacky, you could forcibly relinquish CPU time by calling System.Threading.Thread.Sleep(1000) in some script's Update function. (1000 is just an example; reduce it if you want the app to update more than once per second.) I have no idea if this works on device (although probably every platform has some native function available that can do this) or what kinds of weird side effects it may cause as a result of Unity not expecting such a function to be used in the main thread.
     
    JoeStrout likes this.
  6. pmurph03

    pmurph03

    Joined:
    Mar 17, 2014
    Posts:
    47
    Reduce the usage of behaviour update by using an event system instead of putting essentially anything into an update function except on a single inputhandler script.

    The profiler should show which scripts are consuming the most time in ms, you can also use deep profile to further identify problem areas.

    Coroutine's could also help due only running when you tell them to, and the use of waitforseconds.

    The best way is to get rid of any/all update/fixed update functions of any objects that don't need them.
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,254
    That's pretty much the worst-case scenario for a Unity app. Unfortunately it's not really the right tool for this sort of job. You can slow the framerate way down, but that makes for a pretty terrible user experience since input won't really work right and everything lags. There's still actually quite a bit going on every frame even if you remove all Update functions, since Unity has to continuously cull and upload objects to the GPU. Unity is entirely focused on the "highly interactive and stuff changes every frame" game loop model, and I don't think there's any way to hack it to simulate the "do nothing except wait for input 99% of the time" event model.

    --Eric
     
  8. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,026
    I recognize that, but it's still worth it for me, because by this point I am so much more productive in Unity than in any other dev tool.

    And, for this particular app at least, turning down the framerate to 15 or 30 fps seems to do the trick just fine. Its CPU usage now is comparable to other apps I have running all the time, and the UI still feels quite responsive.
     
  9. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    6,238
    take a screenshot or set camera to 'dont clear' at keep displaying just single image, probably needs one script to monitor for screen click (and then handle click on that position) - no idea if that would work..:)
     
  10. jpease

    jpease

    Joined:
    Apr 18, 2013
    Posts:
    11
    Setting the camera to "don't clear" doesn't reliably work to keep a previous image onscreen, but taking a screenshot with Camera.Render and drawing it as a single quad does in fact work well to optimize things that need to be visible but aren't moving/changing (you can even draw dynamic things on top of the screenshot).
     
  11. bitinn

    bitinn

    Joined:
    Aug 20, 2016
    Posts:
    683
    Joe-Censored and JoeStrout like this.
unityunity