This page gives some general hints for improving script performance on iOS.

此页面提供了一些一般的技巧,提高了在iOS上的脚本性能。

Reduce Fixed Delta Time 减少固定的增量时间

Use a fixed delta time value between 0.04 and 0.067 seconds (ie, 15-25 frames per second). You can change this in Edit->Project Settings->Time. This reduces the frequency with which FixedUpdate is called and how often the physics engine has to perform collision detection and rigidbody updates. If you are using a rigidbody for the main character, you can enable interpolation in the Rigidbody Component to smooth out low fixed delta time steps.

使用fixedDeltaTime值在0.04~0.067秒之间(例如,每秒15~25帧之间)。可以在Edit->Project Settings->Time修改这个。这降低了FixedUpdate被调用和物理引擎执行碰撞检测和刚体更新的频率。如果为主角色使用刚体,你可以在刚体组件启用插值来平滑降低固定增量时间步。

Reduce GetComponent Calls 减少GetComponent调用

Using GetComponent or built-in component accessors can have a noticeable overhead. You can avoid this by getting a reference to the component once and assigning it to a variable (sometimes referred to as "caching" the reference). For example, if you are using something like:-

使用GetComponent或内置的组件访问器有明显的性能开销。可以通过获取一次组件的引用并指定给变量来避免这个(有时也被称为"缓存"的引用)。例如,如果你使用像这样:

function Update () {
transform.Translate(0, 1, 0);
}

...you would get better performance by changing it to:-

......通过改变它会得到更好的性能: -

var myTransform : Transform;

function Awake () {
myTransform = transform;
} function Update () {
myTransform.Translate(0, 1, 0);
}

Avoid Allocating Memory 避免分配内存

You should avoid allocating new objects unless you really need to, since they increase the garbage collection overhead when they are no longer in use. You can often reuse arrays and other objects rather than allocate new ones and doing so will help to minimise garbage collection. Also, you should use structs instead of classes where possible. Struct variables are allocated from the stack like simple types rather than from the heap like object types. Since stack allocation is faster and involves no garbage collection, structs will often improve performance if they are fairly small in size. While large structs will still avoid allocation/collection overhead, they will incur a separate overhead due to "pass-by-value" copying and may actually be less efficient than the equivalent object classes.

你应该避免分配新的对象,除非你真的需要,当他们不再使用时,因为它们增加了垃圾收集的开销。你经常可以重复使用数组和其他物体,而不是分配新的,这样做将有助于减少垃圾收集。此外,在可能的情况下你应该使用结构而取代类。从堆栈(stack)中分配结构变量像简单类型而不是从堆(heap)中,像对象类型。由于堆栈分配速度非常快,不涉及垃圾收集,如果它们的大小相当小,结构往往会提高性能。同时大的结构仍然将避免分配/收集的开销,由于pass-by-value的拷贝将产生一个单独的开销,实际上可能效率低于同等的对象类。

Minimise the GUI 尽量减少GUI

The GUILayout functions are very convenient for automatic spacing of GUI elements. However, this automation naturally comes with a processing overhead. You can avoid some of this overhead by handling the layout manually using the GUI functions. Additionally, you can set a script's useGUILayoutvariable to false in order to disable the layout phase completely:-

GUILayout功能对于GUI元素的自动间距都非常方便。然而这种自动化有一些处理性能开销。通过使用GUI功能手动处理布局可以避免这种开销。此外,您可以设置脚本的useGUILayout变量为false,以完全禁用此布局。

function Awake () {
useGUILayout = false;
}

Use iOS Script Call Optimization 使用iOS脚本调用优化

Most of the functions in the UnityEngine namespace are implemented in C/C++. Calling a C/C++ function from a Mono script involves a performance overhead. You can use iOS Script Call optimization (menu: Edit->Project Settings->Player) to save about 1 to 4 milliseconds per frame. The options for this setting are:-

