Optimize game unity




















Once Unity Profiler has 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.

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 a slice with the mouse.

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 caused the spike and optimizing it is a way of making the frame rate and gameplay experience smoother.

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:. You can also use Timeline Mode of Profiled Data. You have in quick glance the whole situation of a frame in order of occurrence for all threads. So events showed at the left 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. 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 of activity for each marker, including:. Introducing the Profile Analyzer. 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 need thousands of Draw Calls to render the screen each frame. By using the tool, more insight on the rendering process can be got. It also is used to find the areas where the most Draw Calls are spent, and where optimization is required. 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 organized into 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 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 single object or a character in the scene, work on that object might be required.

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 of the 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 leaks and fragmentation with some different tool:.

To use it, a build of the profiled project is needed because separate 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 snapshots have been captured, they show up on the Memory Profiler and the profiled build is no longer needed. Different colored squares indicate 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 by looking 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.

Physics Debugger — tool for quickly inspecting the Collider geometry in your Scene, and profile common 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 customize which types of Colliders or Rigidbody components you can see in the visualizer, to help you find the source of activity. 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.

Benchmarking refers to collecting data from a project to measure its performance. This is different from 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. A single 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 using the profiling tool. 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 is identical between benchmarks, they can be compared to each other and Unity Optimization Process can easily be monitored.

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 created by 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.

Advanced FPS Counter — Simple and flexible in-game frames per second counter with advanced features, made to let you monitor stats of your game right on the target device. Every Unity Project is unique and has its own unique issues. However, there is a many of well-known 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. You need to use C Events instead of UnityEvents.

Moreover, assigning methods to events with Unity Editor is a bad practice in terms of code because debugging a project by another person in this case becomes hard. Instead of it you need to create 2 separate text objects TextMesh Pro , of course :. 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 that when a single UI element on a canvas changes, the whole canvas is marked dirty and needs to be redrawn. This can mean that a complex menu system, even while hidden, needs to be redrawn every frame because it shares the canvas with a timer, for example. 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.

In the final part of this interactive series, participants share their completed game levels — built with t Readymade economy systems can really speed up your game development. In this demo, learn how to use Game Fo Learn how Booz Allen leverages Unity Reflect and digital twins to create a holistic planning process and identify long-term cost savings techniques. Learn how to get precise performance metrics when your project runs on the target device. In this session we use Unity Skip time-consuming data collection by training your AI for robotics entirely in Unity.

Learn about how domain randomization is shaping the future of robotics. Learn how to use AR Foundation to quickly develop or port your AR experience to the Magic Leap 1 headset, while still being able to incorporate Magic Leap-specific features into your apps. Join Unity Insiders and guests for a live deep-dive discussion on creating ray traced scenes in Unity.

The Global Meetup connects developers from every corner of the world. Learn the ins and outs of Furioos, a new cloud-based service that lets you stream fully interactive 3D experiences from Unity — with peak visual quality — in any web browser.

In this deep dive into their development process, The Chinese Room shares lessons learned creating their first Apple Arcade title, Little Orpheus, and how they empowered their artists and designers. In this session, learn how the developers of Neonverse created a highly performant mobile game with beautiful visuals by combining vertex lighting with new techniques, animating in Shaders, and more.

As for the InvokeRepeating function, it is not memory heavy as a coroutine, but it has a slightly lower cost than a coroutine. You can even optimize this further by checking the time condition outside the CountTimer function:. This is because Update is called every frame, if you have 60 frames in one second the CountTimer function will be called 60 times, this way we limit the calling of the CountTimer function only once every second.

One of the things I love to do in mobile games that I created is creating prefabs out of the levels. LoadScene sceneIndex ;. I will simply use Instantiate to create the new level. Instantiate levelPrefab ;. Load Scenes Asynchronously. If your levels are huge and creating prefabs out of them will result in a heavy operation every time you need to delete a level and create a new one then you will use scenes for your level.

LoadScene "Scene Name" ;. LoadSceneAsync "Scene Name" ;. LoadSceneAsync sceneIndex ; while! Of course, instead of using Debug. Log you will display the loading percentage with a UI text on the screen. Use Arrays Over Lists. Use For Loop Over Foreach. Speaking of arrays, if you want to process them, always use for loop over foreach.

I know that foreach loop looks fancy and it is so pleasing to the eye, but with this comes the lack of performance. The reason for that is because foreach loop uses enumerators to iterate a given list, and this is more complex than just iterating through elements of the array using a for loop.

Be Careful With GameObject. Find Functions. NET Framework. The main difference between the two is in the compilation of the code. I will not dive deeper into more differences between the two as that is something that will not affect our development, but what is important to know is that you should always select IL2CPP as the Scripting Backend:. The reason for that is because IL2CPP is a scripting backend developed by Unity, as I already mentioned, and over time Unity will improve that scripting backend to be better and better and eventually ditch the Mono Scripting Backend.

The reason for this is that when you mark a game object as static, Unity will view that game object as static. Because in terms of Physics, there are dynamic and static game objects. Attaching a collider to a game object will make Unity count that game object as a physics object and it will take it into physics based calculations, for example if another game object collides with it.

Because of that, if the game object is not supposed to move, like a fence, or a stone, or a door and so on, marking it as a static object will make it take less resources and thus it will be less heavy on the overall performance. Another very important thing in regards to static colliders is how the physics engine handles them.

When the game starts, the physics engine will generate the data for all game objects that are marked as static, now if new static objects are created during gameplay, it will cause the physics engine to regenerate the data for all static objects.

