This is the sixth day of my participation in the August More text Challenge. For details, see:August is more challenging

Recommended reading

  • CSDN home page
  • GitHub open source address
  • Unity3D plug-in sharing
  • Jane’s address book
  • My personal blog
  • QQ group: 1040082875

One, foreword

If a game is stuck, it’s not fun. This article introduces some very simple performance improvements that every Unity developer should know about in order to keep players happy. No one expects you to make a game that looks like a AAA+ title, but it should have a lot of frames per second.

Note: When we talk about being in an FPS improvement environment, we always mean it takes time to compute (what’s driving our CPU crazy).

Algorithms and data structures

When it comes to game performance, the most important part is the developer’s understanding of efficient algorithms and data structures. This is too big a subject to discuss here. If you’re new to programming, try looking at the following topics to see when it’s best to use them:

  • The list of
  • List of links
  • HashMaps
  • The tree
  • Line up
  • The stack
  • Sort (Quicksort, Bubblesort,.)

Three, mathematics

In the interest of sanity, we will not discuss mathematics in this article. However, mathematics is important for performance. Imagine a monster in our scene. The player has now shot the monster – how can we find out if he hit it?

There are countless ways to do this, and they all involve math. Your job is to get to know them and decide which is the best way. Fortunately, federation already provides us with a lot of functionality. But if you can’t find the right way, the math will come.

Four, grid,

Let’s talk about something you can learn and apply without a lot of effort: optimizing the grid. When making games, we usually have a lot of 3D models in the scene. Each model consists of what is called a grid. A mesh is just a bunch of triangles. The more triangles we have, the less FPS we get, so it’s important to keep them as low as possible.

For example, a monster model can look really good with only 4000 triangles. There’s no need to use a million of them. Most 3D modeling programs already have grid optimization capabilities, depending on how you use them.

If there is no way to bypass a mesh with many triangles, there is another option: LOD(level of detail). The concept of LOD is simple: when the mesh is far away from the camera, it is modified so that it has fewer triangles. The player can’t tell the difference because it’s so far away from the camera. There is universal support for this feature, so I will try it one day.

Cache GetComponent

You usually use the GetComponent function to access the components of a GameObject. Computationally, this function is expensive.

Let’s look at an example of how not to use it:

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {

    // Use this for initialization
    void Start (){}// Update is called once per frame
    void Update () {
        Health h = GetComponent<Health>();
        h.increaseBy(100); }}Copy the code

This example is bad because GetComponent is called once per update (about 60 times per second). This can be easily improved as follows:

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {
    // Cache
    Health h;

    // Use this for initialization
    void Start () {
        h = GetComponent<Health>();
    }
        
    // Update is called once per frame
    void Update () {
        h.increaseBy(100); }}Copy the code

This is a much better example. It provides the same functionality, but with fewer computations, because the GetComponent function is called once to begin with, instead of 60 times per second.

6. Unit shortcut property

To make life easier, Unitity provides a way to access some standard components without using the GetComponent function, such as:

  • transform
  • The game object

Here’s how they work:

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {

    // Use this for initialization
    void Start (){}// Update is called once per frame
    void Update (){ Vector3 pos = transform.position; }}Copy the code

To understand this code is computationally heavy, you need to know a little bit about Unity. Because that’s what it really does:

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {

    // Use this for initialization
    void Start (){}// Update is called once per frame
    void Update (){ Vector3 pos = GetComponent<Transform>.position; }}Copy the code

Even though it doesn’t look like it, it still uses GetComponent every time, which, as mentioned above, is time-consuming to compute.

The right way is to do this:

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {
    // Cache
    Transform tr;

    // Use this for initialization
    void Start () {
        tr = transform;
    }
        
    // Update is called once per frame
    void Update (){ Vector3 pos = tr.position; }}Copy the code

Seven, GUIS

When using GUIS, the easiest approach is usually to use the GUILayout class instead of the GUI class, hierarchy. Unfortunately, the GUILayout class is much slower than the GUI class, so carefully consider whether it’s worth it.

Eight, Shaders,

Shaders can make graphics cards go completely crazy. Unless you’re a Shaders expert, it’s a good idea to only use those Shaders that Unity provides. There are also Unity Shaders with the “Mobile” suffix. These shaders are especially suitable for mobile phones, but they can also bring performance improvements on regular computers.

Note: Unity shaders are well made, but still expensive to compute. Use less!

Nine, baking

Let’s make some cookies. Unity has several baking characteristics. For example, when we want to have shadows in a game, the first thing that comes to mind is this: In every Draw Call:

  • 1. Position of the light
  • 2. Draw the scene
  • 3. Draw a shadow

This means that the shadow is calculated over and over again in each Draw Call. Now, the baking function is similar to the caching we used above. It only calculates the shadows once, and has drawn them on our texture, so they don’t need to be calculated over and over again. This massive performance advantage.

Try it out. This feature can be found under window-LightMap. There are more ways to bake things besides lighting, just play with them and see if they benefit your game’s performance.

Ten, dynamic light

The disadvantage of a baking lamp is that if one of the objects moves, its shadow won’t move with it because it was baked onto the texture a long time ago.

The opposite of a baking lamp is called a dynamic light.. As the object moves, so does the shadow.

While this sounds cool, there is one huge downside: As a rule of thumb, the scene has to be painted again every time a light is added. Therefore, if we use 2 lights in the field, we might get half the FPS(and so on).

While this is a rule of thumb and uniformity is still pretty fast, when it comes to drawing lights, it’s usually a good idea to use as few lights as possible.

11. Memory allocation

Let’s look at the following code:

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {
        
    // Update is called once per frame
    void Update () {
        SomeClass x = new SomeClass("Some".123."Parameters"); }}Copy the code

On each update call, SomeClassx is created (” assigned “) using the keyword. New.. When using the new keyword, United allocates some memory from our computer. When the object is no longer in use, it frees the memory again. This can be a very expensive calculation if done frequently.

There’s more behind the whole concept, but it’s important for us to remember that we should avoid new ones whenever we can.

An alternative might look like this:

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {
    // Cache
    SomeClass v = new SomeClass();
        
    // Update is called once per frame
    void Update () {
        v.setName("Some");
        v.setLevel(123);
        v.setSomethingElse("Parameters"); }}Copy the code

Xii. Abstract

Performance is one of the main problems in computer science. We discussed basic ways to increase your game’s FPS without using any scary math. However, a large, if not the largest, part lies in the programmer’s knowledge of mathematics, algorithms, and data structures. These topics can be overwhelming, but it’s important to learn as much as you can about them during your trip.

By the way, our other articles are generally not optimized for performance in order to keep them as easy to understand as possible. It’s up to you to apply what you’ve learned in this article.