Unity3D优化之Tuning Main Loop Performance

来源:互联网 发布:php门户网站 编辑:程序博客网 时间:2024/06/06 05:11

Tuning Main Loop Performance         调整主循环性能

 

Setting the Desired Framerate

设置所需的帧率

Unity iOS allows you to change the frequency with which your application will try to execute its rendering loop, which is set to 30 frames per second by default. You can lower this number to save battery power but of course this saving will come at the expense of frame updates. Conversely, you can increase the framerate to give the rendering priority over other activities such as touch input and accelerometer processing. You will need to experiment with your choice of framerate to determine how it affects gameplay in your case.

Unity iOS 允许您更改您的应用程序执行渲染循环的频率,而它的默认设置为每秒 30帧。您可以降低此帧率来节省电池的电量,但当然这种节约在每帧中也会给您来一定的更新代价。相反,您也可以增加渲染帧率,从而使画面渲染优先于触摸输入和加速度计处理等其他活动。您需要对帧速率进行反复试验,从而确定它是如何来影响您的游戏体验。

 

If your application involves heavy computation or rendering and can maintain only 15 frames per second, say, then setting the desired frame rate higher than fifteen wouldn't give any extra performance. The application has to be optimized sufficiently to allow for a higher framerate.

如果您的应用程序涉及到大量计算和复杂的渲染,并且只能达到15/秒,那么即便您将所需帧率设置为高于15帧,那也于事无补。您只有将应用程序进行足够的优化,才能允许获得更高的帧率。

 

To set the desired framerate, open the XCode project generated by Unity and open theAppController.mm file. The line

为了设置需要帧率,打开Unity生成的XCode工程,并打开AppController.mm文件。下面一行

 

#define kFPS 30

 

...determines the the current framerate, so you can just change to set the desired value. For example, if you change the define to:-

决定了当前的帧率,所以你可以通过更改该值来得到所需的帧率。比如,如果你改变这个#define为:

#define kFPS 60

 

...then the application will attempt to render at 60 FPS instead of 30 FPS.

那么应用程序将试图将渲染帧率变为60FPS,而不是30FPS

 

The Rendering Loop   渲染循环

When iOS version 3.1 or later is in use, Unity will use theCADisplayLink class to schedule the rendering loop. Versions before 3.1 need to use one of several fallback methods to handle the loop. However, the fallback methods can be activated even for iOS 3.1 and later by changing the line

在使用 iOS 3.1 或更高版本时,Unity将安排渲染循环使用 CADisplayLink类。版本 3.1之前的版本需要使用几种fallback方法来处理循环。但是, 3.1及更高版本的 iOS可以通过以下方法来激活fallback方法。

 

 

#define USE_DISPLAY_LINK_IF_AVAILABLE 1

...and changing it to

改变它为:

#define USE_DISPLAY_LINK_IF_AVAILABLE 0

Fallback Loop Types          回退循环类型

Apple recommends the system timer for scheduling the rendering operation on iOS versions before 3.1. This approach is good for applications where performance is not critical and favours battery life and correct processing of events over rendering performance. However, better rendering performance is often more important to games, so Unity provides several scheduling methods to tweak the performance of the rendering loop:-

苹果公司建议在iOS 版本 3.1之前使用系统计时器来调度的渲染。这种做法适合于那些对性能需求不是十分苛刻的应用,同时,这样做也有利于电池的寿命和正确的渲染处理。但是,对于游戏来说,我们无疑是需要更好地渲染性能,所以Unity提供几个调度方法来调整渲染循环的性能:

 

          System Timer: this is the standard approach suggested by Apple. It uses theNSTimer class to schedule rendering and has the worst rendering performance but

          guarantees to process all input events.

         系统计时器:这是苹果所建议的标准方法。它使用 NSTimer类来安排渲染,但为了保证处理所有的输入的事件大大牺牲了渲染性能。

       

         Thread: a separate thread is used to schedule rendering. This offers better rendering performance than the NSTimer approach, but sometimes could miss touch or

          accelerometer events. This method of scheduling is also the easiest to set up and is the default method used by Unity for iOS versions before 3.1.

         线程:使用一个单独的线程用来处理渲染。这比NSTimer方法提供了更好的渲染性能,但有时可能会漏掉触摸或加速度计的响应事件。这种调度方法也是最简单的设置方

         法,并且是Unity iOS 3.1版本之前使用的默认方法。

        

        Event Pump: this uses aCFRunLoop object to dispatch events. It gives better rendering performance than the NSTimer approach and also allows you to set the

        amount of time the OS should spend processing touch and accelerometer events. This option must be used with care since touch and accelerometer events will be

        lost if there is not enough processor time available to handle them.

        事件泵:它使用 CFRunLoop对象来调度事件。它比 NSTimer方法提供了更好地渲染性能,并还允许您设置操作系统应该花多少时间处理触摸和加速度计事件。但您必

        须小心使用此选项,因为如果没有分配足够的时间来处理触摸器和加速度计事件,那么它们可能会被遗漏或丢失。

      

 

