怎么在unity中测试游戏_Unity中的性能基准测试:如何开始

本文介绍了如何使用Unity Test Runner和Unity Performance Testing Extension在Unity中进行性能基准测试。通过示例项目和详细步骤,展示了如何收集性能指标、创建基准、比较性能测试结果,并跟踪性能变化。这对于确保游戏在不同平台上的流畅运行和优化SDK、驱动程序等至关重要。
摘要由CSDN通过智能技术生成

怎么在unity中测试游戏

As a Unity developer, you want your users to love playing your games, enjoying a smooth experience across all the platforms they may play on. What if I told you that we just made it easier to create performance benchmarks? If you want to learn how to develop games or Unity tools with an eye on performance, please read on!

作为Unity开发人员,您希望用户喜欢玩游戏,在他们可能玩过的所有平台上享受流畅的体验。 如果我告诉您我们使创建性能基准变得更加容易怎么办? 如果您想学习如何着眼于性能来开发游戏或Unity工具,请继续阅读!

In this post, I explain how to use a couple of Unity tools that give you an easy way to start collecting performance metrics and creating benchmarks with them: the Unity Test Runner that ships with the Unity Editor, the Unity Performance Testing Extension, and the Unity Performance Benchmark Reporter.

在本文中,我将说明如何使用几个Unity工具,这些工具为您提供了一种简单的方法来开始收集性能指标并使用它们创建基准:Unity编辑器附带的Unity Test RunnerUnity Performance Testing ExtensionUnity Performance Benchmark Reporter

为什么要在Unity中对性能进行基准测试? (Why Benchmark Performance in Unity?)

As a Unity Developer, you might find yourself in the  following situation: your project was running fast and smooth not too long ago, but then one or more changes have come in, and now scenes are noticeably slow, frames are dropping, and other performance issues have started popping up. Tracking which changes led to the performance regression can be difficult.

作为Unity开发人员,您可能会遇到以下情况:项目不久前运行很快且流畅,但是随后进行了一个或多个更改,现在场景明显变慢,帧掉落以及其他性能问题开始出现。 跟踪导致性能下降的更改可能很困难。

If you’re a Unity Partner, you want to understand the performance changes across your SDKs, drivers, platforms, packages, or other artefacts. Or you’d like to collect performance metrics across different versions of Unity with your products, but it’s not very clear how to do this and then make the comparisons.

如果您是Unity合作伙伴,则希望了解SDK,驱动程序,平台,软件包或其他工件之间的性能变化。 或者,您想收集产品在不同版本的Unity中的性能指标,但是还不清楚如何执行然后进行比较。

Those are just a couple of examples where establishing performance benchmarks can really save the day. Now, let me show you how you can start collecting performance metrics, create benchmarks with them, and visualize changes in performance metrics.

这些只是建立性能基准可以真正节省时间的几个例子。 现在,让我向您展示如何开始收集性能指标,使用它们创建基准并可视化性能指标的变化。

下载样例项目 (Download the Sample Project)

For this discussion, we’ll be looking at the test code in the UnityPerformanceBenchmark sample performance test project.

对于此讨论,我们将在UnityPerformanceBenchmark示例性能测试项目中查看测试代码。

Download the latest XRAutomatedTests release from GitHub. You’ll find the UnityPerformanceBenchmark project in the PerformanceTests subdirectory.

从GitHub下载最新版本XRAutomatedTests 。 您可以在PerformanceTests子目录中找到UnityPerformanceBenchmark项目。

在Unity Test Runner中编写性能测试 (Writing Performance Tests in Unity Test Runner)

The UnityPerformanceBenchmark project contains a variety of sample scenes that are in turn used in Unity Performance Tests using the Unity Performance Testing Extension.

UnityPerformanceBenchmark项目包含各种示例场景,这些示例场景又在使用Unity Performance Testing Extension的Unity Performance Tests中使用。

The first thing we’re going to do is take a look at how we write performances test using the Unity Test Runner with the Unity Performance Testing Extension. Here is a bit of background info on both of these tools before we proceed.

我们要做的第一件事是看看如何使用带有Unity Performance Testing ExtensionUnity Test Runner编写性能测试。 在继续之前,下面是有关这两种工具的一些背景信息。

Unity测试运行器 (Unity Test Runner)

We’re using the Unity Test Runner to run our performance tests. The Unity Test Runner is a test execution framework built into the Unity Editor allowing you to test your code in both Edit and Play mode on target platform players such as Standalone, Android, or iOS. If you aren’t familiar with the Unity Test Runner, check out the Unity Test Runner documentation.

