Tutorial by Examples

Avoid unnecessary operations and method calls wherever you can, especially in a method which is called many times a second, like Update. Distance/Range Checks Use sqrMagnitude instead of magnitude when comparing distances. This avoids unnecessary sqrt operations. Note that when using sqrMagnitude,...
Usage If you have a long running operation that relies on the not-thread-safe Unity API, use Coroutines to split it over multiple frames and keep your application responsive. Coroutines also help performing expensive actions every nth frame instead of running that action each frame. Splitting Lon...
One might argue that there are greater resource hogs in Unity than the humble string, but it is one of the easier aspects to fix early on. String operations build garbage Most string operations build tiny amounts of garbage, but if those operations are called several times over the course of a s...
Cache references to avoid the expensive calls especially in the update function. This can be done by caching these references on start if available or when available and checking for null/bool flat to avoid getting the reference again. Examples: Cache component references change void Update() {...
Avoid calling methods using strings that can accept methods. This approach will make use of reflection that can slow down your game especially when used in the update function. Examples: //Avoid StartCoroutine with method name this.StartCoroutine("SampleCoroutine"); //Ins...
Avoid empty unity methods. Apart from being bad programming style, there is a very small overhead involved in runtime scripting. Over many instances, this can build up and affect performance. void Update { } void FixedUpdate { }

Page 1 of 1