• Unity 中FPS的计算公式


    代码如下,只是用到OnGUI和帧运算:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class FPSShow : MonoBehaviour
    {
    	/// <summary>
    	/// 刷新的时间间隔
    	/// </summary>
    	public float timerInterval = .5f;
    	/// <summary>
    	/// 是否显示
    	/// </summary>
    	public bool isShow = true;
    
    	private float leftTimer = 0;
    
    	/// <summary>
    	/// 在区间内绘制帧
    	/// </summary>
    	private	int frames;
    	/// <summary>
    	/// FPS在间隔时间内累积
    	/// </summary>
    	private float accum = 0;
    	/// <summary>
    	/// 文本数据缓存
    	/// </summary>
    	private string textData = null;
    
    	private GUIStyle guiStyle = new GUIStyle ();
    
    	/// <summary>
    	/// 区域
    	/// </summary>
    	private Rect posSize;
    	/// <summary>
    	/// 是否提示
    	/// </summary>
    	private bool already_Tip = false;
    	/// <summary>
    	/// 数量
    	/// </summary>
    	int low_cout = 0;
    
    	void Start ()
    	{  
    //		#if UNITY_EDITOR||UNITY_STANDALONE_WIN
    //		#else
    //		Destroy(this);
    //		return;
    //		#endif
    		DontDestroyOnLoad (gameObject);
    		leftTimer = timerInterval;
    		//文本默认颜色
    		guiStyle.normal.textColor = Color.red;
    		//文字大小
    		guiStyle.fontSize = 12;
    		//显示区域
    		posSize = new Rect (0, 0, 70, 50);
    	}
    
    	void Update ()
    	{
    		++frames;
    
    		leftTimer -= Time.deltaTime;
    		accum += Time.timeScale / Time.deltaTime;
    
    		//间隔结束   那么就更新GUI文本 并开始新的间隔
    		if (leftTimer <= 0.0) {
    			//显示两个小数(f2格式)
    			float fps = accum / frames;
    
    			//计算粒子系统
    			int particleNum = CountParticleNum ();
    			textData = System.String.Format ("FPS:{0:F2}, 粒子数:{1:d}", fps, particleNum);
    
    
    			//只显示FPS
    			//textData = System.String.Format ("FPS:{0:F2}", fps);
    
    			if (fps < 20) {
    				guiStyle.normal.textColor = Color.red;
    			} else if (fps < 25) {
    				guiStyle.normal.textColor = Color.yellow;
    			} else {
    				guiStyle.normal.textColor = Color.green;
    			}
    			leftTimer = timerInterval;
    			accum = 0.0f;
    			frames = 0;
    		}
    	}
    
    	/// <summary>
    	/// 在GUI中显示
    	/// </summary>
    	void OnGUI ()
    	{
    		if (isShow) {
    			GUI.Label (posSize, textData, guiStyle);
    		}
    	}
    
    	/// <summary>
    	/// 计算当前场景粒子数量
    	/// </summary>
    	/// <returns>The particle number.</returns>
    	int CountParticleNum ()
    	{
    		int totalNum = 0;
    		ParticleSystem[] ps = GameObject.FindObjectsOfType <ParticleSystem> ();
    		foreach (ParticleSystem item in ps) {
    			if (item.isPlaying) {
    				//同屏粒子数(当前被渲染数量)
    				if (item.GetComponent <Renderer> () != null && item.GetComponent<Renderer> ().isVisible) {
    					totalNum += item.particleCount;
    				}
    			}
    		}
    		return totalNum;
    	}
    }
    

      

  • 相关阅读:
    iOS UI(布局)约束是什么?view1.attr1 = view2.attr2 * multiplier + constant
    编程范式-声明式编程
    平庸的投资人和优秀的投资人差在哪儿
    编程语言的发展趋势及未来方向
    再读:编程语言的发展趋势及未来方向
    编程语言的发展趋势:声明式动态并发
    命令式编程与声明式编程
    声明式(编程)语言是解释型语言
    DSL-领域特定语言(英语:domain-specific language、DSL)
    声明式编程
  • 原文地址:https://www.cnblogs.com/jbw752746541/p/9436385.html
Copyright © 2020-2023  润新知