Collision Detection Settings. The default Collision Detection settings for every Rigidbody is set to Discrete:. But if you click on the drop down list for the Collision Detection settings you will see that we have more options:. So what is the difference between them? The Discrete setting moves the physics object a small distance every timestep based on their velocity, then it performs a bounding volume check for any overlaps and if it finds any it will treat them as collisions and it will resolve them based on how the objects overlap.

The problem with this method is that if an object is moving too fast, then it will pass through the collider, and this is not something that we want to see in our game.

The solution for this problem is to use Continuous collision. This shape is then used to check for any collisions between frames. This reduces the risk of of missed collisions but the price for that is a significantly higher CPU overhead compare to Discrete collision setting. In essence the only time you will need to use this option is when you have fast moving objects, but I am mentioning this because when we run into a problem we all search on google for solutions and usually we go with the first answer that works for our project not worrying about will that affect the performance of our game.

Reuse Collision Callbacks. When this option is true, only a single instance of the Collision type is created and reused for each individual callback. This reduces waste for the garbage collector to handle and improves performance. You can read more about this feature here.

When a Transform component changes, any Rigidbody or Collider on that Transform or its children may need to be repositioned, rotated or scaled depending on the change to the Transform.

You can control if the changes made to the Transform are automatically applied to the correct components by setting this property true. When set to false, synchronization only occurs prior to the physics simulation step during the Fixed Update. You can also manually synchronize transform changes using Physics. Collision Layers. Not all objects in your game need to collide with each other, and this is a common thing in game development.

You can use layers to determine which game objects can collide with each other. To do that, you can either click on the Layer drop down list in the Inspector tab and then click Add Layer:. Or above the Inspector tab you can click on the Layers drop down list and click Edit Layers:.

From there click on the Layers drop down list and in the User Layer fields define your layers:. Depending on the scope of your game, this can save you a lot of unnecessary calculations in regards to the physics system because if two layers are not supposed to collide with each other, Unity will ignore collisions between game objects who are set on that layer.

Be Careful With Collider Types. As you are aware there are different types of 2D and 3D colliders:. We can use dynamic and static batching to reduce the number of draw calls, but before we do that, it is important to know what draw calls and what batches are.

A draw call represents the number of calls to the graphics API to draw objects on the screen, while a batch is a group of draw calls to be drawn together. Batching object together minimizes the state changes needed to draw each object inside the batch.

Doing this leads to improved performance by reducing the CPU cost of rendering objects. There are two ways how Unity groups objects in batches to be drawn: Dynamic Batching and Static Batching. It is important to note that only objects that share properties like textures or materials can be batched together.

Here I have a scene with 4 different game objects: cube, sphere, capsule and a cylinder:. I have created a Test Material and attached it on all 4 shapes:.

When we run the game now, we will see that it takes only 9 batches to render the scene:. If we run the game, we will see that it only takes 9 batches to render the scene:.

Whereas if you turned off GPU Instancing it would take 33 batches to render the same scene. Another Unity feature that can eat up performance is lighting. This is something that a lot of beginners as well as intermediate Unity developers are not aware. I am going to reuse the batching example where I have 5 simple cubes in the scene. I have turned off batching and GPU instancing and this is the current number of batches it takes to render the scene:.

Now open the Stats window and take a look at the batches number:. It is amazing how the batch number went from 33 to 7 just by changing this one setting on the Directional Light. This tells you that lights can hit the game optimization very hard, especially on mobile devices.

Another way how we can save up performance with lights is by using baking. Here I have a cemetery scene which contains this temple looking 3D model:. I am going to change the color of the Point Light and move it inside of this temple 3D model:.

In the Lighting tab, click on the Scene tab and then scroll all the way to the bottom and press the Generate Lighting Button:.

If we take a look at the Stats window now, we will see that we saved 14 batches with baking:. Even if we turn off the Point Light object, we will still see the light in the game:. This way we can exclude game objects from being affect by lights and thus save additional game performance. When it comes to the assets that you use for your game, I will first start with sprites as they are the most common assets used in both 2D and 3D games.

The majority of developers leave these settings on default not aware that this can affect the size of your game. And just to show you how these settings can affect your sprites, I am going to change the Format settings from Automatic to RGBA 32 bit:.

You will notice that our background is very blurry in the Game tab which shows you how these settings can affect how your sprites and your whole game will look like. Optimizing Audio Files. Audio optimization is one of the most ignored aspects in Unity game development.

Because most of the times people rely on Unity to take care of the audio optimization. And a lot of the times this works, but as soon as your projects gets a little larger and you add more and more files, you will see a huge spike in performance increase when all things start to add up together. Audio sources that are fully 3D are essentially playing audio in mono. This is because even if the audio file is stereo, the two audio channels still originate from the exact same point in world space, the same way as mono.

Because of that to save memory, it is better to check the Force To Mono checkbox for 3D audio sources. In regards to the Normal option below the Force To Mono checkbox, if you enable it, it will readjust the gain of the audio file so that the mono sound is the same volume as the original audio file. I have mentioned these settings together because they have direct impacts on each other.

Next we have:. In this situation, when the audio file is played for the fist time, it will being loading in the background and it will play when it finishes loading. The problem with this setting is if the file is large, it will cause a delay between triggering and playing, but this is only happens the first time the audio file is played, every next time you play it, it will play normally.

And finally we have:.



0コメント

  • 1000 / 1000