Update Manager - Hertzole/HertzLib GitHub Wiki

Update Manager

Update manager is a singleton script that can handle all the Update methods for you. It requires some very easy set up in scripts but after that, it will handle everything for you. It's basically a class you add your update methods to (FixedUpdate and LateUpdate included) and it will call them on all the added scripts.

Why?

In Unity's blog post '10000 Update() calls', they talked all about this. But the TL;DR is basically, having 10K objects calling Unity's "magic methods" is bad, whilst only having one object to call a similar method on 10K objects is better.

How to use

The only thing you need to do in order to make it work with your scripts is to add it to the UpdateManager. I highly recommend you add it in OnEnable and remove it in OnDisable. You also need to add IUpdate, IFixedUpdate, or ILateUpdate to your script. Here's a full example script to show you how it can be done.

public class UpdateMethods : MonoBehaviour, IUpdate, ILateUpdate, IFixedUpdate
{
    private void OnEnable()
    {
        // Add the required updates to the Update Manager.
        UpdateManager.AddUpdate(this);
        UpdateManager.AddLateUpdate(this);
        UpdateManager.AddFixedUpdate(this);
    }

    private void OnDisable()
    {
        // Remove the required updates from the Update Manager.
        UpdateManager.RemoveUpdate(this);
        UpdateManager.RemoveLateUpdate(this);
        UpdateManager.RemoveFixedUpdate(this);
    }

    public void OnUpdate()
    {
    }

    public void OnLateUpdate()
    {
    }

    public void OnFixedUpdate()
    {
    }
}

Of course, you don't have to remove it in OnDisable. This gives you a bit more control over how Update is called and can even be called on disabled objects if you choose to do so. But it's very important to clean up after yourself!

Define

The Update Manager will automatically add a define to your project. The define it adds is HERTZLIB_UPDATE_MANAGER and can then be used by scripts to automatically adapt if it's present. Right now, my other asset Gold Player uses it to automatically use the Update Manager if it's present.