Skip to main content
1 of 5
raisa_
  • 167
  • 1
  • 2
  • 10

Showing Frame Rate Info in the Unity scene using Custom Script failed

I want to lock the framerate of my scene to 30fps, then I learnt from this post that fps info in Editor Stat isn't reliable, that's why my fps is still around 100. This is my script for setting the framerate (vSync also has been set into Don't Sync) :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TargetFramerate : MonoBehaviour {
    public int frame_rate = 30;

    void Start () {
    }

    void Awake (){
        QualitySettings.vSyncCount = 0;
        Application.targetFrameRate = frame_rate;
    }

    void Update () {
        if (frame_rate != Application.targetFrameRate) {
            Application.targetFrameRate = frame_rate;
        }
    }
}

Following Wiki Unity 3D - FramePerSecond, I want to show FPS in my scene using custom script, I'm using the first code from tutorial :

 using UnityEngine;
 using System.Collections;
  
 public class FPSDisplay : MonoBehaviour
 {
     float deltaTime = 0.0f;
  
     void Update()
     {
         deltaTime += (Time.unscaledDeltaTime - deltaTime) * 0.1f;
     }
  
     void OnGUI()
     {
         int w = Screen.width, h = Screen.height;
  
         GUIStyle style = new GUIStyle();
  
         Rect rect = new Rect(0, 0, w, h * 2 / 100);
         style.alignment = TextAnchor.UpperLeft;
         style.fontSize = h * 2 / 100;
         style.normal.textColor = new Color (0.0f, 0.0f, 0.5f, 1.0f);
         float msec = deltaTime * 1000.0f;
         float fps = 1.0f / deltaTime;
         string text = string.Format("{0:0.0} ms ({1:0.} fps)", msec, fps);
         GUI.Label(rect, text, style);
     }
    }

But it won't show. Where should I put this script ? The description says in any GameObject will be ok, so I put in one of my GameObject with camera attached on it. But it's not showing. By the way, I'm also using SteamVR camera script for my GameObjects (for HMD device), is it affecting this script ?

raisa_
  • 167
  • 1
  • 2
  • 10