Original address:
Http://unity.jb51.net/chengxukaifa/kaifajingyan/1233.html
1. When using arrays or ArrayList objects, you should pay attention to
Length=myarray.length;
for (int i=0;i<length;i++)
{
}
Avoid
for (int i=0;i<myarray.length;i++)
{
}
2, if it is not necessary to process each frame, it can be processed every few frames
void Update () { if (time.framecount%6==0) {dosomething ();}}
3, timed repeat call can be implemented using the Invokerepeating function, for example, 0.5 seconds after the start of every 1 seconds to execute the dosomething function:
void Start ()
{
Invokerepeating ("dosomething", 0.5f, 1.0f);
}
4, less use of temporary variables, especially in the update Ongui, such as real-time call functions.
void Update ()
{
Vector3 POS;
Pos=transform.position;
}
Can be changed to:
Private Vector3 POS;
void Update ()
{
Pos=transform.position;
}
5, the active garbage collection
void Update ()
{
if (time.framecount%50==0)
{
System.GC.Collection ();
}
}
6, optimization of mathematical operations, to avoid the use of float, and use of int, especially in mobile games, as little as possible with complex mathematical functions, such as Sin,cos functions. Divide/Multiply, for example: Use x*0.5f instead of x/2.0f.
7. Compressed Mesh
After importing the 3D model, it is a good idea to open the Mesh Compression without affecting the display effect. OFF, Low, Medium, and high these options can be selected as appropriate. It is best to use one material for a single mesh.
8, the operation to minimize Tris and Draw Calls
When previewing, you can open Stats to see the overhead of drawing rendering. Pay special attention to the two parameters Tris and Draw Calls. In general, to achieve: Tris remain below 7.5k, Draw Calls remain below 35.
9. Avoid a lot of built-in Mesh with Unity's own Sphere
Unity built-in Mesh, the number of polygons is larger, if the object is not required to be particularly smooth, can be imported into other simple 3D model instead.
10, if possible, will gameobject unnecessary script disable off. If you have a big scene in your game and the enemy's location is thousands of meters unexpectedly, this is where you can disable your enemy AI scripts until they are close to the camera. A good way to turn on or off Gameobject is to use setactiverecursively (false), and the spherical or box-type collider is set to trigger.
11. Delete the empty Update method. When you create a new script from the assets directory, the script includes an Update method that deletes it when you are not using it.
12. Refer to the most logical component of a game object. Some people may write somegameobject.transform,gameobject.rigidbody.transform.gameobject.rigidbody.transform, but do some unnecessary work, you can in the first place to lead Use it, like this:
Privatetransform Mytrans;
void Start ()
{
Mytrans=transform;
}
13, Synergy is a good way. You can use a synergistic program instead of a method that does not have to be executed per frame. (There are also invokerepeating methods that are a good alternative to update).
14, try not to update or fixedupdate using the search method (such as Gameobject.find ()), you can get it in the Start method as before.
15, do not use methods such as SendMessage, he is 100 times times slower than the direct call method, you can directly invoke or through the C # Delegate to achieve.
16, when using JavaScript or boo language, you'd better determine the type of the variable, do not use the dynamic type, which will reduce the efficiency, you can use #pragmastrict at the beginning of the script to check, so that when you compile your game there will be no inexplicable error.
Unity3d Code and Efficiency optimization Summary (difficulty 1 recommended 3)