我们正在使用Unity Test Runner运行性能测试。 Unity Test Runner是Unity编辑器中内置的测试执行框架,可让您在目标平台播放器(例如Standalone,Android或iOS)上以Edit和Play模式测试代码。 如果您不熟悉Unity Test Runner,请查看Unity Test Runner文档

Unity性能测试扩展 (Unity Performance Testing Extension)

The Unity Performance Testing Extension is a Unity Editor package that provides an API and test case attributes allowing you to sample and aggregate both Unity profiler markers and   non-profiler custom metrics, in the Unity Editor and players. You can learn more by checking out the Unity Performance Testing Extension documentation, but we’re going to look at some examples here.

Unity Performance Testing Extension是一个Unity Editor 软件包 ,它提供API和测试用例属性,使您可以在Unity Editor和播放器中采样和聚合Unity Profiler标记和非Profiler自定义指标。 您可以通过查看Unity Performance Testing Extension文档来了解更多信息 ,但是我们将在此处查看一些示例。

The Unity Performance Test Extension requires Unity 2018.1 or higher. Be sure to use Unity version 2018.1 or higher if you want to run the sample performance tests in the UnityPerformanceBenchmark project or whenever you are using the Unity Performance Test Extension.

Unity Performance Test Extension需要Unity 2018.1或更高版本。 如果要在UnityPerformanceBenchmark项目中运行示例性能测试,或者在每次使用Unity Performance Test Extension时,请确保使用Unity版本2018.1或更高版本。

使用命令行打开示例项目 (Open the Sample Project Using the Command Line)

The UnityPerformanceBenchmark project implements the IPrebuildSetup interface, a Unity Test Runner facility, where we can implement a Setup method that is automatically called before the test run is executed by the Unity Test Runner.

UnityPerformanceBenchmark项目实现IPrebuildSetup接口,这是Unity Test Runner工具,我们可以在其中实现Setup方法,该方法在Unity Test Runner执行测试运行之前会自动调用。

The first thing the UnityPerformanceBenchmark project’s IPrebuildSetup.Setup method does is  parse the command line arguments looking for player build settings. This allows us to flexibly build the player for our performance tests using the same Unity project against different platforms, render threading modes, player graphics APIs, scripting implementations, and XR-enabled settings such as stereo rendering path and VR SDKs.

UnityPerformanceBenchmark项目的IPrebuildSetup.Setup方法要做的第一件事是解析命令行参数,以查找播放器生成设置。 这使我们能够针对不同的平台,渲染线程模式,播放器图形API,脚本实现以及启用XR的设置(例如立体声渲染路径和VR SDK)针对同一平台使用相同的Unity项目灵活地为性能测试构建播放器。

Thus, we’ll need to open the UnityPerformanceBenchmark project with Unity from the command line, passing in the player build options we want to use when we run the tests in the Unity Test Runner.

因此,我们需要从命令行通过Unity打开UnityPerformanceBenchmark项目,并传入我们在Unity Test Runner中运行测试时要使用的播放器构建选项。

Example: Launch UnityPerformanceBenchmark project from Windows to Build Android Player:

示例:从Windows启动UnityPerformanceBenchmark项目以构建Android Player:

1

2
3
Unity.exe -projectPath
C:\XRAutomatedTests-2018.2\PerformanceTests\UnityPerformanceBenchmark
-testPlatform Android -buildTarget Android -playergraphicsapi=OpenGLES3 -mtRendering -scriptingbackend=mono

1

2
3
Unity . exe - projectPath
C : \ XRAutomatedTests - 2018.2 \ PerformanceTests \ UnityPerformanceBenchmark
- testPlatform Android - buildTarget Android - playergraphicsapi = OpenGLES3 - mtRendering - scriptingbackend = mono

Here we launch Unity on Windows to build for Android with OpenGLES3 graphics API, multithreaded rendering, and mono scripting backend.

在这里,我们在Windows上启动Unity,以使用OpenGLES3图形API,多线程渲染和单色脚本后端为Android构建。

Example: Launch UnityPerformanceBenchmark project from OSX to Build iOS Player

示例:从OSX启动UnityPerformanceBenchmark项目以构建iOS Player

1

2
3
4
./Unity -projectPath /XRAutomatedTests-2018.2/PerformanceTests/UnityPerformanceBenchmark
-testPlatform iOS -buildTarget iOS -playergraphicsapi=OpenGLES3 -mtRendering -scriptingbackend=mono
-appleDeveloperTeamID=<yourAppleDeveloperTeamID>
-iOSProvisioningProfileID=<yourIosProvisionProfileID>

