Here is a quick and easy script to enable viewing a FPS counter on the Oculus Rift and GearVR using Unity UGUI.
Follow these steps:
- RIght Click on your VR Camera (or Center Eye Camera) and create a UI->Canvas.
Set it to World Space.
Set it’s position to X:0 Y:0 and Z:0.5 (to push it out in front of the camera)
Scale it down to 0.001 (X,Y and Z) so it fits in front of the user - Right Click on your new Canvas and create a UI->Text.
- Drag the script blow onto that Text game object
That’s it. Hit run. Press ‘F’ key to toggle the FPS.
Note: It uses the new fancy Unity 5 Time.unscaledDeltaTime property ! (Thanks to Adrian Butt for the tip)
using UnityEngine; using UnityEngine.UI; // Display FPS on a Unity UGUI Text Panel // To use: Drag onto a game object with Text component // Press 'F' key to toggle show/hide public class TextFPSCounter : MonoBehaviour { public Text text; public bool show = false; private const int targetFPS = #if UNITY_ANDROID // GEARVR 60; #else 75; #endif private const float updateInterval = 0.5f; private int framesCount; private float framesTime; void Start() { // no text object set? see if our gameobject has one to use if (text == null) { text = GetComponent<Text>(); } } void Update() { if (Input.GetKeyDown(KeyCode.F)) { show = !show; } // monitoring frame counter and the total time framesCount++; framesTime += Time.unscaledDeltaTime; // measuring interval ended, so calculate FPS and display on Text if (framesTime > updateInterval) { if (text != null) { if (show) { float fps = framesCount/framesTime; text.text = System.String.Format("{0:F2} FPS", fps); text.color = (fps > (targetFPS-5) ? Color.green : (fps > (targetFPS-30) ? Color.yellow : Color.red)); } else { text.text = ""; } } // reset for the next interval to measure framesCount = 0; framesTime = 0; } } }TextFPSCounter.cs Download - FPS Demo Unity Package
Leave a Reply