包含此页的版本:
不含此页的版本:
我们将探索的最后一件事是通过性能测试扩展 Unity 测试框架的包。
性能测试包可用于衡量我们游戏的性能。如果我们想跟踪项目中随时间推移发生的各种回归/进展,这是一个很好的工具。在此示例中,您将学习如何创建测量游戏平均帧数的测试。
<project>/Packages目录。包管理器使用它来配置许多内容,包括该项目的依赖项列表,以及要查询包的任何包存储库。更多信息Unity.PerformanceTesting在 PlayModeTests 程序集定义中访问性能测试 API。您现在已准备好完成您的目标。在PerformanceTests.cs创建一个名为MainScene_MeasureAverageFrames().在此功能中,将角色移动到魔杖位置,并等待魔杖拾取效果结束。在这段时间里,测量帧。
Time.deltaTime来自 UnityEngine API 和Measure.Custom从性能测试包 API 中。Measure.Frames().Scope()API 将它们测量到单独的范围内。PerformanceTests.cs
using System.Collections;
using Unity.PerformanceTesting;
using UnityEngine;
using UnityEngine.TestTools;
using UnityEngine.SceneManagement;
public class PerformanceTests
{
private Transform _characterTransform;
private float _wandLocation = 21.080f;
[UnityTest, Performance]
public IEnumerator MainScene_MeasureAverageFrames()
{
SceneManager.LoadScene("Assets/Scenes/Main.unity", LoadSceneMode.Single);
using (Measure.Frames().Scope("Frames.MainSceneOnLoad.Unstable"))
{
for (var i = 0; i < 25; i++)
{
yield return null;
}
}
using (Measure.Frames().Scope("Frames.MainSceneGameplay"))
{
yield return GoRight();
while (GetCurrentCharacterPosition() <= _wandLocation)
{
yield return null;
}
StopMoving();
yield return new WaitForSeconds(15);
}
}
private float GetCurrentCharacterPosition()
{
// Get Main character's Transform which is used to manipulate position.
if (_characterTransform == null)
{
_characterTransform = GameObject.Find("Sara Variant").transform;
}
return _characterTransform.position.x;
}
private IEnumerator GoRight()
{
TestInputControl.MoveLeft = false;
yield return null;
TestInputControl.MoveRight = true;
}
private void StopMoving()
{
TestInputControl.MoveRight = false;
TestInputControl.MoveLeft = false;
}
}
奖金解决方案
Measure.Custom("FPS", (int)(1f / Time.deltaTime));