Skip to content

Latest commit

 

History

History
229 lines (108 loc) · 10.2 KB

File metadata and controls

229 lines (108 loc) · 10.2 KB

Unity Performance

See the sample project here: https://github.com/omundy/dig250-unity-performance

"Profiling should not be an afterthought or something left until the final stages of development. Instead, integrate profiling into your development process from the very beginning." —Eugene Martynenko1

Frame Per Second (FPS)

  • The most important metric of a performant game is a high Frames Per Second (FPS) rate, accessed in the Stats window (Game View).
  • To increase your game's performance, simply lessen the work of the CPU, GPU, memory, and network.
  • A frame rate of 60 FPS is optimal. Anything less than 30 FPS will cause noticeable stutter.

Reduce, Reuse, and Recycle

Many performance gains can come from reducing and reusing CPU or memory.

  1. Always Cache References - When you need a reference to a component, property, camera, or script, do so in Awake() or Start() and store the reference in a variable accessible by the entire class. This improves performance because your script won't have to look up the same reference over and over in Update(). 2
Rigidbody rb; // do this for components...
Vector3 distanceToEnemy; // variables...
Camera cam; // the main camera...
Transform myTransform; // and the transform
private void Awake() {
  rb = GetComponent<Rigidbody>();
  cam = Camera.main;
  myTransform = transform;
}
void Update() {
  distanceToEnemy = myTransform.position - enemyPosition;
}

Garbage Collection

  • When you remove (Destroy) objects from your game their data is no longer needed.
  • The memory set aside to store that data, "Garbage", must be freed before it can be reused.
  • Garbage Collection is the process that makes previously used memory available again.
  • It can cause big performance problems (a frame rate stutter) if not properly handled.

Object Pooling

  • A pattern to decrease garbage collection (and stutter) created by adding and removing objects from memory.
  • Start by pre-instantiating objects you need at any specific moment before the scene starts.
  • Place the objects off-camera in a "pool" so they are in memory, but the CPU/GPU doesn't have to draw them.
  • When you need an object, pull it from the pool. Once finished with it, don't destroy it (which causes garbage collection), rather put it back in the pool to reuse later.
  • See more about Object Pooling in Unity

Strings increase garbage

In Unity, retrieving strings from game Objects will create a duplicate of the string, which will need to be garbage collected. Some ways to save memory:

  • Don't use gameObject.tag == "string"; Using CompareTag(string) instead does not add to the heap resulting in a ~27% increase in performance. See: Unity 5 Game Optimization by Chris Dickinson and Unity forum
  • Don't concat strings in loops or Update(). See: Unity Manual: Understanding Automatic Memory Management

More on Garbage Collection

The Profiler

  • Regularly check the Profiler Window to identify obvious dips in performance.
  • Since changes in one "performance area" can affect others, test everything as you make each improvement.

profiler

Graphics Performance

Draw calls

  • A draw call is the command the CPU sends to your GPU to draw geometry on your screen
  • Effectively, the number of objects being drawn.
  • Keep this number down (<2000 desktop and <200 mobile) to maintain good performance.

How to lower your draw calls

profiler

More on Graphics Performance

Mobile Optimization

Networks, Bandwidth, and Handling Data

Optimizing Unity UI

If you have a lot of UI in your game start with this video Unite Europe 2017: Squeezing Unity: Tips for raising performance

More UI Tips

UI performance resources

Static vs Dynamic GameObjects

  • Marking an object as static (the check box at top right of the Inspector) "can save on runtime calculations, and potentially improve performance." (Unity), depending on things like the overall number and complexity of the objects involved, whether or not you use lighting in your game.
  • Keep in mind that this will increase the filesize of your build (and thus memory during play) "Using static batching requires additional memory for storing the combined geometry."

Bake Your Lighting

image

Depending on your game's requirements, baking your lights can increase performance a lot, especially on mobile. A lightmap is essentialy a texture that includes surface information like color (albedo) and relief (normals), as it is seen with direct and indirect static lights in the scene. The data baked into lightmaps cannot change at runtime. Real-time lights can be overlaid and used additively on top of a lightmapped scene, but cannot interactively change the lightmaps themselves.

More Performance Tips

Editor Performance

General best practices include:

  1. Restart Unity once a while
  2. Delete the contents of the project Library (resets package caches and others that Unity rebuilds). Quit Unity, delete contents, then start again.
  3. Use Assembly definitions

(Experimental) Editor Iteration Profiler

https://discussions.unity.com/t/introducing-the-editor-iteration-profiler/794996

Sources

Footnotes

  1. Martynenko, Eugene. “10 Tips for Optimizing Performance in Unity Games.” (2024)

  2. Fahir Mehovic. “Optimize Your Games In Unity – The Ultimate Guide.” (2021)