San Mateo 2023 Flashcards

1
Q

Animators: UI

A

Something not mentioned on this slide,is the use of animators for UI. The animator system is particularly inefficient when used with Unity UI. Animators animating a property of a UI element will force it to rebuild the Canvas and Layout every frame, and cause unnecessary batching work.
So we recommend avoiding animators with UI.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Physics Simulations: What Are They

A

Physics ensure that the objects correctly accelerate and respond to collisions, gravity, and various other forces. Physics can apply to your 3D, 2D, object-oriented, or data-oriented games.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Physics Simulations: Performance Cost

A

Physics calculations can be quite expensive, especially on mobile devices. The total amount of physics calculation depends on the number of non-sleeping rigid bodies and colliders in the scene
and the complexity of the colliders. Even if these aren’t present in your scene, depending on the settings of your game Physics can still have CPU overhead, due to to how frequently it’s being simulated, despite not being used. There are ways to reduce this performance cost, or eliminate it altogether if Physics aren’t necessary for your project.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Physics Simulations: Disable or Reduce Collisions

A

There are a few possible areas where you can disable or reduce collisions. For instance avoid using the collision module for your particle systems unless you need it. Additionally, Mesh Colliders are generally expensive, so consider substituting more complex Mesh Colliders with primitive or simplified ones to approximate the original shape.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Physics Simulations: Change FixedUpdate to Update

A

If you do need physics in your game, but your logic doesn’t strongly depend on deterministic physics calculations, we suggest moving fixedupdate logic to Update. That is because this has the frequency of the physics system, and is called every fixed frame rate frame. Like I had mentioned, this is by default set at .02 in your physics settings, so could potentially be called twice per frame.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Physics Simulations: Check Project Settings

A

There are a few changes you can make in your project settings to reduce the impact of physics on your performance. The one mentioned broadly in the project reviews was disabling Auto Simulation. Auto Simulation has CPU overhead due to housekeeping operations of the physics simulations, and will automatically run the physics simulations. If you don’t need Physics simulations running constantly, you can choose to disable this, but enable it manually to give you explicit control over when physics runs.
You can also disable Auto Sync Transformations in your project settings to avoid excess synchronization that occurs before every raycast operation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Physics Simulations: Adjust Fixed Timestep

A

The physics engines work by running on a Fixed Timestep, which defines the time delta used by each Physics Step. The default value is at 0.02, which equates to 50 frames per second. If you were to leave your physics Timestep at this default value with a target frame rate of 30 fps, it would be called twice per frame. If you need physics in your game, you can increase the Fixed Timestep so Physics aren’t as heavy on the CPU. This can be done via Time Manager or during runtime using the Time.fixedDeltaTime property.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Managed Allocations and Garbage Collection: What is it/How does it work

A

Automatic memory management allows you to write code quickly and easily, and with few errors without needing to manually request the release of memory. The garbage collector manages a section of memory called the Managed Heap, and will periodically release and allocate memory. However, this convenience does come with memory and performance problems if you’re not careful;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Managed Allocations and Garbage Collection: Performance Cost

A
  • this memory management process impacts runtime performance, because allocating managed memory is time-consuming for the CPU, and could result in stuttering because the garbage collector is unpredictable with when it releases and allocated memory.
  • Garbage collection might also stop the CPU from doing other work until it completes.
  • Also, when the garbage collector releases an object, the memory it occupied is freed up. However this free space doesn’t become a part of a single pool of free memory, and instead the memory becomes fragmented.
  • This ultimately leads to the heap expanding, because the managed heap can’t find or free up a single block of contiguous memory to assign new allocations.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Managed Allocations and Garbage Collection: Avoid Temporary Allocations

A

It’s common for an application to allocate temp data to the managed heap in each frame, but this does affect the performance as I mentioned before. If a program allocates one kilobyte (1KB) of temporary memory each frame, and it runs at 60 frames per second, then it must allocate 60 kilobytes of temporary memory per second. Over the course of a minute, this adds up to 3.6 megabytes of memory available to the garbage collector. So try to get to as close to 0 bytes allocated per frame as possible. As an example of an issue with managed allocations, in one game your team was seeing around 32.8 Kilobytes allocated every frame. Per frame allocations can quickly add up so this is particularly important, and it can cause runtime hitches caused by Garbage Collection.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Managed Allocations and Garbage Collection: Use Project Auditor

A

Most of the code paths that cause managed memory allocation can be easily spotted with the help of the Project Auditor. In particular, the code analysis section of the tool that uses Roslyn Analyzers helps to quickly identify the location of managed memory allocations. We recommend that teams try the tool and strive for having code without allocations during gameplay.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Managed Allocations and Garbage Collection: Avoid Debug Logging

A

Debug logging is great for diagnosing issues, but it does create strings, which are subject to garbage collection. You should aim to remove logging from your final builds. If left in the final build, these calls will still try to write to some console, and the string will still be allocated. You can create a wrapper to make a custom logger, which can enable or disable logging in your build, but will still create string garbage from where the log function was called from.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Resources Folder: What is it

