playMaker

Author Topic: Show framerate [SOLVED]  (Read 4706 times)

adamgoodrich

  • Playmaker Newbie
  • *
  • Posts: 2
    • View Profile
Show framerate [SOLVED]
« on: March 01, 2013, 01:21:44 AM »
Hi,
I am a playmaker and unity noob. Loving both :)
In order to get instant feedback as I try out new ideas it would be great to be able to show the current framerate.
Can anyone suggest how I can do this?
Thanks,
Adam.
« Last Edit: March 10, 2013, 10:17:38 PM by Alex Chouls »

A3DStudio

  • Playmaker Newbie
  • *
  • Posts: 43
    • View Profile
    • A3DStudio
Re: Show framerate
« Reply #1 on: March 01, 2013, 01:54:59 AM »
You can try the FPS Counter to show the framerate:
http://hutonggames.com/playmakerforum/index.php?topic=1763.0

adamgoodrich

  • Playmaker Newbie
  • *
  • Posts: 2
    • View Profile
Re: Show framerate
« Reply #2 on: March 01, 2013, 01:58:02 AM »
Thanks :)

This worked a treat.

The one I used was:

Code: [Select]
using UnityEngine;
using System.Collections;
 
[AddComponentMenu( "Utilities/HUDFPS")]
public class HUDFPS : MonoBehaviour
{
// Attach this to any object to make a frames/second indicator.
//
// It calculates frames/second over each updateInterval,
// so the display does not keep changing wildly.
//
// It is also fairly accurate at very low FPS counts (<10).
// We do this not by simply counting frames per interval, but
// by accumulating FPS for each frame. This way we end up with
// corstartRect overall FPS even if the interval renders something like
// 5.5 frames.
 
public Rect startRect = new Rect( 10, 10, 75, 50 ); // The rect the window is initially displayed at.
public bool updateColor = true; // Do you want the color to change if the FPS gets low
public bool allowDrag = true; // Do you want to allow the dragging of the FPS window
public  float frequency = 0.5F; // The update frequency of the fps
public int nbDecimal = 1; // How many decimal do you want to display
 
private float accum   = 0f; // FPS accumulated over the interval
private int   frames  = 0; // Frames drawn over the interval
private Color color = Color.white; // The color of the GUI, depending of the FPS ( R < 10, Y < 30, G >= 30 )
private string sFPS = ""; // The fps formatted into a string.
private GUIStyle style; // The style the text will be displayed at, based en defaultSkin.label.
 
void Start()
{
    StartCoroutine( FPS() );
}
 
void Update()
{
    accum += Time.timeScale/ Time.deltaTime;
    ++frames;
}
 
IEnumerator FPS()
{
// Infinite loop executed every "frenquency" secondes.
while( true )
{
// Update the FPS
    float fps = accum/frames;
    sFPS = fps.ToString( "f" + Mathf.Clamp( nbDecimal, 0, 10 ) );
 
//Update the color
color = (fps >= 30) ? Color.green : ((fps > 10) ? Color.red : Color.yellow);
 
        accum = 0.0F;
        frames = 0;
 
yield return new WaitForSeconds( frequency );
}
}
 
void OnGUI()
{
// Copy the default label skin, change the color and the alignement
if( style == null ){
style = new GUIStyle( GUI.skin.label );
style.normal.textColor = Color.white;
style.alignment = TextAnchor.MiddleCenter;
}
 
GUI.color = updateColor ? color : Color.white;
startRect = GUI.Window(0, startRect, DoMyWindow, "");
}
 
void DoMyWindow(int windowID)
{
GUI.Label( new Rect(0, 0, startRect.width, startRect.height), sFPS + " FPS", style );
if( allowDrag ) GUI.DragWindow(new Rect(0, 0, Screen.width, Screen.height));
}
}

I simply attached it to my FPS controller and it added a little window at the top lhs of the screen. Brill!
« Last Edit: March 01, 2013, 02:14:16 AM by adamgoodrich »

kiriri

  • Hero Member
  • *****
  • Posts: 506
    • View Profile
Re: Show framerate
« Reply #3 on: March 01, 2013, 07:13:16 AM »
for everyone who doesn't want to use any external scripts, just do this:

Create a new FSM. Then create a new int variable called "Count" and a new string variable called "CountString".

Create a new state and call it "active" (just for the sake of this explanation). Add a GUI Label action to it and use the CountString variable as text.
Then add an IntAdd action and add 1 every frame to Count.
Then add a Wait action and set it to 1 second wait time. Add a FINISHED event and use that in the wait event.

Now you can create a second state which you could call "reset".

Link up the "active" state to the "reset" state, and vice versa (again with a FINISHED event).
In the reset state add an IntSet action and set Count to 0.
Now add an IntToString action and convert Count to CountString.

If you do none of those things every frame, it will fire the FINISHED event automatically and return to the "active" state for another second. There it will count how many frames there are and after a second it will update the string again in the "reset" state.

It's not a perfect setup, but it's easy to setup :)
Best,
Sven