The road of Unity3d optimization

Source: Internet
Author: User

Unity3d optimization of the road. U3d's architecture section has been much talked about, here I would like to talk about u3d optimization of the hands-on experience.

The road to optimization is divided into three blocks:

One. Render level.

GUI part: I use Ngui, it to dynamic movement, rotation, scaling GUI support is relatively poor, so I try not to put too much mobile rotation scaling part of the GUI, but in many cases can not be avoided, such as: a large number of damage figures, items drop, icon movement and rotation, etc. In order not to allow the GUI to control these rendered objects, a small part of me uses 3D patches instead, and most of the programs used to generate patch rendering are out of control of the GUI. In addition to those static GUI, I used static object optimization properties, coupled with the elimination of unnecessary GUI settings, so that the GUI part of the efficiency is high enough.

3D: Special effects are the most influential part of the screen effect, I try not to use particles or reduce the number of particles to a minimum. Minimize the use of light, instead of using a baked figure. Eliminate those models that do not need to be displayed.

2D: Special effects are not placed in the GUI but instead are placed in another camera other than the GUI. So, the final camera will have more than one, the total stack is this: Top:gui_effect_camera, Middle:gui_camera, Bottom:3d_camera;

Two. Reduce the amount of space occupied.

1. I am using the dynamic resource loading mode www mode. So I use Buildassetbundleoptions.disablewritetypetree packaging to reduce the size of the package when I export the resource .

Because the WWW cache mode after the download after the decompression or will increase the space consumption, so I use streamassets this in Unity3d fixed folder, put the WWW resources there, after the game began to buffer read, so as to reduce the space consumption, It is also possible to read resources asynchronously, with the only drawback being that any change to a resource requires the player to download a new game pack. For the two ways of the WWW resource load, in the specific project can be used together, part of the use of Network load, part of the use of local streamassets load.

2. For each texture, the image is set to compress.

3. Let the art reduce the number of model patches, and in the FBX model settings, set the model to compress

3. Let the art reduce the number of animation frames, and in the animation settings, set the animation compression

4. Do not use System,system.xml and other system-brought DLL, he will be a few more megabytes of space, and with open source source code instead.

Three. Memory

1. The most basic is to load the required resources, after use, release.

The 2.GUI section adds a part of the Guimanager management class that periodically checks the GUI to release the GUI resources that are not displayed.

3. Because there are times when one-time loading of too many resources, memory will suddenly swell, io too slow lead to the crash, so I choose the resources asynchronous loading. Makes loading so many resources less scary and smoother.

4. Memory release: Here's the point, I'm focused on testing the memory release of the neglect point. When we use Ngui, or 2dtoolkit for GUI programming, it is common to forget to set the GUI reference to NULL when releasing its GUI nodes, which results in a memory leak. Some stickers or instance data that you no longer use continue to persist in memory. I also did an experiment on whether there was a need to set these GUI variables to null.

First put the screen empty, no extra memory, then the GUI display, and then hide the GUI to destroy the Gameobject, first set all the reference variables to null, then display, then destroy, this time destroy, do not set the variable to null. The memory concludes that the GUI component variable is not NULL, and the map memory is stuck. Such as:

The test code is as follows:

  1. Using Unityengine;
  2. Using System.Collections;
  3. Public class Testgui
  4. {
  5. private Tk2dsprite Tex;
  6. private gameobject root;
  7. private Tk2dsprite tex1;
  8. private Gameobject root1;
  9. private Tk2dsprite tex2;
  10. private Gameobject Root2;
  11. public Void Show ()
  12. {
  13. Root = Gameobject.instantiate (Resources.load ("Gameobject")) as gameobject;
  14. Root.transform.parent = Gameobject.find ("Root/middle_center"). Transform;
  15. Tex = root. Getcomponent<tk2dsprite> ();
  16. ROOT1 = Gameobject.instantiate (Resources.load ("Gameobject1″)") as Gameobject;
  17. Root1.transform.parent = Gameobject.find ("Root/middle_center"). Transform;
  18. Tex1 = Root1. Getcomponent<tk2dsprite> ();
  19. Root2 = Gameobject.instantiate (Resources.load ("Gameobject2″)") as Gameobject;
  20. Root2.transform.parent = Gameobject.find ("Root/middle_center"). Transform;
  21. Tex2 = Root2. Getcomponent<tk2dsprite> ();
  22. }
  23. public void Hiden1 ()
  24. {
  25. Gameobject.destroy (root);
  26. Gameobject.destroy (ROOT1);
  27. Gameobject.destroy (ROOT2);
  28. Resources.unloadunusedassets ();
  29. }
  30. public void Hiden ()
  31. {
  32. Gameobject.destroy (root);
  33. Gameobject.destroy (ROOT1);
  34. Gameobject.destroy (ROOT2);
  35. root = null;
  36. ROOT1 = null;
  37. Root2 = null;
  38. Tex = null;
  39. TEX1 = null;
  40. TEX2 = null;
  41. Resources.unloadunusedassets ();
  42. }
  43. }

Please look at the green curve in the red box, the first time is the empty case, the memory is destroyed immediately, and the second time is pail empty, the memory still resides. The conclusion is obvious, so it is very important to set the reference blank when you are programming, which directly affects your memory usage. This is also a matter of C # garbage collection mechanism, when the instance does not have a reference quantity when the memory will be recycled, and completely destroyed.

The author finally reminds the optimization no limit, in fact, in detail, can save a little CPU is a point, can save a little memory is a bit. If you don't beat 99.5% of others, you won't have a chance to be successful.

The road of Unity3d optimization

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