A

This is the system that allows developers to store Assets within one or more folders named Resources and to load or unload Objects from those Assets at runtime using the Resources API.

The Resources folder may be useful in some trivial cases, if the content is:
- Generally required throughout the project’s lifetime
- Not memory intensive
- Not prone to patching, or does not vary across platforms or devices

The resources folder can be very useful for fast prototyping, but for full production it presents many problems and we don’t recommend using it for several reasons

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Resources Folder: Performance Issues

A

It can bloat the size of a project’s build, lead to uncontrollable excessive memory utilization, and significantly increase application start up times. Every asset within Resources folders, and every asset it references, are included in Resources system data. Because of this, the time required to initialize the Resources system increases with the number of files within Resources folders.

Another common issue with Resources folders is that everything inside a Resources folder will be included in the build, even debug and placeholder assets. Unity can strip unused assets in other folders to remove them from builds, but it has no way of knowing which assets might be loaded from Resources at runtime, so it does not discriminate.

Asset management because very difficult as the number of assets in those folders increase.

The Resources system makes it hard to deliver custom content to specific platforms and eliminates the possibility of incremental content upgrades.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Resources Folder: Switch to Addressables

A

Using Addressables can improve runtime memory by introducing weak references to prevent assets from being loaded. Weak references mean that you have control over when the references asset is loaded into and out of memory, and the addressable system will find all the necessary dependencies and load them too. Addressables can be daunting to set up, but it’s worth it. We recently put out a blog about addressables best practices, and have a specific guide about setting up addressables for a mobile game with frequent content updates that might come in handy.
We do recommend completely switching to addressables, because if you use asset bundles and resources together in tandem, that can lead to duplication of assets, leading to further memory bloat.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Resources Folder: Use Project Auditor

A

I’m suggesting project auditor again, because in this case, project auditor’s Asset Diagnostics view can provide an overview of all the assets contained in Resources and their dependencies. This will hopefully help asset management as the resources folder grows.

17
Q

Resources Folder: Use Build Report Inspector

A

Build Report Inspector is the tool I mentioned that lets you access information about your last build. However the important feature here is the ability to analyze which assets are included in the build. This can help you catch any assets still being included in the Resources folder.

18
Q

Animators: What Is It

A

I’m sure you’re all familiar, but I’ll just briefly introduce the animation system in Unity for context to this problem. Unity’s animation system is based on Animation Clips which contain the info on how characters or objects move. Those animation clips are then organized into a flowchart system called the animator controller, and this is responsible for blending or switching between given animations based on what’s happening in the game. These animator controllers are attached to animator components.

19
Q

Animators: Performance Cost

A

The Mecanim animation system does have a performance cost, because it is a more complex system than the legacy animation system. Mecanim has temporary buffers it uses for blending, and there is additional copying of the sampled curve and other data. The Mecanim layout is optimized for animation blending and more complex setups. Additionally, Mecanim was designed to handle complex animations with work split across multiple threads, in the case of simple animations overhead of scheduling jobs results in worse performance.

20
Q

Animators: Use Legacy Animations for Simple Animations

A

Using the Mecanim animation system puts an additional strain on the CPU. Mecanim was designed to handle complex animations with work split across multiple threads, in the case of simple animations overhead of scheduling jobs results in worse performance. In comparison, the Legacy Animation component runs on the main thread and is much better suited for simple animations, because it does not have this overhead and doesn’t use a state machine.
Something that can be classified as a “Simple Animation” is one with fewer than 400 curves. The attached graph on this slide demonstrates the performance comparison of using legacy animations versus the newer animation system based on the amount of curves within the animation. The recommendation is if you’re using legacy animation, is to do simple tweening instead.

21
Q

Animators: Configure Import Settings to Disable Unused Features

A

Occasionally, it’s possible to import in empty animation clips with assets, or in some cases instantiate objects with unused Animator Controllers and attached avatars. When this situation happens, although these assets are seemingly not being used, this could take a significant amount of memory. We recommend turning off the generation of an Avatar by setting the Animation Type to None in Rig tab. You should also disable importing empty animation clips that are exported with models by disabling Import Animation in the Animation tab. This will ensure that you don’t have unused assets taking up memory.

22
Q

Animators: Cull Animators

A

The CPU frame time of animator components are divided among three steps: preparing animation frames, processing frames when the animation begins, and processing frames when the animation ends. CPU time of the first step depends on the number of enabled Animator components in the active scene. CPU time of the second and third steps depends on the number of enabled Animator components and the culling mode of those components. Based on this, it’s very important to cull animators when the object no longer needs to be animating or when it’s out of view. We recommend exploring different animator culling modes that will cull them when the renderer is not visible. The culling mode “Cull Completely”, where the animation will be completely disabled when not visible. You’ll want to make sure you’re disabling the animator component, as disabling the game object won’t have the same effect.

23
Q

Animators: Use Shader Animations

A

In many cases we’ve seen in the Game the simplest animations can be offloaded to the GPU by animating them in a shader. For example, making a flag wave in a shader will allow you to remove the skin weights from the mesh, bones, and animator component entirely.

