noradninja

Vita perf tool

Mar 5th, 2023
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.51 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.PSVita;
  6. using UnityEngine.UI;
  7. public class FPS_Counter : MonoBehaviour {
  8.  
  9.     //public float updateInterval = 0.5f; //How often should the number update
  10.     public Text fpsText;
  11.     public int frameRange = 60;
  12.     public float updateInterval = 0.5f;
  13.     public int maxFPS;
  14.     public int minFPS;
  15.     float accum = 0.0f;
  16.     int frames = 0;
  17.     float timeleft;
  18.     int[] fpsBuffer;
  19.     int fpsBufferIndex;
  20.     public static float averageFPS;
  21.     public float msFrame;
  22.     public Text vramText;
  23.     public Text ramText;
  24.  
  25.     // Use this for initialization
  26. void Start(){
  27.                
  28.         timeleft = updateInterval;
  29.     }
  30.  
  31.     // Update is called once per frame
  32.     void Update()
  33.     {
  34.         timeleft -= Time.unscaledDeltaTime;
  35.         accum += Time.timeScale / Time.unscaledDeltaTime;
  36.         ++frames;
  37.      
  38.         // Interval ended - update GUI text and start new interval
  39.         if (timeleft <= 0.0)
  40.         {
  41.             // display two fractional digits (f2 format)
  42.            // averageFPS = (accum / frames);
  43.             msFrame = (1/averageFPS) * 1000; //get ms/frame
  44.             timeleft = updateInterval;
  45.             accum = 0.0f;
  46.             frames = 0;
  47.             //Display the fps/ms and round to n decimals
  48.             fpsText.text = (Mathf.Clamp(Mathf.RoundToInt(averageFPS),0,60).ToString("F0") +
  49.                                         " FPS Average / " + msFrame.ToString("F2") + " ms/frame" +
  50.                                         Environment.NewLine + "Max (Life): " + maxFPS + " / Min (Prev sec): " +
  51.                                         minFPS);
  52.             switch (Application.isEditor)
  53.             {
  54.                 case false:
  55.                 {
  56.                     float VRAMValue = UnityEngine.PSVita.Diagnostics.GetFreeMemoryCDRAM();
  57.                     decimal VRAMFree = Math.Round((decimal)(VRAMValue/1000000), 2);
  58.                     decimal calcVRAM = (((128-VRAMFree)/128)*100);
  59.                     decimal percentVRAM = Math.Round((decimal)calcVRAM ,2);
  60.                
  61.                     float RAMValue = UnityEngine.PSVita.Diagnostics.GetFreeMemoryLPDDR();
  62.                     decimal RAMFree = Math.Round((decimal)(RAMValue/1000000), 2);
  63.                     decimal calcRAM = (((512-RAMFree)/512)*100);
  64.                     decimal percentRAM = Math.Round((decimal)calcRAM ,2);
  65.                    
  66.                     //refresh values on screen
  67.                     vramText.text = ("VRAM: " + VRAMFree + "MB Free");
  68.                     ramText.text = ("RAM: " + RAMFree + "MB Free");
  69.                    
  70.                     //change VRAM/RAM colors
  71.                     if (percentVRAM >= 75){
  72.                         vramText.color = Color.red;
  73.                     }
  74.                     else if(percentVRAM < 75 && percentVRAM > 50){
  75.                         vramText.color = Color.yellow;
  76.                     }
  77.                     else vramText.color = Color.green;
  78.  
  79.                     if (percentRAM >= 75){
  80.                         ramText.color = Color.red;
  81.                     }
  82.                     else if(percentRAM < 75 && percentRAM > 50){
  83.                         ramText.color = Color.yellow;
  84.                     }
  85.                     else ramText.color = Color.green;
  86.                    
  87.                     //change FPS colors
  88.                     if (Mathf.RoundToInt(averageFPS) > 27.00f){
  89.                         fpsText.color = Color.green;
  90.                     }
  91.                     else if (Mathf.RoundToInt(averageFPS) <= 27.00f) {
  92.                         fpsText.color = Color.red;
  93.                     }
  94.                    
  95.                     break;
  96.                 }
  97.                 case true:
  98.                     vramText.text = ("VRAM: Unavailable");
  99.                     ramText.text = ("RAM: Unavailable");
  100.                     break;
  101.             }
  102.         }
  103.        
  104.         if (fpsBuffer == null || fpsBuffer.Length != frameRange) {
  105.             InitializeBuffer();
  106.         }
  107.         UpdateBuffer();
  108.         CalculateFPS();
  109.  
  110.    
  111.     }
  112.     void InitializeBuffer () {
  113.         if (frameRange <= 0) {
  114.             frameRange = 1;
  115.         }
  116.         fpsBuffer = new int[frameRange];
  117.         fpsBufferIndex = 0;
  118.     }
  119.    void UpdateBuffer () {
  120.         fpsBuffer[fpsBufferIndex++] = (int)(1f / Time.unscaledDeltaTime);
  121.         if (fpsBufferIndex >= frameRange) {
  122.             fpsBufferIndex = 0;
  123.         }
  124.     }
  125.     void CalculateFPS () {
  126.         int sum = 0;
  127.         int highest = 0;
  128.         int lowest = int.MaxValue;
  129.         for (int i = 0; i < frameRange; i++) {
  130.             int fps = fpsBuffer[i];
  131.             sum += fps;
  132.             if (fps > highest) {
  133.                 highest = fps;
  134.             }
  135.             if (fps < lowest) {
  136.                 lowest = fps;
  137.             }
  138.             minFPS = lowest;
  139.         }
  140.         averageFPS = sum / frameRange;
  141.         averageFPS += 1; //fix for off by one error cause I am fucking lazy
  142.  
  143.         if (highest > maxFPS)
  144.         {
  145.             maxFPS = highest;  
  146.         }
  147.     }
  148.    
  149. }
Add Comment
Please, Sign In to add comment