This is the original.
The performance of mobile games is extremely important, especially for fast-paced action games. As you may know, we are now doing a pinball game on the Android/ios platform, characterized by a fast physics engine in a 3D world combined with full character animations, any pinball game that requires 60fps of performance, in order to achieve it we need to squeeze the device CPU bit by bit.
So, the challenge is to keep the game running smoothly at least 60fps. As long as the lower to 60fps below will reduce the frame rate by half! Switching between 60fps-30fps means a serious hick-up. And if it happens frequently, it will bring a bad game experience.
Limit situation, we want to support the oldest iphone 3GS, and to run above 30fps.
Above:momo and Fry is beating up two owl bandits– four roles appear on the screen at the same time.
So performance is very important, how do you improve performance when you use unity to develop an Android/iphone game? There are four unity tools we used in the past to optimize the frame rate of the iphone/Android version of Momonga.
1. Using Proformance Profiler
The first thing to do is to look at the unity Profiler that you want to improve the performance of the game, which is a Unity Pro feature that lets you analyze performance bottlenecks. The parser is an invaluable tool through which you can determine where all the frame rate problems originate. You run the game on your target device, and then run the Analyzer on your PC, and when you start the game, the profiler displays performance data.
The easy part is that you can run Profiler on a Windows machine to analyze an iphone or ipad.
Using Profiler on your device, simply build a game in developer mode. Through the Unity Documentation:
In order to be able to connect a device, the device must find the development build in the Buildsettings dialog box and tick the editor and player AutoConnect, which may also need to be checked.
When you play the game Analyzer shows the graph of CPU usage, simply keep your device connected with your development machine, then play the game, the analyzer will show all bottlenecks and hick-ups in real time. The parser categorizes activities in some places: rendering, scripting, physics, garbage collection, vertical synchronization, and others.
For mobile games, the problem is usually related to rendering, such as Momonga, which consumes 40% of the CPU, which is a point to our attention: we try to reduce the number of vertices and poly.
The occasional spike in the script when loading the scene-but there is a low frame rate when loading the scene-it's not unusual. You can still see some spikes, most of which are related to the physics engine, which will be the next step in our attention to further improving performance.
2. static batch processing (static batching)
Static batching is a feature of unity that saves a lot of CPU cycles. It's working like this.
Each time an object is rendered there will be a "Draw call"-a command that tells the CPU or GPU, which needs to be rendered. Unity emits several drawcalls and then puts them on top of other drawcalls to make the scene complete. However, each draw call has CPU overhead, and you want to minimize draw call and compare a draw call to a blank canvas that needs to be painted, do you want to paint 1 or 50? That's what I was thinking.
Batching, which ensures that no unnecessary draw call is used, batching has two flavors: static and dynamic.
Static gives you the best performance, so we always use static batch processing. Learn the details to see here.
Use static batching effectively you need to use as few different materials as possible. In order to achieve this you need to integrate your material into a large texture, we choose to use vertex color and all objects have the same pattern of texture, vertex shading allows you to give each vertex a color, remove the need for real-time illumination, the color will be directly drawn on the model by our artist. This means that all of our props and objects in the environment use the same material, static batch rods.
Above: When static batching runs, note that these objects are rendered in a separate draw call.
Using vertex colors only looks a bit less, so we have a pair of textures multiplied by the vertex color to give the object a little more "texture".
Above: Vertex color and a pattern texture
The final step is to add a ray map in the scene, because we have almost no texture memory for the object itself, and we can have a fairly detailed ray mapping without memory problems.
Static batching requires you to select "Static" on the Ojbect properties panel, which can only be used on objects that are not moved, rotated, or scaled in the scene.
3. Dynamic batch processing (batching)
When static batching is not the best choice, dynamic batching can save the situation, in fact, it is always used in what is not static objects, using it you just use as small as possible objects and as few vertices as possible.
Batching dynamic objects Each vertex has some overhead, so the batch only supports mesh objects with a mesh containing less than 900 vertices.
Our shaders use vertex positions, Uvs, and colors, so each of our objects can have up to 300 vertices.
Some helpful tips from Unity manual:
- Batching a dynamic object has some overhead for each vertex, so batching is only allowed to use the number of meshes contained in the grid and less than 900 vertex attributes.
- If your shader is using vertex positions, normals and single Uvs, you can batch 300 vertices, and if your shader is using vertex position, normals, UV0, UV1 and tangent vectors, you will only be able to handle 180 vertices.
- Do not use scaled dimensions, objects with scaled dimensions (1,1,1) and (2,2,2) will not be batched.
- Objects with uniformly scaled scales are not batched with objects that are not uniformly scaled.
- Two objects using scale scales (1,1,1,) and (1,2,1) are not batched, but two objects using the scale scale (1,2,1) and (1,3,1) will be batch-processed.
- Instantiating an object using a different material will cause the batch to fail.
- Objects with Lightmap contain additional (hidden) material attributes, such as lightmap offset and scaling coefficients. So objects that have lightmap will not be batched (unless they point to the same part of Lightmap).
- Multi-channel shader can interfere with batch operations. For example, almost all shaders in unity support multiple light sources in forward rendering and effectively open multiple channels for them.
- The case of the preset body automatically uses the same mesh model and material.
There are also some important tips: If an object has an animation but part of it never moves, you can mark that part as static, and he will not affect the animation.
Dynamic batching is very useful for star pickups and other small objects that's animated but be otherwise the same in T He scene.
Dynamic batching is very useful for star pickups and some small objects that are moving but are in the same scene.
4. Audio Optimization (Optimizations)
We had some serious performance problems, and then we dug up the problem and found that the main culprit was the audio
The Unity component document has such a passage about audio clips:
As a general rule, compressing audio (or modules) is suitable for raised here files like background music or dialog boxes, while native is better for short sound sounds.
This is a good suggestion, but there is a problem that we find that playing audio with incorrect compression settings can disrupt your memory usage or cause CPU spikes.
Reading the Unity Manual helps us locate the problem:
"Note that is only one hardware audio stream can is decompressed at a time."
"Please note that only one hardware audio stream can be decompressed at a time. ”
We see CPU spikes because the iphone uses a hardware decoder to decompress only one audio file at a time.
This can be solved by using the "decompress on Load" setting, but it's a boost, but it's causing a spike in memory, so use it when you really need it.
When using different types of audio, we have some rules of thumb for optimal performance:
- Short Clips-Local
- Long (or looped) clips-compressed in memory
- Music-disc stream (stream from disc)
- Files that cause CPU spikes-unzip at load time
If you follow these rules, audio should not bring you too much trouble unless you start all the sound files at once.
Conclusion
The most important thing for a mobile game is to keep your triangles, vertices, and drawcall as little as possible, and if you encounter performance problems, Unity offers a lot of tools to get your game back in control and it will not disappoint you in reading the document.
That's all we have now.
What are your tips for improving performance? Let us know in the comments.
4 ways to improve your Unity game performance