24
Q

Texture Memory: Performance Cost

A

Textures are often the most memory intensive assets in a project, due to resolutions, accidental duplication, or using the wrong compression format for your chosen platforms. There are a lot of checkboxes that can easily go overlooked when importing your textures in that cause memory to balloon, but we have great tools for diagnosing these issues and there are solutions for bringing down your texture memory.

25
Q

Texture Memory: Load Only the Textures you need

A

The assets you load into a scene can vary to some degree, and it may be the case that it’s unecessary to load in particular assets if they’re unused. For instance, if you load in an asset that has different customizable variations, it’s only necessary to load in the one’s that are being used. As a game’s content grows, you should unload resources from memory when they’re not needed. This doesn’t actually just apply to textures, but is a good thing to remember for all assets in a scene.

26
Q

Texture Memory: Use the Right Compression Format

A

Make sure all your textures are compressed. This can can result in faster load times, a smaller memory footprint, and improved GPU rendering performance. Compressed textures only use a fraction of the memory bandwidth needed for uncompressed textures. We have documentation online about which compression format you should use for each platform. A common suggestion in these project reviews was to use ASTC for iOS. ASTC results in better quality than other options, and allows for more control over compression quality, may improve power usage, and is supported by all modern devices on iOS and Android. The UnityDataTool, which I mentioned earlier, can be used to sort by compression format and catch any uncompressed textures taking up unnecessary space and memory.

27
Q

Texture Memory: Check Import Settings

A

An easy thing to forget about it checking the import settings of your textures. You should disabled Read/Write on textures, because this setting causes an extra copy of the texture to be included in CPU memory. This should only be enabled if the game needs to read the texture data back on the CPU side. The other important setting is Generate MipMaps. MipMaps allow for faster rendering and reduction of artifacts when rendering textures at lower that their full resolutions, but they come with a 33% memory overhead. It’s also unnecessary to enable mipmaps for UI textures. You can use AssetPreprocessor API to automatically apply proper import settings to all the assets in the project.

28
Q

Texture Memory: Reduce Texture Resolution

A

This is probably the simplest suggestion, but if you can get away with reduced visual fidelity, or can’t tell the difference between resolutions, Reducing your texture resolutions is a guaranteed way to reduce the game’s memory footprint. It’s best to test your different resolutions to see what you can get away with. This is especially important with lower end devices, where they’ll likely to have lower screen resolutions and will need those memory gains.

29
Q

Tools Used: Project Auditor

A

Project Auditor was developed by our accelerate solutions team, and they use it in nearly every project review to diagnose issues. The Unity Project Auditor is a static analysis tool that can inspect various aspects of a Unity project. It can help to track down the causes of managed memory allocations, inefficient settings, and possible performance bottlenecks. The tool will generally provide a high-level written description of why it has flagged certain features as potentially problematic.
Some of the reported issues may turn out to be false positives, but they should still provide valuable points for the team to investigate. In particular, the code analysis section of the tool that uses Roslyn Analyzers helps to quickly identify the location of managed memory allocations, costly API calls, and other possible issues in code. The tool is also useful in finding out which shader variants are included in the build, and which of them are compiled, including exact shader keywords and features.

30
Q

Tools Used: Build Report Inspector

A

This tool lets you access information about your last build, and helps you profile the time spent building your project and the builds disk size footprint. It’ll also let you see all the assets included in your build. This information may help you improving your build times and build sizes.
This script allows you to inspect this information graphically in the Editor UI, making it more easily accessible than the script APIs would.

31
Q

Tools Used: UnityDataTools

A

This is a set of command line tools, where the main purpose is to analyze the content of Unity data files. It can also be used to analyze the content of the asset bundles included in the built project.
This tool was developed by a member of the Accelerate Solutions team and outputs a database that can be opened to review large assets, duplicates, shader variants, and read-write properties on Textures and Meshes. It was specifically mentioned in these project reviews for catching any textures that are uncompressed.

32
Q

Texture Memory: Memory Profiler

A

A great tool to help diagnose your texture memory issues is the new memory profiler package, which is out of experimental as of the 2022.2 Tech Stream. There’s a great blog post we put out about it which goes into using it for texture memory issues which I recommend taking a look at. You don’t actually need to upgrade your project to 2022.2 to use it. Just create a blank 2022.2 project, and load in the capture taken in your target editor version. The only drawback to this method is that you will not be able to view relevant Assets or find them in the editor.

33
Q

Managed Memory: JSON Serialization/Deserialization

A

Serializing and deserializing JSON data is expensive and happens often during gameplay, as sections are created, destroyed, updated, and repopulated while as players move through menus and gameplay. JSON parsing is slow and it also produces a lot of managed heap allocations. This affects memory fragmentation and triggers Garbage Collection on heap expansion. We recommend that the team evaluate alternatives to JSON for data serialization, as JSON libraries often have detrimental performance impacts. If you do keep JSON serialization, you can consider pushing back the serialization so that the CPU spoke is hidden behind a loading screen or UI of somesort.