1. Use static type An important optimization when using JavaScript is to replace dynamic types with static types. Unity uses a technology called type inference to automatically convert JavaScript to a static type script. VaRFoo = 5; In the preceding example, foo is automatically deduced as an integer. Therefore, unity may use a large amount of editing time for optimization, rather than time-consuming dynamic name variable search. This is one of the reasons why the average speed of JavaScript Execution in unity is 20 times that of other JavaScript. The only problem is that sometimes not all things can make type inference, and unity will re-use dynamic types for these variables. In this way, write JavascriptCodeIt is simple, but it also slows down the code. Let's look at an example: Function Start() { VaRFoo = getcomponent (myscript ); Foo. dosomething (); } Here Foo will be a dynamic type, so the call function dosomething must take a long time. Because the foo type is unknown, it must understand whether the function supports dosomething. If yes, call the function. Function Start() { VaRFoo: myscript = getcomponent (myscript ); Foo. dosomething (); } Here we force Foo to be of the specified type, and you will get better performance. 2. Use # pragma strict Now the problem is that you usually don't realize that you are using dynamic types. # Pragma strict can solve this problem! Simply add # pragma strict at the top of the script. Then, unity will disable the dynamic type of the script and force you to use the static type. If a type is unknown, unity reports a compilation error. Below, Foo will report an error during compilation: # Pragma strict Function Start() { VaRFoo = getcomponent (myscript ); Foo. dosomething (); } 3. cache component search Another optimization is component caching. This optimization requires some code and is not always necessary. However, if your code is really large and you need to improve the performance as much as possible, it will be well optimized. When you get a component or variable through getcomponent, unity must find the correct component from the game object. Then you can reference a private variable through a cache component. Set: Function Update(){ Transform. Translate (0, 0, 5 ); } Convert: Private VaRMytransform: transform; Function Awake(){ Mytransform = transform; } Function Update(){ Mytransform. Translate (0, 0, 5 ); } The subsequent code runs fast, because unity does not need to look for transform components at each frame. Script components are also supported. You can use getcomponent to obtain components or other shortcut attributes. 4. Use the built-in array The built-in array is very fast. The arraylist or array class is easy to use and you can easily add components. But they have different speeds. The built-in array has a fixed length, and most of the time you know the maximum length before filling it. The best thing about built-in arrays is that they directly embed structured data types in a tight cache without any additional type information or other overhead. Therefore, it is very easy to traverse it in the cache, because each element is aligned. Private VaRPositions: vector3 []; Function Awake(){ Positions =NewVector3 [100]; For(VaRI = 0; I <100; I ++) Positions [I] = vector3.zero; } 5. Do not call a function unless necessary The simplest and best optimization is to perform the least work. For example, it is usually feasible to put an enemy in sleep when it is in a distance. When the player is near, you can do this: Function Update() { // Early out if the player is too far away. If(Vector3.distance (transform. Position, target. Position)> 100) Return; Perform real work... } This is not a good method, although unity has to access the UPDATE function at each frame. A better way is to disable this behavior until the player approaches. There are 3 methods to do this: Use onbecamevisible and onbecameinvisible. These calls are associated with the rendering system. Once the camera sees an object, onbecamevisible will be called. If it does not, onbecameinvisible will be called. This is sometimes useful. But it is usually useless for AI, because when you turn your enemy back, the enemy becomes unavailable. Function Onbecamevisible(){ Enabled = true; } Function Onbecameinvisible() { Enabled =False; } 2. Use a trigger. A Simple spherical trigger can produce amazing results. You can call ontriggerenter/exit when entering the desired scope of action. Function Ontriggerenter(C: Collider) { If(C. comparetag ("Player")) Enabled = true; } Function Ontriggerexit(C: Collider) { If(C. comparetag ("Player")) Enabled =False; } 3. Use collaborationProgram. The problem with update is that it occurs at each frame. It may take only five seconds to check the player distance. This can save a lot of processing cycles. By: cartoonboy |