The different fallback loop types can be selected by changing defines in the AppController.mm file. The significant lines are the following:

可以通过更改AppController.mm文件的定义来选择不同的fallback循环类型。重要的代码如下所示:

 

#define FALLBACK_LOOP_TYPE NSTIMER_BASED_LOOP#define FALLBACK_LOOP_TYPE THREAD_BASED_LOOP#define FALLBACK_LOOP_TYPE EVENT_PUMP_BASED_LOOP


The file should have all but one of these lines commented out. The uncommented line selects the rendering loop method that will be used by the application.

在文件中会注释掉以上三行代码中的两行。未被注视的代码显示了在应用程序中所用的渲染循环方法。

 

If you want to prioritize rendering over input processing with the NSTimer approach you should locate and change the line

如果您想在使用 NSTimer 方法的时候提升渲染处理的优先级,使其先于输入操作进行处理,那么您应该在AppController.mm文件中找到并更改以下代码:

 

#define kThrottleFPS 2.0

...in AppController.mm. Increasing this number will give higher priority to rendering. The result of changing this value varies among applications, so it is best to try it for yourself and see what happens in your specific case.

增加这个数值会提高渲染的优先级。改变此值的结果会影响您的整个应用程序,所以您最好亲自多试验几遍,来看看它对您应用的的影响。

 

If you use the Event Pump rendering loop then you need to tweak thekMillisecondsPerFrameToProcessEvents constant precisely to achieve the desired responsiveness. ThekMillisecondsPerFrameToProcessEvents constant allows you to specify exactly how much time (in milliseconds) you will allow the OS to process events. If you allocate insufficient time for this task then touch or accelerometer events might be lost, and while the application will be fast, it will also be less responsive.

如果您使用事件泵渲染循环,您需要调整kMillisecondsPerFrameToProcessEvents常数来达到想要的响应。KMillisecondsPerFrameToProcessEvents常数允许您指定操纵系统可以用多少时间(以毫秒为单位)来处理事件。如果此任务分配的时间不足,那么触摸消息或加速度计事件可能会丢失,并且应用程序将会变快,同时也会丢失一些响应。

 

To specify the amount of time (in milliseconds) that the OS will spend processing events, locate and change the line

为了设置操作系统处理消息的时间总量(以毫秒为单位),您需要在AppController.mm文件中找到并修改以下代码:

#define kMillisecondsPerFrameToProcessEvents 7.0


...in AppController.mm.

 

Tuning Accelerometer Processing Frequency             调整加速度计处理频率

 

If accelerometer input is processed too frequently then the overall performance of your game may suffer as a result. By default, a Unity iOS application will sample the accelerometer 60 times per second. You may see some performance benefit by reducing the accelerometer sampling frequency and it can even be set to zero for games that don't use accelerometer input. You can change the accelerometer frequency from theOther Settings panel in the iOS Player Settings.

如果过于频繁地处理加速度计,那么你的游戏的整体性能可能会受到一定的损失。默认情况下,一个Unity iOS应用每秒将处理60次加速度事件。您会看到,如果降低加速度计的调用频率,将会提升一些性能。所以对于不使用加速度计的游戏,您甚至可以将它设置为零。您可以在iOS Player Settings中的Other Settings面板中改变加速度计的更新频率。

 

备注:最近一直在研究Unity3D的性能优化问题,这段时间可能会多翻译这方面的文章,如有翻译不当之处还请多多指出。

原文地址:http://unity3d.com/support/documentation/Manual/iphone-Optimizing-MainLoop.html

	
				
		
原创粉丝点击