大多数在UnityEngine命名空间的功能是在C/C++实现。从Mono脚本调用C/C++功能涉及到性能开销。可以使用iOS脚本调用优化(菜单: Edit->Project Settings->Player),节省大约每帧1至4毫秒。

  • Slow and Safe - the default Mono internal call handling with exception support. 
    慢而安全 - 默认的Mono内部调用支持异常处理。
  • Fast and Exceptions Unsupported - a faster implementation of Mono internal call handling. However, this doesn't support exceptions and so should be used with caution. An app that doesn't explicitly handle exceptions (and doesn't need to deal with them gracefully) is an ideal candidate for this option. 
    快而不提供异常 - Mono内部调用处理快速执行。然而,并不提供异常,所以应慎用。应用程序并不需要明确的处理异常,用此选项。

Optimizing Garbage Collection 优化垃圾收集

As mentioned above, it is best to avoid allocations as far as possible. However, given that they can't be completely eliminated, there are two main strategies you can use to minimise their intrusion into gameplay:-

如上所述,最好是尽可能避免分配。然而,由于不能完全消除,主要有两种方法可以使用,以减少它们侵入到游戏: -

Small heap with fast and frequent garbage collection
小堆快而频繁的垃圾收集

This strategy is often best for games that have long periods of gameplay where a smooth framerate is the main concern. A game like this will typically allocate small blocks frequently but these blocks will be in use only briefly. The typical heap size when using this strategy on iOS is about 200KB and garbage collection will take about 5ms on an iPhone 3G. If the heap increases to 1MB, the collection will take about 7ms. It can therefore be advantageous sometimes to request a garbage collection at a regular frame interval. This will generally make collections happen more often than strictly necessary but they will be processed quickly and with minimal effect on gameplay:-

这种策略对于长时间的游戏是最好的,有平滑的帧率是主要的考量。像这样的游戏通常会频繁地分配小块,但这些块将只是简单使用。当在iOS上使用这一策略时,典型的堆大小是大约200KB,在iPhone 3G垃圾收集大于需要5ms,如果堆增加到1MB,收集大约需要7ms。因此这是很有利的,有时垃圾收集在一个规则的帧间隔。这通常会使收集发生很多时候绝对必要的,但他们将迅速处理并对游戏影响很小: -

if (Time.frameCount % 30 == 0)
{
System.GC.Collect();
}

However, you should use this technique with caution and check the profiler statistics to make sure that it is really reducing collection time for your game.

但是,应该谨慎使用这项技术,并检查分析器统计以确保它真的为游戏减少了垃圾收集时间。

Large heap with slow but infrequent garbage collection
大堆慢,但不经常垃圾收集

This strategy works best for games where allocations (and therefore collections) are relatively infrequent and can be handled during pauses in gameplay. It is useful for the heap to be as large as possible without being so large as to get your app killed by the OS due to low system memory. However, the Mono runtime avoids expanding the heap automatically if at all possible. You can expand the heap manually by preallocating some placeholder space during startup (ie, you instantiate a "useless" object that is allocated purely for its effect on the memory manager):-

这一策略最适合于分配是相对偶发并可在游戏暂停期间处理的游戏。对于堆需要尽可能的大而由于系统内存不足没有那么大,操作系统将杀死应用,这是很有用的。然而,如果可能,在Mono运行时避免自动扩大堆。在启动期间通过预留占位空间可以手动扩展堆(例如,实例化一个无用的对象来分配,纯粹为其在内存管理器的效果)。

function Start() {
var tmp = new System.Object[1024]; // make allocations in smaller blocks to avoid them to be treated in a special way, which is designed for large blocks
for (var i : int = 0; i < 1024; i++)
tmp[i] = new byte[1024]; // release reference
tmp = null;
}

A sufficiently large heap should not get completely filled up between those pauses in gameplay that would accommodate a collection. When such a pause occurs, you can request a collection explicitly:-

一个足够大的堆,在游戏容纳一个收集,暂停之前不应该完全被填满。当这样暂停发生时,应该明确要求收集:

System.GC.Collect();

Again, you should take care when using this strategy and pay attention to the profiler statistics rather than just assuming it is having the desired effect.

再次,使用此策略,应该注意分析器统计情况,而不是仅仅假设它是有预期的效果。

页面最后更新: 2011-10-11

分类:Manual| 翻译: U_鹰

优化脚本性能 Optimizing Script Performance的更多相关文章

  1. Unity3D Optimizing Graphics Performance for iOS

    原地址:http://blog.sina.com.cn/s/blog_72b936d801013ptr.html icense Comparisons http://unity3d.com/unity ...

  2. Redis 优化查询性能

    一次使用 Redis 优化查询性能的实践   应用背景 有一个应用需要上传一组ID到服务器来查询这些ID所对应的数据,数据库中存储的数据量是7千万,每次上传的ID数量一般都是几百至上千数量级别. 以前 ...

  3. 一次使用 Redis 优化查询性能的实践

    因为我的个人网站 restran.net 已经启用,博客园的内容已经不再更新.请访问我的个人网站获取这篇文章的最新内容,一次使用 Redis 优化查询性能的实践 应用背景 有一个应用需要上传一组ID到 ...

  4. 页面性能监控之performance

    页面性能监测之performance author: @TiffanysBear 最近,需要对业务上的一些性能做一些优化,比如降低首屏时间.减少核心按钮可操作时间等的一些操作:在这之前,需要建立的就是 ...

  5. REORG TABLE命令优化数据库性能

    [转]DB2日常维护——REORG TABLE命令优化数据库性能     一个完整的日常维护规范可以帮助 DBA 理顺每天需要的操作,以便更好的监控和维护数据库,保证数据库的正常.安全.高效运行,防止 ...

  6. DB2日常维护——REORG TABLE命令优化数据库性能

    一个完整的日常维护规范可以帮助 DBA 理顺每天需要的操作,以便更好的监控和维护数据库,保证数据库的正常.安全.高效运行,防止一些错误重复发生. 由于DB2使用CBO作为数据库的优化器,数据库对象的状 ...

  7. DB2日常维护——REORG TABLE命令优化数据库性能(转)

    [转]DB2日常维护——REORG TABLE命令优化数据库性能 一个完整的日常维护规范可以帮助 DBA 理顺每天需要的操作,以便更好的监控和维护数据库,保证数据库的正常.安全.高效运行,防止一些错误 ...

  8. [转]优化Flash性能

    原文:http://www.adobe.com/devnet/flash/articles/optimizing-flash-performance.html 翻译:http://bbs.9ria.c ...

  9. 使用 Chrome Timeline 来优化页面性能

    使用 Chrome Timeline 来优化页面性能 有时候,我们就是会不由自主地写出一些低效的代码,严重影响页面运行的效率.或者我们接手的项目中,前人写出来的代码千奇百怪,比如为了一个 Canvas ...

随机推荐

  1. 五、react中父子组件间如何传值

    1.父组件向子组件传递数据:父组件绑定属性值传给子组件,子组件通过this.props()接受. 2.子组件向父组件传递数据:子组件绑定一个方法,方法中通过this.props.父组件方法名(参数)传 ...

  2. 2017.12.20 Java中的 IO/XML学习总结 File类详细

    IO / XML 一.File类 1.定义/概念 Java是面向对象的语言,要想把数据存到文件中,就必须要有一个对象表示这个文件.File类的作用就是代表一个特定的文件或目录,并提供了若干方法对这些文 ...

  3. angular2+ form 表单中 input输入框的disabled属性设置无效

    最近项目中遇到一个表单input设置disabled问题,直接赋值angular原生的[disabled]=“isDisabled”无效,浏览器警告信息: 无奈,只能按照控制台提示修改: 问题解决

  4. java基础编程——重建二叉树

    题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7, ...

  5. C/C++程序基础 (二)常用知识点

    使用宏实现max 注意括号在宏内的使用 #define MAX(x, y) ( ( (x) > (y) ) ? (x) : (y) ) 宏参数连接 a##e##b 转化为字符串 #a const ...

  6. dynamic routing between captual

    对于人脑 决策树形式 对于CNN 层级与层级间的传递 人在识别物体的时候会进行坐标框架的设置 CNN无法识别,只能通过大量训练 胶囊 :一个神经元集合,有一个活动的向量,来表示物体的各类信息,向量的长 ...

  7. k8s的pv和pvc简述

    pvc:资源需要指定:1.accessMode:访问模型:对象列表:    ReadWriteOnce – the volume can be mounted as read-write by a s ...

  8. php同一个用户同时只能登陆一个, 后登陆者踢掉前登陆者(排他登陆)

    通常用户登陆,如果没有特别的限定, 同一个用户可以同时登陆, 今天搞了一个东西限定一个用户不能同时登陆到一个系统上, 后登陆者会把前面登陆的踢出来.(有点像QQ,同个帐号不能在多个地方同时在线, 后面 ...

  9. Python 正则表达式 贪心匹配和非贪心匹配

    Python的正则表达式默认是“贪心匹配”,即在有第二义的情况下,尽可能匹配最长的字符串,在正则表达式的花括号后面跟上问号,可以变为非贪心模式 >>> >>> ha ...

  10. 列举Asp.net页面之间传递值的几种方式和优缺点?

    一.QueryString变量 优点:使用简单,对于安全性要求不高时传递数字或是文本值非常有效. 缺点:缺乏安全性,由于它的值暴露在浏览器的URL地址中的:不能传递对象. 二. 使用Applicati ...