Log in or register to post comments

Display framerate in app

May 20, 2011 - 12:32am #1

Hello,

Is there an easy way to display the framerate in the app?

-Thomas

Display framerate in app

August 27, 2015 - 7:37am #10

Hi, I have to create new classes to input this methods?

Display framerate in app

June 18, 2014 - 10:43am #9

TrackerBehaviour.cs seems to be non-available on later versions of Vuforia, is there a correct way to access to TrackerBehaviour.OnPreRender to deal with viewport correction?

 

We're having similar issue, but with GUITexture, when display video-on-texture with the vuforia video. Any idea would be welcomed.

Re: Display framerate in app

May 23, 2011 - 8:30am #8

This is OT but learning about the viewport being modified helped me fix a problem I was having. :)

I switch to a different camera script when not tracking anything but everything was getting stretched. Disabling the changing of the viewport when nothing is being tracked fixed me right up.

For those that are interested the changing of the viewport happens in TrackerBehaviour.OnPreRender

Re: Display framerate in app

May 20, 2011 - 11:23am #7

You may very well be right, I retract my statement :) Still, watch the viewport when using GUIText / GUITextures!

- Kim

Re: Display framerate in app

May 20, 2011 - 11:08am #6
Quote:

I believe OnGUI is still the preferred method for creating GUIs, however.

I'm not a Unity guru, but the advice I've read on the Unity site is that OnGUI should be avoided on Android, and mobile devices generally, due to the overhead. GUITextures are the recommended approach, and there are also some 3rd party sprite classes that people are using.

Re: Display framerate in app

May 20, 2011 - 10:48am #5

FYI, GUIText should work, with one caveat. QCAR sets the viewport to something slightly larger than the screen, to accommodate the resolution of the camera image (the augmentation needs to be rendered at the same resolution). You can find the viewport values at runtime in TrackerBehaviour.cs. Be careful not to place GUIText in the very corners of the screen, as it will most likely end up off screen.

I believe OnGUI is still the preferred method for creating GUIs, however.

- Kim

Re: Display framerate in app

May 20, 2011 - 3:31am #4
//attach this to any gameobject in the scene to get readings from the HUDFPS

using UnityEngine;
using System.Collections;
using System;

public class ArGui : MonoBehaviour
{
	private string fpsString;
	private GUIStyle fpsStyle;

	public void Start ()
	{
		fpsStyle = new GUIStyle ();
		fpsStyle.normal.textColor = Color.blue;
		HUDFPS hs = FindObjectOfType (typeof(HUDFPS)) as HUDFPS;
		hs.OnFpsChange += fpsChange;//add the event listener here....
	}


	public void fpsChange (string s)
	{
		//event sent from HUDFPS
		fpsString = s;
	}

	private void drawFps ()
	{
		GUI.Label (new Rect (5f, 5f, 100f, 100f), fpsString, fpsStyle);
	}


	public void OnGUI ()
	{
		drawFps ();
	}
	
}

//for some reason I made these into two separate behaviours. both are needed. Below is the calculate code.

using UnityEngine;
using System.Collections;

public class HUDFPS : MonoBehaviour
{

// Attach this to anything in the scene to collect/calculate the fps
	// subscribe to the OnFpsChange event to do something with it...
	//modified from the unitywiki 
	
	public float updateInterval = 0.5f;

	private float accum = 0;
	private int frames = 0;
	private float timeleft;
	
	//event
	public delegate void fpsChangedHandler (string timestring);
	public event fpsChangedHandler OnFpsChange;

	void Start ()
	{
		timeleft = updateInterval;
	}

	void Update ()
	{
		timeleft -= Time.deltaTime;
		accum += Time.timeScale / Time.deltaTime;
		++frames;
		
		// Interval ended - update GUI text and start new interval
		if (timeleft <= 0.0) {
			// display two fractional digits (f2 format)
			float fps = accum / frames;
			string format = System.String.Format ("{0:F2} FPS", fps);
			if (OnFpsChange != null) {
				OnFpsChange (format);
			}
			//guiText.text = format;
			
			timeleft = updateInterval;
			accum = 0.0f;
			frames = 0;
		}
	}
}

Like it says in the code, the calc is done in one behaviour and the display (OnGUI) is located in another one. you could just merge them into one if you like...

Re: Display framerate in app

May 20, 2011 - 3:11am #3

Thanks for your reply. It's good to know i'm not alone out there ;)

If you could post the code that would be great, since i'm not really that much of a programming guru

-Thomas

Re: Display framerate in app

May 20, 2011 - 1:07am #2

Hi!
Funny you ask, because I was working on that yesterday.
There is a generic solution at http://www.unifycommunity.com/wiki/index.php?title=FramesPerSecond
Turns out this is not working for the AR cam solution, it uses a GUItext and it magically disappears when publishing to the device. [I guess this is a bug report, using a GUITexture doesn´t seem to work with the ARCamera, workaround is to use OnGUI instead]

The way I solved it was to use the "measuring logic" from the above script and send the fps measurement to a OnGUI call instead.

I could post the code if you are interested

Log in or register to post comments