Unity Android Performance Optimization

Transcription

Unity android performance optimization

Unity mobile performance tips. Unity android performance. Unity android optimization.Are you planning to optimize your game made with Unity and get the best performance? In this Complete Unity Optimization Guide, I will show you how to increase performance in Unity correctly, so your game will run fast & stable. Here I will consider optimization tools provided by Unity Game Engine and also by Indie Developers (Publishers) inUnity Asset Store — Top Unity Assets (Tools) for Unity Optimization. I will also deep into details of common performance issues in video games and find solutions for them: optimization of code and assets, tips & tricks. Main Optimization Cycle in Unity Unity Optimization refers to the process of making your game run better. Usually the main reasonfor optimization is to make the gameplay smoother, or to make the game more available to a wider audience so the game runs better on lower end devices. Next will be presented common optimization cycle, which you need to go sequentially every time when noticeably low FPS suddenly occurs in your game after next stage of development: If yourgame works without lags and freezes on target platforms, then the game does not need to be optimized. Do more important things like new levels in current game. Optimization for the sake of optimization is a waste of time. If you have lags and freezes then the starting point is Profiling (firstly with Unity Profiler) to detect what kind of performanceissues Unity Project has.After the optimization tasks have been determined and before the process of optimization is started, it is advisable to take some time and consider which area of optimization is the priority. This is done by comparing the approximated time each specific implementing of optimization would take and a performance gain inresult.Resulting ordered list of optimization tasks will work as a guideline for the entire process of optimization starting with the most effective action. This also helps to gain the most performance with the least efforts. Follow this list until the game on target platforms has smooth gameplay and stable frame rate.If the game is executing excellentwithout visible problems and the list of optimization tasks is not empty then forget further optimization process.If the game still has problems then project should be profiled and the data stored. Next stored data can be compared with a previous data allowing for closer inspection of the state of the whole optimization process.Repeated profiling allowsyou to understand what happened after the completion of a single optimization task, whether this procedure brought a performance improvement or not. Types of Performance Issues You can Optimize Unity Game in different ways depending on problem nature. Performance problems can be divided into several types depending on their nature.Spikes Spike is a sudden drop in the frame rate of a game. This is noticed when a game suddenly stops and doesn’t move for a noticeable time. This can break the immersion of the player or cause him to make a mistake he wouldn’t have otherwise made. Spikes can be seen as a high points on Profiler Graph. Spikes are mainly caused by complexcalculations or difficult operations performed during a single frame. Annoying Spikes are a problem in high-intensity games which need a stable frame rate and high control over the game to feel good, such as driving or shooting games (FPS). Spikes of Garbage Collector Garbage collection spikes are frame rate drops specifically caused by thegarbage collection system of Unity. These can be huge frame rate spikes and are easily noticed while playing the game. The spikes happen when the memory garbage limits met and the collection runs, cleaning up the unnecessary objects from the memory. Their frequency is mandated by how much garbage the game generates each frame. Thefrequency can be slowed down by generating less garbage during runtime. The only way of preventing these spikes completely is to generate no garbage during runtime. This is a huge undertaking and must be considered from the very beginning of a project. Every-frame costs Every-frame costs are the calculations and operations that are run everysingle frame. These can be, for example, physics calculations, running AI behavior or handling animations of characters. Every-frame costs slow down the general frame rate of the game. They are the little things that slow the game down and make it feel less fluid. If a game just generally runs poorly, this is the area that needs work. Loading TimeLoading time refers to how long the game takes to load. This includes the first load when the game is opened, and loading that happens during runtime, for example between scenes. While not usually a major issue, having extremely long loading times or having loading screens appear far too often can negatively affect the user experience. To reducethe length of loading screens, consider splitting up the work done during them. This can mean preloading assets beforehand to reduce the number of objects that need to be loaded during loading screen, or reducing the complexity of loaded scenes. Open World In an open world game, where many objects need to be loaded during runtime, a methodof recycling or streaming assets can be implemented. In “Inside” Game a small part of every frame is dedicated to loading and unloading assets. This allows the whole four-hour experience to be played through with just a single initial loading screen. Memory Usage RAM (Random Access Memory) is the memory on which a game is loaded whilerunning. RAM stores everything needed by a game during runtime. VRAM (Video Random Access Memory) is on a graphics card and is intended for graphical effects. VRAM is the storage used to store textures and models drawn by the graphics card. RAM is what your CPU uses, while VRAM is what your GPU uses. Either or both might become abottleneck on a project running with unoptimized or simply with far too many assets. When there is not enough memory, stuttering may occur. To reduce the amount of memory needed by your project, consider next: Reduce the complexity of objects and the resolution of textures;Make less unique assets in the project;Use Object Pools. Profiling Toknow what is the issue with the performance of a project, profiling is needed. Profiling assists in understanding the unique issues found in a project, and should always be the starting point for optimizing a project. Process of Profiling means reading the performance data of the project and finding out where the performance issues are located. Fromthis data bottlenecks can be found. Unity provides some different tools for profiling: This chapter goes through each of these tools, analyses them and gives information on how to use them. Connection to Profiler Unity Profiler and Frame Debugger can both be used to profile a project currently running on the same editor. However, the editor affectsthe performance of the project and the profiling information can be inaccurate. For this reason, if accurate profiling data is needed, a separate build should be created for profiling purposes. Making a build for profiling To make a build which can be profiled, turn on options “Development build” and “Autoconnect Profiler” in Build Setting beforemaking a build. This allows Unity Editor to find the build and for the Profiler to connect to it. Profiling External Devices To get accurate profiling data, the build should be profiled on end user device. When you optimize Unity Game for Mobiles then Profiling on a powerful PC gives little insight on how a user will experience the game. The easiest wayto connect the Profiler to an external device is to have the two devices share the same local network. If there is a device running a development build of the project, it should be selected in the “Editor” dropdown menu of Unity Profiler, Frame Debugger or Console. Once the correct device has been selected, the Profiler should automatically startprofiling it. Profiling on an external device still affects the performance, but the effect is less than when profiling on the same device. Profiling of Mobile Devices Modern mobile devices are powerful but often lack an active cooling system. Because of this, the heat of the device affects its performance highly. On mobile devices, the performance of agame can be directly compared to the heat level of the device. When the device is just turned on and the application is launched, the performance is at its best. Once the device has warmed up the performance starts to fall as the device tries to cool itself. And again, once the device has cooled down enough, performance is improved. Mobile devicescan be profiled through USB cable. To effectively profile a mobile device, it should be warmed up first. After the build has run for 10 minutes, and the device has warmed up properly, the profiling information becomes more accurate and more applicable to an actual user experience. Unity Profiler Unity Profiler is the main tool for profiling UnityProjects. It provides much information on the different performance areas of a project: CPU & GPU Usage;Rendering Info;Memory usage;Audio & Video;Physics (2D & 3D);Network Messages & Operations;UI;Global Illumination. This tool is the beginning of every optimization process on Unity. From information provided by it, bottlenecks are foundand from these bottlenecks the first optimization tasks are created. Learning to use it helps to understand the work of Unity Engine and Current Project. Using the Profiler Profiler Window can be opened through the “Window Analysis” tab of Unity Editor, or by pressing the shortcut CTRL 7 on Windows or CMD 7 on Mac OS X. Once Unity Profilerhas been connected to a running build and the record button has been pressed, profiling information will begin to be collected and displayed in the window. To stop or pause Unity Profiler simply toggle the Record button. On the left side of the window the different profiling categories can be seen, and more can be added from the “Add Profiler”dropdown menu at the top. When profiling, information on each active category appears on the right side of the window. Each slice of the information presents a single frame. On the CPU usage category, execution time the frame is presented by the height of the slice. Frames can be switched by pressing the arrow keys on the window, or by clicking aslice with the mouse. Information on the selected category and target frame appears at the bottom of the screen. Hierarchy Mode Next to the function name different values are shown. The values provide information on the performance of the function in the selected frame. These values are the percentage of the total CPU time spend on this function,the number of times the function is called in the chosen frame, how much garbage the function generated and the time it took to finish the function in milliseconds (total time including inner functions (Time ms) and without them (Self ms)). If these functions themselves call other functions, a small arrow is shown next to the name of the function.Clicking this arrow opens a function hierarchy that shows how the full time of the function is split among the functions called by it. The height of the lines shown on the upper half of the window tells how long that frame took to finish. If these lines have noticeable spikes in them, there has been a frame rate drop. Hunting down what function causedthe spike and optimizing it is a way of making the frame rate and gameplay experience smoother. Hierarchy Raw Mode Hierarchy Raw Mode will separate global Unity function calls into individual lines. This will tend to make Profiled Data more difficult to read, but may helpful if we are trying to: understand how Unity Engine works,to count howmany times a particular global method has been invoked,to determine if one of these calls is costing more CPU/Memory than expected. Timeline You can also use Timeline Mode of Profiled Data. It’s just another way to show the data. You have in quick glance the whole situation of a frame in order of occurrence for all threads. So events showed at theleft are the first that happened and the right are the last. Timeline Mode can be useful since it show more details than the normal version, and if you will click on some block then Timeline will tell you what Game Object connected to this block (e.g., for Skinned Mesh Renderer or Animator). It is useful to discover some bottlenecks, maybe you cannotice the rendering is taking very little but it’s waiting a lot for CPU to send commands. You have the same data on Hierarchy Mode of Profiler but for some cases it’s easier to spot certain issues as you can graphically compare two items. Hierarchy Mode of Profiler is better when you are focusing on specific parts, the Timeline Mode it’s better for ageneral picture. Unity Profile Analyzer Unity Profile Analyzer (package) the Unity Profiler’s single-frame analysis by adding the ability to analyze multiple frames at once. This is useful when it’s important to have a wider view of your performance, such as: upgrading Unity versions,testing optimization benefits,tracking performance as part of yourdevelopment cycle. It analyzes CPU frame and marker data that is pulled from the active set of frames currently loaded in the Unity Profiler or loaded from a previously saved Profile Analyzer session. The analyzed CPU frame and marker data gets summarized and graphed using histograms, and box and whisker plots, which compliment ordered list ofactivity for each marker, including: minimum,maximum,mean,instance count,range,which frame the marker first appeared in. The Profile Analyzer’s Compare view lets you load two data sets at once to measure the effect of an optimization, settings change or Unity version update. A comprehensive filtering system is available in both the Single andCompare views to limit the analysis by marker, thread, frame, and stack depth. Getting Started with Profile Analyzer Use Unity 2018.3 or higher: Open your project in Unity and go to “Window Package Manager”.If you don’t see “Profile Analyzer” in package list then open the “Advanced” drop-down and make sure “Show Preview Packages” isset.Select the “Profile Analyzer” & click “Install”.Open “Window Analysis Profile Analyzer”. Introducing the Profile Analyzer. Unity Frame Debugger Draw Call — single task performed on the GPU used to draw the screen. A single frame consists of multiple Draw Calls. Modern 3D games with complex graphical assets and effects may needthousands of Draw Calls to render the screen each frame. Reducing the number of Draw Calls is a simple method to reduce the workload of the GPU. To get information on what the GPU spends its Draw Calls on, Unity Frame Debugger can be used. By using the tool, more insight on the rendering process can be got. It also is used to find the areaswhere the most Draw Calls are spent, and where optimization is required. Frame Debugger Window can be opened through the “Window Analysis” tab of Unity Editor. Frame Debugger is connected the same way as Unity Profiler and can easily be used either inside Unity Editor, debugging “Game” Window, or on a separate device. Debugging abuild on the same device as the editor can cause some issues like window freeze. Once Frame Debugger has been activated, it freezes the profiled build and collects data on the frozen frame. Once the data has been collected, information on every Draw Call used to render that frame appears on the left side of the window. Draw Calls are organizedinto hierarchies such as drawing and image effects. Next to the hierarchy name is shown the amount of Draw Calls it took to finish rendering that area. By switching between Draw Calls on Frame Debugger Window, changes can be seen on the frozen frame of profiled build (or in the “Game” View in Unity Editor if you are profiling without build) asthe debugger shows what was drawn by each call. By clicking a single Draw Call, more information on that specific call appears on the right side of the window. Switching between Draw Calls and watching the frozen screen change can give enough of an idea on where optimization is required. If the most of Draw Calls are spent drawing a singleobject or a character in the scene, work on that object might be required. Unity Memory Profiler v2 Use the Memory Profiler (package) to identify potential areas in your Unity project (and the Unity Editor itself) where you can reduce memory usage. For example, use the Memory Profiler to capture snapshot (represents the whole memory usage ofthe project), inspect, and compare 2 memory snapshots. The Memory Profiler is unified solution allowing you to profile both small projects on mobile devices and big AAA projects on high-end machines. The Memory Profiler is a window in Unity Editor with an overview of native and managed memory allocations and can help you detect memory leaksand fragmentation with some different tool: Tree Map;Memory Map (Sequential Visual Representation of Memory Blocks);Tables (Lists of All Managed Objects with Filtering, Difference between 2 snapshots);Overview or Connectome (Visual Graph of Connected Objects). Getting Started with Memory Profiler Use Unity 2018.3 or higher: Open yourproject in Unity and go to “Window Package Manager”.If you don’t see “Memory Profile” in package list then open the “Advanced” drop-down and make sure “Show Preview Packages” is set.Select the “Memory Profiler” & click “Install”.Open “Window Analysis Memory Profiler”. To use it, a build of the profiled project is needed becauseseparate build has more accurate data in comparison with running project in Unity Editor. Profiler must be connected to the build when the build is running and Memory Profiler is open in Unity Editor. Once the window has been opened, the “Capture Player” button can be pressed. Wait for Problem Moment of Game and Capture Second Snapshot.Once the snapshots have been captured, they show up on the Memory Profiler and the profiled build is no longer needed. Next you can investigate each snapshot with different tools and compare them with “Diff” button. Tree Map Tree Map — Visual Representation of Memory like Programs for Space Analyzing of Hard Disk. Different colored squaresindicate memory taken by different object types, for example textures or meshes. By clicking a square, it is divided further into smaller squares representing individual objects. More information on these individual objects can be seen at the bottom of window by clicking them, and even a reference on what is using the object can be seen. Just bylooking at the memory usage snapshot split up by object type tells which objects use the most memory. Tracking down the individual big spenders and working on those can quickly bring down the memory used by the project. Unity Physics Debugger Physics Debugger — tool for quickly inspecting the Collider geometry in your Scene, and profilecommon physics-based scenarios. It provides a visualization of which Game Objects should and should not collide with each other. This is particularly useful when there are many Colliders in your Scene, or if the Render and Collision Meshes are out of sync. You can use Physics Debug to profile and troubleshoot physics activity in your game. You cancustomize which types of Colliders or Rigidbody components you can see in the visualizer, to help you find the source of activity. Show active Rigidbody components only To see only the Rigidbody components that are active and therefore using CPU/GPU resources, tick “Hide Static Colliders” and “Hide Sleeping Bodies”. Show Active Objects —Physics Debugger Show non-convex Mesh Colliders only Non-convex (triangle-based) Mesh Colliders tend to generate the most contacts (comparing to colliders of simple forms) when their attached Rigidbody components are very near a collision with another Rigidbody or Collider. To visualize only the non-convex Mesh Colliders, set the window toShow Selected Items mode, click the “Select None” button, then tick the “Show Mesh Colliders (concave)” checkbox. Unity UIElements Debugger UIElements Debugger — tool for debugging of Unity Editor Extensions that was written with UIElements, a new retained-mode GUI system for extending the Editor in Unity 2019 and higher. It is useful forAsset Store Publishers who develop Editor Tools like “Bolt“. When you are working with UIElements, UIElements Debugger helps you visualize the hierarchy tree, properties and USS (Unity Style Sheets) files applied to each element. Benchmarking Benchmarking refers to collecting data from a project to measure its performance. This is differentfrom profiling in that the benchmarking data should be as close to the performance of the final product as possible. Because of this the data should not be collected by using the Unity Profiling tools, since just running the tool has an impact on the performance. Benchmarks should be taken on the same device as the final product is meant to run on. Asingle device can be good enough, but for getting many data, multiple devices of different performance capabilities should be benchmarked on. This way gives more information of the performance on low- mid- and high-tier devices. When benchmarking the frame rate of the project, a separate script can be used to track the frame rate instead of usingthe profiling tool. Read Also Complete Article about Benchmarking on Unity Blogs. Auto-Benchmarking System To keep the frame rate data accurate between benchmarks, the gameplay should be reproduced as accurately as possible. The best way to handle this is to have a script that plays the game the same way every time. When the gameplay isidentical between benchmarks, they can be compared to each other and Unity Optimization Process can easily be monitored. You can create a script having a single “Hub” Scene from which the other scenes were launched. The script would start the project on the lowest quality setting in the “Hub” Scene. From this scene it would open and playthrough the gameplay scenes, always returning to “Hub” Scene after a scene was completed. After finishing all the gameplay scenes, the quality level would be increased and playing through the scenes started again. The automatic system would also collect performance information while running. This information was then stored on text files createdby the script. These files were saved on the hard drive of the device and organized by scene and quality level. You can run script on two or more devices over the night and fresh data could be gathered on the next morning. Unity Assets for Benchmarking Advanced FPS Counter — Simple and flexible in-game frames per second counter with advancedfeatures, made to let you monitor stats of your game right on the target device. Show Advanced Stats with 3 tools: FPS Counter;Memory Counter;Device Information Counter. Also, take a look on These Awesome Tools: Unity Optimization: Tips & Tricks Every Unity Project is unique and has its own unique issues. However, there is a many of wellknown performance related problems associated with Unity Engine itself. This chapter goes through several of the known performance issues in Unity and provides information on avoiding them. The article is updating regularly. Please, share your experiences in the comments to extend this manual. Code & Scripting Events You need to use C#Events instead of UnityEvents. Yes, C# Events are not as convenient to use as Unity Events because Unity Events can be shown in Unity Editor at least, but Unity Events have bad performance in most cases. You can read research about comparing C# & Unity Events. Moreover, assigning methods to events with Unity Editor is a bad practice in termsof code because debugging a project by another person in this case becomes hard. Strings Concatenation Don’t combine 2 strings in Update () Function because every time it creates garbage by memory allocation. Example with UI: scoreText.text "Score: " currentScore.ToString(); Instead of it you need to create 2 separate text objects (TextMeshPro, of course): For updating the text with current score.For caching “Score: ” text. Unity UI (UGUI) The most common optimization issue with new Unity projects is the UI system. Canvas The basic element of the Unity UI is a canvas. These canvases then house all other elements of the UI, be it text or images. The issue with these canvases is thatwhen a single UI element on a canvas changes, the whole canvas is marked dirty and needs to be redrawn. Often with developers who haven’t had much experience with Unity, all the UI in the game is set up under a single canvas. This can mean that a complex menu system, even while hidden, needs to be redrawn every frame because it shares thecanvas with a timer, for example. Multiple canvases is solution to reduce CPU Usage. Optimize Unity UI with common method by splitting UI elements to separate canvases by update frequency. Elements that are updated regularly, like Timers or Mana Orbs, under a single canvas and static elements under another one. This fixes the issue of havingto update every single UI element in every frame. UI is a bottleneck especially on mobile devices. Transparent graphics are not handled well by mobile graphics cards, and all the UI elements in Unity are considered transparent. For this reason, splitting elements between multiple canvases can have a huge effect on the performance of the project.Unity Text vs TextMesh Pro Unity Text Component (UI) & Unity 3D Text (Text Mesh component) have bad performance.Optimize Text with TextMesh Pro instead of default Unity Texts. Getting Started with TextMesh Pro Use Unity 2018.3 or higher: Open your project in Unity and go to “Window Package Manager”.Select the “TextMesh Pro” & click“Install”.Right Click on “Hierarchy View”: “3D Object TextMeshPro — Text” for texts in 3D space. “UI TextMeshPro — Text” for HUD Texts with Canvases. Read Next: Making the most of TextMesh Pro. Static Text vs. Texture Your asset can have a texture that must contain static text (e.g., Road Signs). Yes, you can use TextMesh Pro to create atext over the texture, but this can be a bad idea, since UI elements that move and resize in 3D space as the player moves cause performance load. In this case signs can be replaced with transparent textures so Texture of Road Sign must contain image & text now. Memory Management Memory Management is done automatically in Unity. Whendeallocating the memory, Unity Engine often causes a huge frame rate drops. These drops are especially noticeable in fast reaction games, where stable frame rate is important. Many Unity Functions generate a garbage, making it almost impossible to fix the garbage collection spikes, without changing the source code of the engine which is availablefor purchasing exclusively to Pro and Enterprise customers of Unity Engine. Following are listed several methods of possible fixing of garbage collection issues. Garbage Collector (GC) All memory deallocation in Unity is done by Garbage Collector. When enough memory has been allocated, the garbage collector automatically clears unused objectsfrom the memory for a single frame. This is referred to as running the garbage collector, and always causes a frame rate drop. Unity 2019.1 with .NET 4x Equivalent introduces to Incremental Garbage Collection which splits GC operation over number of frames. So just turn it on in “Player Settings Other Use Incremental GC”. Learn More aboutIncremental Garbage Collection. Performance Tools: Advanced Render System;Super Level Optimizer;Advanced Culling System. Courses for completely understanding Unity Workflow. There are multiple ways to reduce the performance impact and the frequency of running the garbage collector. The simplest is to generate less garbage duringruntime. When profiling the CPU, the amount of garbage generated by each function can be seen. Then you can change these functions to generate less garbage. Manual Garbage Collection One way to solve the issue is to manually run the garbage collection when the gameplay is less active, and the frame rate drop won’t affect the user experience.For example, during loading screens or during more peaceful (passive) periods of gameplay. System.GC.Collect(); This still caused a noticeable frame rate drop, but at a time when it did not negatively affect the gameplay. I don’t recommend to use Manual Garbage Collection because is a last measure — in 95% of cases you will success without it.First of all, you should deal with all the bottlenecks of your project. Caching Caching is a technique of reusing object. Caching refers to storing something that is often needed as a variable instead of calculating or fetching it whenever it is needed. For example, this can mean storing the camera as a variable in a script instead of calling “Camera.Main”whenever the camera is needed. Unity APIs such as “Camera.main” or “gameObject.transform” causes a little overhead. Camera.Main is a shortcut for finding a camera object from the scene with the tag Main. This value is never stored so every time the function is called, a search operation is performed. All this overhead can be fixed by storing areference to the object as a variable after the first call of the function. For example, no need of creating the same local variable every frame in Update () Function: void Update() { string s "Text"; text.text s; } Instead of it you can assign the variable when declare a variable as a field of class, and initialize it here or in Awake () or Start () functionsif it makes sense in your case. Object Pooling Object Pooling is also Caching, but in this case we are dealing with several objects of the same type (e.g. with the same script components) or same prefabs. Read more about Object Pool. Texture Settings Resolution You can reduce the

Unity android optimization. Are you planning to optimize your game made with Unity and get the best performance? In this Complete Unity Optimization Guide, I will show you how to increase performance in Unity correctly, so your game will run fast & stable. Here I will consider optimization tools provided by Unity Game Engine and also by Indie .