作为开发人员,我们经常会遇到美术给的模型面数过多等情况。为了避免返工,可以先做一个简单的游戏性能测试用例。
具体功能如下:
1,生成测试盒子;2,得到游戏中的点面数;3,FPS实时监测
检测点面数的代码如下:
using UnityEngine;
using System.Collections;
/// <summary>
/// 计算场景中的顶点与三角面
/// </summary>
public class CalculateVertsAndTris : MonoBehaviour
{
public float f_UpdateInterval = 0.5F; //刷新间隔
private float f_LastInterval; //上一次刷新的时间间隔
public static int verts;
public static int tris;
// Use this for initialization
void Start()
{
f_LastInterval = Time.realtimeSinceStartup;
}
/// <summary>
/// 得到场景中所有的GameObject
/// </summary>
void GetAllObjects()
{
verts = 0;
tris = 0;
GameObject[] ob = FindObjectsOfType(typeof(GameObject)) as GameObject[];
foreach (GameObject obj in ob)
{
GetAllVertsAndTris(obj);
}
}
//得到三角面和顶点数
void GetAllVertsAndTris(GameObject obj)
{
Component[] filters;
filters = obj.GetComponentsInChildren<MeshFilter>();
foreach (MeshFilter f in filters)
{
tris += f.sharedMesh.triangles.Length / 3;
verts += f.sharedMesh.vertexCount;
}
}
void OnGUI()
{
string vertsdisplay = verts.ToString("#,##0 verts");
GUIStyle fontStyle = new GUIStyle();
fontStyle.normal.background = null; //设置背景填充
fontStyle.normal.textColor = GUI.color; //设置字体颜色
fontStyle.fontSize = 80; //字体大小
GUI.Label(new Rect(0, 0, 200, 200), "顶点数:" + vertsdisplay, fontStyle);
//GUILayout.Label(vertsdisplay);
string trisdisplay = tris.ToString("#,##0 tris");
GUIStyle fontStyle2 = new GUIStyle();
fontStyle2.normal.background = null; //设置背景填充
fontStyle2.normal.textColor = GUI.color; //设置字体颜色
fontStyle2.fontSize = 80; //字体大小
GUI.Label(new Rect(0, 200, 200, 200), "三角面数:" + trisdisplay, fontStyle2);
//GUILayout.Label(trisdisplay);
}
// Update is called once per frame
void Update()
{
if (Time.realtimeSinceStartup > f_LastInterval + f_UpdateInterval)
{
f_LastInterval = Time.realtimeSinceStartup;
GetAllObjects();
}
}
}
这里引用了这篇博客的代码与思路。
FPS检测工具:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class FPSUtils : MonoBehaviour
{
public static float f_Fps;
public float f_UpdateInterval = 0.5f; //每个0.5秒刷新一次
private float f_LastInterval; //游戏时间
private int i_Frames = 0;//帧数
void Awake()
{
//Application.targetFrameRate = 30;
Application.targetFrameRate = 60;
//Time.timeScale=0.2f;
}
void OnGUI()
{
if (f_Fps > 50)
{
GUI.color = new Color(0, 1, 0);
}
else if (f_Fps > 40)
{
GUI.color = new Color(1, 1, 0);
}
else
{
GUI.color = new Color(1.0f, 0, 0);
}
GUIStyle fontStyle = new GUIStyle();
fontStyle.normal.background = null; //设置背景填充
fontStyle.normal.textColor = GUI.color; //设置字体颜色
fontStyle.fontSize = 80; //字体大小
GUI.Label(new Rect(0, 600, 200, 200), "FPS:" + f_Fps.ToString("f2"),fontStyle);
}
void Update()
{
++i_Frames;
if (Time.realtimeSinceStartup > f_LastInterval + f_UpdateInterval)
{
f_Fps = i_Frames / (Time.realtimeSinceStartup - f_LastInterval);
i_Frames = 0;
f_LastInterval = Time.realtimeSinceStartup;
}
}
}
简单的AI逻辑与生成为自己写的,测试结果如下:
项目工程已上传到下载链接里面去了。
FPS50左右的真机测试结果:
低端机:29万顶点数,15万三角面
高端机:100万顶点数,50万三角面
补充:
如果用球进行测试的话,算出来的结果不太一样。我们以顶点数为准,这个最好取下中间值。
我们游戏用的是37万顶点,极限值在80万。