1

2
3
4
. / Unity - projectPath / XRAutomatedTests - 2018.2 / PerformanceTests / UnityPerformanceBenchmark
- testPlatform iOS - buildTarget iOS - playergraphicsapi = OpenGLES3 - mtRendering - scriptingbackend = mono
- appleDeveloperTeamID = < yourAppleDeveloperTeamID >
- iOSProvisioningProfileID = < yourIosProvisionProfileID >

Here we launch Unity on OSX to build for iOS with OpenGLES3 graphics API, multithreaded rendering, and mono scripting backend. We also provide the Apple developer team and provisioning profile information needed to deploy to an iOS device.

在这里,我们在OSX上启动Unity,以使用OpenGLES3图形API,多线程渲染和单色脚本后端为iOS构建。 我们还提供了Apple开发人员团队和调配配置文件信息,以部署到iOS设备。

When we open the UnityPerformanceBenchmark project with Unity from the command line like in the examples above, the command line args will be in memory for the IPrebuildSetup.Setup method to parse and use to build the player with.

当像上面的示例一样从命令行使用Unity打开UnityPerformanceBenchmark项目时,命令行args将在内存中供IPrebuildSetup.Setup方法解析并用于构建播放器。

While this launch-from-command-line approach isn’t required to run tests in the Unity Test Runner, it’s a good pattern to use to avoid using a separate test project for each player configuration.

虽然在Unity Test Runner中运行测试不需要使用这种从命令行启动的方法,但是这是避免在每个播放器配置中使用单独的测试项目的一种好模式。

I’ve detailed the command line options for opening the project, or just running the tests, from the command line on the wiki for the test project: How to Run the Unity Performance Benchmark Tests. To learn more about how we’re parsing the player build settings in the test project, take a look at the RenderPerformancePrebuildStep.cs file in the Scripts directory of the UnityPerformanceBenchmark test project.

我已经从测试项目Wiki上的命令行详细介绍了用于打开项目或仅运行测试的命令行选项: 如何运行Unity Performance Benchmark Tests 。 要了解有关我们如何在测试项目中解析播放器构建设置的更多信息,请查看UnityPerformanceBenchmark测试项目的Scripts目录中的RenderPerformancePrebuildStep.cs文件。

打开测试运行器窗口 (Open the Test Runner Window)

After we open the UnityPerformanceBenchmark, we need to open the Unity Test Runner window in the Unity Editor

打开UnityPerformanceBenchmark之后,我们需要在Unity编辑器中打开Unity Test Runner窗口

  • in Unity 2018.1, go to Window > Test Runner.

    在Unity 2018.1中,转到窗口>测试运行器

  • in Unity 2018.2, go to Window > General > Test Runner.

    在Unity 2018.2中,转到窗口>常规>测试运行器

The Unity Test Runner window will open and look like the image below.

Unity Test Runner窗口将打开,如下图所示。

Unity Test Runner with Tests.

Unity Test Runner与测试。

These are our Unity Performance tests. We can run them in the Editor using the Run button at the top left of the window, or on the actual device or platform using the “Run all in player” button at the top right of the window.

这些是我们的Unity Performance测试。 我们可以使用窗口左上方的“运行”按钮在编辑器中运行它们,也可以使用窗口右上方的“在播放器中运行所有”按钮在实际设备或平台上运行它们。

Debugging Tip

调试技巧

If you want to debug code in your IPrebuildSetup.Setup method

如果要调试IPrebuildSetup.Setup方法中的代码

  1. Set breakpoints in your IPrebuildSetup.Setup code in Visual Studio

    在Visual Studio中的IPrebuildSetup.Setup代码中设置断点

  2. Attach to the Unity Editor with the Visual Studio Tool for Unity extension

    使用Visual Studio Tool for Unity扩展附加到Unity编辑器

  3. Run your tests in the Editor using the “Run All” or “Run Select” button in the Unity Test Runner window.

    使用Unity Test Runner窗口中的“全部运行”或“运行选择”按钮在编辑器中运行测试。

At this point the Visual Studio debugger will break into your code where you can debug as needed.

此时,Visual Studio调试器将分解为您的代码,您可以在其中根据需要进行调试。

Unity性能测试示例 (Example Unity Performance Test)

Let’s take a look at a performance test example so we can get a better understanding of how it works.

让我们看一下性能测试示例,以便我们可以更好地了解它的工作方式。

Example: Sampling Profiler Markers in a Unity Performance Test

示例:在Unity性能测试中采样探查器标记

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值