Scripting
- Are you using the right algorithm?Selecting the right algorithm for a task yields much better optimization than any other code tweaking you might do. note that the best algorithm is not always the one with the lowest average complexity. for small datasets, it is often better to use a slow algorithm with a small setup cost than a smart one with high initialization cost. (eg. one wocould use HashTables or binary search trees for large list of data accessed by name, but a simple list and linear search if you are only accessing a few elements. -- although. net's hashtable class already chooses the most optimal method depending on your data in this case .)
- Needless to say, the logic must be correct.
- Keep your fixedupdate functions as lean as possible.These functions can get called around und 50-100 times a second per applicable script per object, so they are a good target to optimize. if there are things that need to be only done when the display is updated, put that code inside the UPDATE function.
-
- If possible, disable scripts on objects when they are not needed.If you have a large level in your game and there is an enemy kilometers away, you can probably switch off its AI script until the camera gets closer. A good way to switch on and off objects is by using gameobject. setactiverecursively (false) and sphere or box colelizers set as triggers.
-
- If possible, disable unnecessary scripts on the gameobject. If you have a big scene in your game and the enemy's location is several kilometers away, you can disable your enemy's AI scripts until they are close to the camera. A good way to enable or disable gameobject is to use setactiverecursively (false) and set the spherical or box-type collision to trigger.
- Beware the empty update functions.When creating new scripts with the assets menu, they include an empty Update () function. remove it if you don't need it as it comes at a (rather light) performance cost. this performance cost applies to all overridable functions in monobehaviour scripts, with update and fixedupdate being the main targets.
-
- Delete an empty update method. When a new script is created through the Assets Directory, the script contains an update method, which is deleted when you do not use it.
-
- Refer to a gameobject by the most logical component.One coshould theoretically write: somegameobject. transform. gameobject. rigidbody. transform. gameobject. rigidbody. transform, but there is a whole lot of needless work done there. if you want to deal with an object's transform, refer to it in your script as such in the first place.
- Reference the most logical component of a game object. Someone may write it like this.Somegameobject. Transform, gameobject. Rigidbody. Transform. gameobject. Rigidbody. Transform, but this does unnecessary work and you can reference it at the beginning, as shown in the following figure:
Private transform mytransform;
Private Rigidbody myrigidbody;
Void start (){
Mytransform = transform;
Myrigidbody = Rigidbody;
}
- Coroutines are your friends.Coroutines have only a tiny overhead and can be preferrable to an update method that is called all the time needlessly. for example, if you had a script to fade in and out a light on command, you coshould do the fading in a coroutine instead of in update. that way, most of the time when the light is not fading, the script takes a minimum amount of performance. if the fading was done in the update function, you wocould be inefficiently polling to see if there is fading to be done.
-
- Collaboration is a good method. Available collaborationProgramInstead of every frame. (The invokerepeating method is also a good method to replace update)
- Don't use methods which search for objects any more than is
Necessary.This includes des methods such as gameobject. findbytag ()
And gameobject. getcomponent (), as well as all the component
Convenience properties (transform, light, etc.). These methods are
Optimised to operate as quickly as possible, but they still have
Search through all the relevant objects to find the one you want.
The most important thing is to avoid calling search methods
Repeatedly in update () or fixedupdate (). Instead, call the Method
Once, store its result in a member variable of your class, and then
Use the member variable to access it the next time you need
It.
-
- The key here is to try not to use the search method in update or fixedupdate. You can get it in the Start method as before.
- Don't use sendmessage () (or similar functions) if you don't
Have.Sendmessage () is at least 100 times slower
Calling a function directly, and this number increases as more
Scripts and functions are available on the object. If you can find
The script you need ahead of time, do so and then call the Function
Directly.
-
- Do not use sendmessage or other methods. It is 100 times slower than the direct call method. You can directly call the method or use the C # Delegate.
-
- Javascript's (and Boo's) Duck typing takes a small amount
Computation.In performance critical areas and when using
Javascript, try to explain icitly say what types of variables you are
Using when declaring them. (although this is often done
Automatically by the compiler through type inference, which is just
As valid tive as specifying the type yourself, so your milage may
Vary)
- when using JavaScript or boo, you 'd better determine the type of the variable instead of dynamic type, which will reduce the efficiency, you can use # pragma
strict at the beginning of the script to check whether the game is compiled incorrectly.