Tutorial: Update call reduction.

Before I dive into this tutorial this method came from this blog post over on the Unity3D site. That post goes more into the performance of it along with having an implementation of it on Github.

In this tutorial, I'm going to go into my own implementation which expands on the groundwork set in the above post.

This variation does the following:

  • Uses abstration to only have one Start() and Update() function running at a time.
  • Allows something to be passed into the start (in this case it's a GameObject but it can be anything).
  • Allows for updating the object list at any point.

So, first things first, you'll need to setup a class called ManagedObjectBehaviour.cs. This class will inherit the MonoBehaviour class and be abstracted with two abstracted methods StartMe(GameObject managers) and UpdateMe()

The StartMe method will replace any of the Unity Start() methods later on and as you may have guessed UpdateMe will replace any of the Unity Update() methods.

At this point your ManagedObjectBehavious.cs class should look like the following:

using UnityEngine;

public abstract class ManagedObjectBehaviour : MonoBehaviour
{
    public abstract void StartMe(GameObject managers);
    public abstract void UpdateMe();
}

Presuming you don't have one already you'll need to have a central point in your scene where the main update code can sit. Personally, I have a 'Management' GameObject where things like a GameManager, UiManager etc sit.

In this case we're going to create a GameManager.cs class this will hold an array of ManagedObjectBehaviours and will iterate through out them either running .StartMe() or .UpdateMe().

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour
{
    private ManagedObjectBehaviour[] _currentObjects;
    private GameObject _managers;
    
    void Start()
    {
        _managers = gameObject;
        
    }
    
    void Update()
    {
        for(var i = 0; i < _currentObjects.Length; i++)
        {
            _currentObjects[i].UpdateMe();
        }
    }
    
     public void RefreshObjects()
    {
        _currentObjects = FindObjectsOfType<ManagedObjectBehaviour>();
        for (var i = 0; i < _currentObjects.Length; i++)
        {
            _currentObjects[i].StartMe(_managers);
        }
    }
}

Once you have the GameManager class built to use this, any new classes will have to inherit from ManagedObjectBehaviour and implement their own StartMe(GameObject managers) and UpdateMe() as follows.

using UnityEngine;

public class SomeClass : ManagedObjectBehaviour
{
    public override void StartMe(GameObject managers)
    {
        //Startup logic here.
    }
    
    public override void UpdateMe()
    {
        //Update logic here.
    }
}

Replace the comments with your own logic and what you'd normally put in the Start() and Update() functions.

It's also worth noting that because the ManagedObjectBehaviour class inherits from MonoBehaviour it's possible to still use the Unity functionaility such as OnTriggerEnter() etc.

If you have any questions or suggestions feel free to contact me over on twitter and if you found this tutorial useful feel free to throw a coffee my way.

Luke Parker

Luke Parker