Search Unity

Frameloop control

Discussion in 'Editor & General Support' started by shaun, Nov 12, 2007.

  1. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    Searched the manuals but haven't found a way to lock the framerate at runtime.
    I want my app to be running in the background and it doesn't need to sync with the vertical refresh (usually 60fps for LCD's) - I want to limit to say, 25-30 FPS.

    How would I do this - or is it even possible?

    thx
    Shaun
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    There's this. Someone also had another way of doing it that doesn't involve busy-waiting (so it doesn't take up 100% CPU time), but I'm not sure where that is.

    --Eric
     
  3. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    I think the other solution was calling Thread.Sleep (or whatever is the function name in .NET, don't remember) to maintain more or less constant frame rate.
     
  4. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    Thanks guys - the key is using as little CPU as possible. Whenever I run this Unity app on my MacBookPro, my fans start spinning like crazy, it heats up and I generally feel uncomfortable to leave running it in the background (its drawing about 40% cpu avg, gpu - no idea)

    Which brings up another point - what is actually paused when "RunInBackground' is set to false - do networking or other time critical commands still get processed?

    Aras - where would one put the Thread.Sleep() statement? And wouldn't it mess with the behavioral processing? How about making this part of UnityEngine? ;)

    Shaun.
     
  5. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    I can tell you that networking doesn't work when paused.

    If you run a server that is paused, the game updates stop for everyone.

    Because of that, I would assume it all halts.

    -Jeremy
     
  6. Brian-Kehrer

    Brian-Kehrer

    Joined:
    Nov 7, 2006
    Posts:
    411
    Just needed to solve the same issue--funny how that works :roll:

    This will maintain constant framerate, and reduce cpu load. As for side effects, I don't know yet.

    I just wrote this based on the busy waiting solution, but switched to sleep based on Aras's idea

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Threading;
    5.  
    6.  
    7.  
    8.  
    9. public class FpsClamp : MonoBehaviour {
    10.     float oldTime = 0.0F;
    11.     float theDeltaTime= 0.0F;
    12.     float curTime= 0.0F;
    13.     float timeTaken = 0.0F;
    14.     int frameRate = 30;
    15.    
    16.     // Use this for initialization
    17.     void Start () {
    18.     theDeltaTime = (1.0F /frameRate);
    19.     oldTime = Time.realtimeSinceStartup;
    20.     }
    21.    
    22.    
    23.     // Update is called once per frame
    24.     void LateUpdate () {
    25.     curTime = Time.realtimeSinceStartup;
    26.     timeTaken = (curTime - oldTime);
    27.     if(timeTaken < theDeltaTime){
    28.         Thread.Sleep((int)(1000*(theDeltaTime - timeTaken)));
    29.     }
    30.    
    31.    
    32.     oldTime = Time.realtimeSinceStartup;
    33.     }
    34. }
    35.  
     
  7. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    Nice one - thanks Brian.

    What would be good is to update the code so that the framerate is reduced to 1FPS when in the background - I guess that could be done with a combination RunInBackground and OnApplicationPause. :)