Unity3D optimization Summary

Source: Internet
Author: User
Tags mathematical functions

1. When using an array or ArrayList object, note that

length=myArray.Length;for(int i=0;i<length;i++){}

Avoid

for(int i=0;i<myArray.Length;i++) {}

2. If you do not need to process each frame, you can process it every few frames.

void Update(){ if(Time.frameCount%6==0) { DoSomething(); }}

3. You can use the InvokeRepeating function to implement regular and repeated calls. For example, you can run the DoSomeThing function once every 1 second after 0.5 seconds:

void Start() {       InvokeRepeating("DoSomeThing", 0.5f, 1.0f);} 

4. Use less temporary variables, especially in functions called in real time such as Update OnGUI.

void Update(){   Vector3 pos;   pos=transform.position;}

You can change it:

private Vector3 pos;void Update(){   pos=transform.position;}

5. Take the initiative to recycle garbage

void Update(){    if(Time.frameCount%50==0)    {       System.GC.Collection();    }}

6. Optimize mathematical operations to avoid float and use int, especially in mobile games. Use as few complex mathematical functions as possible, such as sin and cos. Change Division/to multiplication. For example, use x * 0.5f instead of x/2.0f.

7. compress the Mesh

After importing a 3D model, it is best to enable Mesh Compression without affecting the display effect. Off, Low, Medium, and High options can be selected as appropriate. It is best to use one material for a single Mesh.

8. Minimize Tris and Draw CILS during runtime

During preview, you can click Stats to view the overhead of graphic rendering. Pay special attention to the Tris and Draw CILS parameters. In general, Tris should be kept below 7.5 kb, and Draw cballs should be kept below 35.

9. Avoid extensive use of built-in Mesh such as Sphere provided by Unity.

In the built-in Mesh of Unity, the number of polygon is relatively large. if the object is not particularly smooth, you can import other simple 3D models instead.

10. 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.

11. Delete the 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.
12. reference the most logical component of a game object. Someone may write someGameObject like this. transform, gameObject. rigidbody. transform. gameObject. rigidbody. transform, but it does some unnecessary work. You can reference it at the beginning, like this:

PrivateTransform myTrans;

Void Start ()

{

MyTrans = transform;

}

 

13. Collaboration is a good method. You can use a program to replace the method that does not have to be executed every frame. (The InvokeRepeating method is also a good method to replace Update ).

14. Try not to use the search method (such as GameObject. Find () in Update or FixedUpdate. You can get it in the Start method as before.

15. 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.

16. When using javascript or Boo language, you 'd better determine the type of the variable instead of dynamic type, which will reduce the efficiency. You can use # pragmastrict at the beginning of the script to check the efficiency, in this way, when you compile your game, there will be no inexplicable errors.

 

 

 

 

 

 

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.