Wednesday, October 30, 2013

UNITY3D fps counter script.

/*
    Copyright 2013, Aleksander Hristov, boxcollider@gmail.com .

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using UnityEngine;
using System.Collections;

//Class responsible for showing current game fps.
//Attach this script to an empty game object in scene.

public class FPSCounter : MonoBehaviour
{
 GUIText fpsText;
 string fpsTitle = "FPS : ";
 Color textColor = Color.red;

 void Start ()
 {
  initGUIText ();
  InvokeRepeating ("trackTime", 1, 1);
 }

 void initGUIText ()
 {
  transform.position = new Vector3 (0.5f, 0.1f, 0);
  fpsText = gameObject.AddComponent<GUIText> ();
  fpsText.material.color = textColor;
  fpsText.fontSize = (int)(Screen.height * 0.07f);
  fpsText.anchor = TextAnchor.MiddleCenter;
  fpsText.alignment = TextAlignment.Center; 
 }
 
 int lastFrameCount = 0;

 void trackTime ()
 {  
  fpsText.text = fpsTitle + (Time.frameCount - lastFrameCount).ToString ();
  lastFrameCount = Time.frameCount;
 }
}

No comments:

Post a Comment