unity3D技术之Performance Optimization 性能优化_第1页
unity3D技术之Performance Optimization 性能优化_第2页
unity3D技术之Performance Optimization 性能优化_第3页
unity3D技术之Performance Optimization 性能优化_第4页
unity3D技术之Performance Optimization 性能优化_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Use Static Typing使用静态类型When using JavaScript the most important optimization is to use static typing instead of dynamic typing. Unity uses a technique called type inference to automatically convert JavaScript constructs to statically typed code without you having to do any work.使用JavaScript很重要的优化是使用

2、静态类型替代动态类型。Unity使用一种叫做类型推理 的技术来自动转换JavaScript为静态类型而无需你做任何工作。【狗刨学习网】C#JavaScriptvar foo = 5;In the above example foo will automatically be inferred to be an integer value. Thus Unity can apply a lot of compile time optimizations, without costly dynamic name variable lookups etc. This is one of the re

3、asons why Unitys JavaScript is on average around 20 times faster than other JavaScript implementations.上面例子中的foo将自动推断为一个整数值。因此Unity能节约大量时间,而不使用动态名 称变量查找等耗时的计算。这就是为什么Unity的执行平均速度比其他JavaScript快20倍 的原因之一。The only problem is that sometimes not everything can be type inferred, thus Unity will fall back t

4、o dynamic typing for those variables. By falling back to dynamic typing, writing JavaScript code is simpler. However it also makes the code run slower.唯一的问题是,有时不是所有的东西都能被类型推断,因此Unity会对这些变量重新使用动 态类型.通过这样,书写JavaScript代码会很简单。不过他也使代码执行变慢。Lets see some examples.我们来看一些例子.C#JavaScriptfunction Start () (var

5、 foo = GetComponent(MyScript);foo.DoSomething();Here foo will be dynamically typed, thus calling the function DoSomething takes longer than necessary - because the type of foo is unknown, it has to figure out whether it supports DoSomething function, and if it does, invoke that function.这里foo将是动态类型,

6、因此调用函数DoSomething必须要更长时间-因为foo的类型未 知,它必须弄明白是否支持DoSomething函数,如果支持,就调用那个函数。C#JavaScriptfunction Start () (var foo : MyScript = GetComponent(MyScript); foo.DoSomething();Here were forcing foo to be of specific type. You will get much better performance.这里我们指定foo为静态类型.你将获得更好的性能.Use #pragma strict 使用 #p

7、ragma strictNow the problem is of course, that you dont usually notice when you are using dynamic typing. #pragma strict to the rescue! Simply add #pragma strict at the top of a script and Unity will disable dynamic typing in that script, forcing you to use static typing. Wherever a type is not know

8、n, Unity will report compile errors. So in this case, foo will produce an error when compiling:现在问题是。你通常意识不到你使用了动态类型。#pragma strict可解决!只需在脚本开始 处简单的添加#pragma strict. Unity将会禁用脚本的动态类型,强制你使用静态类型。当一个 类型未知,Unity就会报告编译错误。所以,如下代码,foo编译时将会产生一个错误:C#JavaScript#pragma strictfunction Start ()(var foo = GetCompo

9、nent(MyScript);foo.DoSomething();Cache component lookups缓存组件查找Another optimization is caching of components. This optimization unfortunately requires a bit of coding effort and is not always worth it. But if your script is really used a lot and you need to get the last bit of performance out of it,

10、this can be a very good optimization.另一个优化是组件缓存.不过它需要一些代码,而且不一定都有必要。但是如果你的代码真 是很大,且你需要一定量的性能提升,那么它可以提供很好的优化。Whenever you access a component through GetComponent or an accessor variable, Unity has to find the right component from the game object. This time can easily be saved by caching a reference t

11、o the component in a private variable.当你通过GetComponent访问一个组件或一个变量,Unity必须从游戏对象里找到正确的组 件。这时你可以将参数缓存致一个私有的组件。Simply turn this:只要把这个:C# JavaScriptfunction Update () ( transform.Translate(0, 0, 5);Into this:变成这样C#JavaScriptprivate var myTransform : Transform ;function Awake () (myTransform = transform;f

12、unction Update () (myTransform.Translate(0, 0, 5);The latter code will run a lot faster since Unity doesnt have to find the transform component in the game object each frame. The same applies for scripted components, where you use GetComponent instead of the transform or other shorthand property.后者运

13、行得快一些,因为Unity不再在每帧查找transform组件。它同样适用于脚本组件, 在你用GetComponent替换transform或其他快捷属性。Use Builtin arrays 使用 Builtin arraysBuiltin arrays are FAST, very fast, so use them. While the ArrayList or Array classes are easier to use since you can easily add elements they dont have nearly the same speed. Builtin ar

14、rays have a fixed size but most of the time you know the maximum size in advance and can just fill it out later. The best thing about builtin arrays is that they directly embed struct data types in one tightly packed buffer, without any extra type information or other overhead. Thus iterating throug

15、h is very easy on the cache as everything is aligned.Builtin arrays运行速度很快。虽然ArrayList或Array类很容易使用,你能轻易添加组件,但 是他们有完全不同的速度。Builtin arrays有一个固定的长度,而且多数情况下你事先知道最 大长度,且能扩展它builtin arrays最大好处是它直接嵌入struct数据类型在一个缓冲区里, 不需要额外类型信息或其他。因此更易于做缓存遍历。C#JavaScriptprivate var positions : Vector3 ;function Awake () (po

16、sitions = new Vector3 100;for (var i=0;i100)return;/perform real work work.This is not a good idea since Unity has to invoke the update function and you are performing work every frame. A better solution is to disabling the behaviour until the player comes closer. There are 3 ways to do this: 1. Use

17、 OnBecameVisible and OnBecameInvisible. These call backs are tied into the rendering system. As soon as any camera can see the object, OnBecameVisible will be called, when no camera sees it anymore OnBecameInvisible will be called. This is useful in some cases, but often for AI it is not useful beca

18、use enemies would become disabled as soon as you turn the camera away from them.这并不是很好的方法,因为Unity不得不访问update函数,且你得在每一帧执行它。更好 的方法是禁用该行为直到玩家靠近。有3种方法实现它:1.使用OnBecameVisible和OnBecameInvisible。这些回调函数绑定在渲染系统,一旦摄像 机能看到对象,OnBecameVisible将被调用,当没有摄像机看他OnBecameInvisible被调用。 这有时很有用,但是对AI来讲就不能用了,因为你将摄像机移开敌人后,敌人就失效了,(敌 人的AI就失效了)C#JavaScriptfunction OnBecameVisible () ( enabled = true;function OnBecameInvisible () (enabled = false;Use triggers. A simple sphere trigger

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论