Details and optimization of unity3d procedures

Source: Internet
Author: User

Details and optimization of unity3d procedures

(Mobile-based development)

1, each script created for the use of the start (),. The update () function can be removed, especially the latter, which is updated even if nothing is done.

2, do not do complex mathematical operations, such as root Operation Mathf.sqrt (), and so on, when we seek two objects of distance, you can directly calculate the root of the first level of the operation. Finding trigonometric functions is also a complex operation.

3, if using the Mono editor, in the comments, try to use English comments (prevent Chinese garbled)

4, unity of every component are updated, the script is also counted components, so a different object of the script, the function of similar can be placed in a script, reduce the script component.

5, some components do not need to look for each frame, copy, index, can fully customize the component cache, at the beginning of the code, such as Transform....,gameobject.find ... And so on, you can create a cache or cache group, placed in the start () function. So be cautious about using the unity API

6, the compilation order of a script, Awake ()--start ()--fixedupdate ()--update ()--lateupdate (), for this order, to determine the order of things, such as the generation of objects, and find the object's components, this time, It is necessary to generate the lookup first.

7, the project can be every stage, with performance detector check performance, see where needs to modify, advance modification can reduce the later optimization.

8. Structs run faster than class. The former is in the stack, the latter in the heap.

More than 9 uses static type, can increase speed.

10, in Fixedupdate () to deal with the object update, for non-action games, or do not need to update each frame, you can update multiple frames. Time.framcount% 10 ...

11. The For loop is more useful than the Foreach loop.

12, Int is better than float, so it is not necessary to use float for all data. According to the actual situation adopted.

13, do not have the necessary string connection, as far as possible to remove, can also be integrated into a string, otherwise it will leave a lot of memory garbage. About 4 connection strings are more than 1kb of memory garbage from a string.

14, if (Go.comparetag (")) is better than if (go.tag==" "), the principle of matching the Comparetag with a different string will immediately return false results.

15. Use the level of detail to turn off functions that are no longer felt. For example, when a distant enemy AI is turned off, the physical collider can be turned off and the particle system can be turned off. That is, the best way to raise performance is to do nothing.

16, minimize collision detection, such as Ray cast, as far as possible from each check to obtain more information.

17. Reduce the call of the role Controller Move command, you can cache the move request per frame.

18, similar to get the length of arrays and array list, should be at the initial time to get the length, do not in the update function to cycle the calculation of his length.

19, reduce the use of temporary variables, each update will allocate space.

20, do not easily change the name of the public array.

21, onbecamevisible () when visible, call this function;

Onbecameinvisible () Call this function when the camera is not visible, and use the code for independent objects such as special effects.

22, about component lookup, preferably placed in the awake () function, or placed at the start () function at the beginning of the position, to prevent the occurrence of the corresponding component is not found, this is the result of code order.

23, about Gui.matrix matrix to control the GUI resolution adaptation, the UI can not use screen self-bandwidth high control, use absolute size to be effective.

Details and optimization of unity3d procedures

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.