Unity3d Code and Efficiency optimization Summary (difficulty 1 recommended 3)

Source: Internet
Author: User
Tags mathematical functions

Original address:

Http://unity.jb51.net/chengxukaifa/kaifajingyan/1233.html


1. When using arrays or ArrayList objects, you should pay attention to

    1. Length=myarray.length;

    2. for (int i=0;i<length;i++)

    3. {

    4. }

Avoid

    1. for (int i=0;i<myarray.length;i++)

    2. {

    3. }

2, if it is not necessary to process each frame, it can be processed every few frames

    1. 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:

    1. void Start ()

    2. {

    3. Invokerepeating ("dosomething", 0.5f, 1.0f);

    4. }


4, less use of temporary variables, especially in the update Ongui, such as real-time call functions.

    1. void Update ()

    2. {

    3. Vector3 POS;

    4. Pos=transform.position;

    5. }

Can be changed to:

    1. Private Vector3 POS;

    2. void Update ()

    3. {

    4. Pos=transform.position;

    5. }

5, the active garbage collection

    1. void Update ()

    2. {

    3. if (time.framecount%50==0)

    4. {

    5. System.GC.Collection ();

    6. }

    7. }


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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.