查看: 2258|回复: 0
打印 上一主题 下一主题

[脚本章节]Overview: Performance Optimization 性能优化

[复制链接]

3795

主题

2

听众

5万

积分

版主

Rank: 7Rank: 7Rank: 7

纳金币
53202
精华
32

活跃会员 优秀版主 推广达人 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2012-10-11 10:37:16 |只看该作者 |倒序浏览
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 cons***cts to statically typed code without you having to do any work.
使用JavaScript很重要的优化是使用静态类型替代动态类型。Unity使用一种叫做类型推理的技术来自动转换JavaScript为静态类型而无需你做任何工作。


    C#
    JavaScript


using UnityEngine;using System.Collections;public class example : MonoBehaviour {public int foo = 5;}


var 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 reasons why Unity's 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 to dynamic typing for those variables. By falling back to dynamic typing, writing JavaScript code is simpler. However it also makes the code***n slower.
唯一的问题是,有时不是所有的东西都能被类型推断,因此Unity会对这些变量重新使用动态类型.通过这样,书写JavaScript代码会很简单。不过他也使代码执行变慢。
Let's see some examples.
我们来看一些例子.


    C#
    JavaScript


using UnityEngine;using System.Collections;public class example : MonoBehaviour {void Start() {duck foo = GetComponent<MyScript>();foo.DoSomething();}}


function Start () {var 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将是动态类型,因此调用函数DoSomething必须要更长时间 – 因为foo的类型未知,它必须弄明白是否支持DoSomething函数,如果支持,就调用那个函数。


    C#
    JavaScript


using UnityEngine;using System.Collections;public class example : MonoBehaviour {void Start() {MyScript foo = GetComponent<MyScript>();foo.DoSomething();}}


function Start () {var foo : MyScript = GetComponent(MyScript);foo.DoSomething();}


Here we're forcing foo to be of specific type. You will get much better performance.
这里我们指定foo为静态类型.你将获得更好的性能.
2. Use #pragma strict

使用#pragma strict
Now the problem is of course, that you don't 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 known, 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


using UnityEngine;using System.Collections;public class example : MonoBehaviour {void Start() {MyScript foo = GetComponent<MyScript>() as MyScript;foo.DoSomething();}}


#pragma strictfunction Start (){var foo = GetComponent(MyScript);foo.DoSomething();}


3. 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, 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 to the component in a private variable.
当你通过GetComponent访问一个组件或一个变量,Unity必须从游戏对象里找到正确的组件。这时你可以将参数缓存致一个私有的组件。
Simply turn this:  只要把这个:


    C#
    JavaScript


using UnityEngine;using System.Collections;public class example : MonoBehaviour {void Update() {transform.Translate(0, 0, 5);}}


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


Into this: 变成这样


    C#
    JavaScript


using UnityEngine;using System.Collections;public class example : MonoBehaviour {private Transform myTransform;void Awake() {myTransform = transform;}void Update() {myTransform.Translate(0, 0, 5);}}


private var myTransform : Transform ;function Awake () {myTransform = transform;}function Update () {myTransform.Translate(0, 0, 5);}


The latter code will***n a lot faster since Unity doesn't 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.
后者运行得快一些,因为Unity不再在每帧查找transform组件。它同样适用于脚本组件,在你用GetComponent替换transform或其他快捷属性。
4. Use Builtin arrays

使用Builtin arrays
Builtin 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 don't have nearly the same speed. Builtin arrays 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 s***ct data types in one tightly packed buffer, without any extra type information or other overhead. Thus iterating through is very easy on the cache as everything is aligned.
Builtin arrays运行速度很快。虽然ArrayList或 Array 类很容易使用,你能轻易添加组件,但是他们有完全不同的速度。Builtin arrays有一个固定的长度,而且多数情况下你事先知道最大长度,且能扩展它。builtin arrays最大好处是它直接嵌入s***ct数据类型在一个缓冲区里,不需要额外类型信息或其他。因此更易于做缓存遍历。


    C#
    JavaScript


using UnityEngine;using System.Collections;public class example : MonoBehaviour {private Vector3[] positions;void Awake() {positions = new Vector3[100];int i = 0;while (i < 100) {positions = Vector3.zero;i++;}}}


private var positions : Vector3 [];function Awake () {positions = new Vector3 [100];for (var i=0;i<100;i++)positions = Vector3.zero ;}


5. Don't call a function if you don't have to

如果不是必须,不要调用函数
The simplest and best of all optimizations is to perform less work. For example , when an enemy is far away it is most of the time perfectly acceptable to have the enemy fall asleep. That is do nothing until the player comes close. The slow way of handling this situation would be:
最简单和最好的优化是执行最少的工作。例如,当一个敌人足够远,很多时候可以理想的认为敌人已沉睡。他什么都不做,直到角色接近。慢方法处理这种情况将是这样:


    C#
    JavaScript


using UnityEngine;using System.Collections;public class example : MonoBehaviour {public Transform target;void Update() {if (Vector3.Distance(transform.position, target.position) > 100)return;}}


function Update (){// Early out if the player is too far away.// 尽早离开如果玩家太远if ( Vector3.Distance (transform.position, target.position) > 100)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 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 because 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#
    JavaScript


using UnityEngine;using System.Collections;public class example : MonoBehaviour {void OnBecameVisible() {enabled = ***e;}void OnBecameInvisible() {enabled = false;}}


function OnBecameVisible () {enabled = ***e;}function OnBecameInvisible (){enabled = false;}


2. Use triggers. A simple sphere trigger can work wonders though. You get OnTriggerEnter/Exit calls when exiting the sphere of influence you want
使用触发。一个简单的范围触发能获得惊人的效果。 当你要使物体在你期望的作用范围触发事件的时候,你可以调用OnTriggerEnter/Exit


    C#
    JavaScript


using UnityEngine;using System.Collections;public class example : MonoBehaviour {void OnTriggerEnter(Collider c) {if (c.CompareTag("layer"))enabled = ***e;}void OnTriggerExit(Collider c) {if (c.CompareTag("layer"))enabled = false;}}


function OnTriggerEnter (c : Collider ){if (c.CompareTag("layer"))enabled = ***e;}function OnTriggerExit (c : Collider ){if (c.CompareTag("layer"))enabled = false;}


3. Use Coroutines. The problem with Update calls is that they happen every frame. Quite possibly checking the distance to the player could be performed only every 5 seconds. This would save a lot of processing power.
使用协同程序。 Update的问题是他在每帧都调用。完全可以让它每5秒检查一次玩家的距离,这将节约大量的处理能力。



来源:unity3d圣典 更多分享尽在web3D纳金网http://www.narkii.com/



——————————————————————————


Subsections 章节

     [脚本章节]Scripting Overview 脚本概述
    [脚本章节]Overview: Script compilation 脚本编译(高级)
    [脚本章节]Overview: Performance Optimization 性能优化
    [脚本章节]Overview: The most important classes 重要的类
    [脚本章节]Overview : Writing Scripts in C# 使用C#书写脚本
    [脚本章节]Overview: Coroutines & Yield 协同程序 & 中断
    [脚本章节]Overview: Instantiate 实例
    [脚本章节]Overview: Member Variables & Global Variables 成员变量 & 全局变量
    [脚本章节]Overview: Vectors 向量
    [脚本章节]Overview: Accessing Other Game Objects 访问其他游戏物体
    [脚本章节]Overview: Accessing Other Components 访问其他组件
    [脚本章节]Overview: Keeping Track of Time 记录时间
    [脚本章节]Overview: Common Operations 常用操作
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2024-11-17 21:24 , Processed in 0.138573 second(s), 32 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部