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:
- Using Unityengine;
- Using System.Collections;
- Public class Testgui
- {
- private Tk2dsprite Tex;
- private gameobject root;
- private Tk2dsprite tex1;
- private Gameobject root1;
- private Tk2dsprite tex2;
- private Gameobject Root2;
- public Void Show ()
- {
- Root = Gameobject.instantiate (Resources.load ("Gameobject")) as gameobject;
- Root.transform.parent = Gameobject.find ("Root/middle_center"). Transform;
- Tex = root. Getcomponent<tk2dsprite> ();
- ROOT1 = Gameobject.instantiate (Resources.load ("Gameobject1″)") as Gameobject;
- Root1.transform.parent = Gameobject.find ("Root/middle_center"). Transform;
- Tex1 = Root1. Getcomponent<tk2dsprite> ();
- Root2 = Gameobject.instantiate (Resources.load ("Gameobject2″)") as Gameobject;
- Root2.transform.parent = Gameobject.find ("Root/middle_center"). Transform;
- Tex2 = Root2. Getcomponent<tk2dsprite> ();
- }
- public void Hiden1 ()
- {
- Gameobject.destroy (root);
- Gameobject.destroy (ROOT1);
- Gameobject.destroy (ROOT2);
- Resources.unloadunusedassets ();
- }
- public void Hiden ()
- {
- Gameobject.destroy (root);
- Gameobject.destroy (ROOT1);
- Gameobject.destroy (ROOT2);
- root = null;
- ROOT1 = null;
- Root2 = null;
- Tex = null;
- TEX1 = null;
- TEX2 = null;
- Resources.unloadunusedassets ();
- }
- }
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