Facebook
From Samet, 1 Month ago, written in C#.
Embed
Download Paste or View Raw
Hits: 135
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using UnityEngine;
  5.  
  6. public class FrameRateManager : MonoBehaviour
  7. {
  8.     [Header("Frame Settings")]
  9.     int maxFrameRate = 9999;
  10.  
  11.     public float TargetFrameRate = 60.0f;
  12.  
  13.     float currentFramTime;
  14.  
  15.     private void Awake()
  16.     {
  17.         QualitySettings.vSyncCount = 0;
  18.         Application.targetFrameRate = maxFrameRate;
  19.         currentFramTime = Time.realtimeSinceStartup;
  20.  
  21.        
  22.     }
  23.  
  24.     IEnumerator WaitForNextTime()
  25.     {
  26.         while (true)
  27.         {
  28.             yield return new WaitForEndOfFrame();
  29.             currentFramTime += 1.0f / TargetFrameRate;
  30.             var t = Time.realtimeSinceStartup;
  31.             var sleepTime = currentFramTime - t - 0.01f;
  32.             if (sleepTime > 0)
  33.             {
  34.                 Thread.Sleep((int)(sleepTime * 1000));
  35.             }
  36.             while (t < currentFramTime)
  37.             {
  38.                 t = Time.realtimeSinceStartup;
  39.             }
  40.  
  41.  
  42.         }
  43.     }
  44.  
  45. }
  46.