Greetings,
I had it in mind to create a base MonoBehavior script class, which provides virtual base implementations of all the primary update functions: Update, FixedUpdate, and LastUpdate. Each game script would then inherit from this base class (instead of MonoBehavior), and simply override any of the functions that it needs.
A simple implementation of the idea looks like this:
public class BehaviorBase : MonoBehaviour
{
[SerializeField]
private bool _updateWhenPaused = false;
public bool UpdateWhenPaused
{
get { return _updateWhenPaused; }
set { _updateWhenPaused = value; }
}
// The GameStateManager is a singleton which tracks the paused/unpaused
// state of the game
protected GameStateManager GameState { get { return GameStateManager.Instance; } }
private void Awake()
{
OnAwake();
}
private void Start()
{
OnStart();
}
private void Update()
{
IfNotPaused(OnUpdate);
}
private void FixedUpdate()
{
IfNotPaused(OnFixedUpdate);
}
private void LateUpdate()
{
IfNotPaused(OnLateUpdate);
}
// Proxy function; will only call the provided action, if the game state
// is 'not paused', or if the script is configured to Update when paused.
private void IfNotPaused(Action action)
{
var paused = GameState.IsPaused;
if(!paused || UpdateWhenPaused)
action();
}
// Override these, to implement your initialization and update logic
protected virtual void OnStart() { }
protected virtual void OnAwake() { }
protected virtual void OnUpdate() { }
protected virtual void OnFixedUpdate() { }
protected virtual void OnLateUpdate() { }
}
And the child class could look like this:
public class MyChildScript : BehaviorBase
{
protected override void OnUpdate()
{
// Update logic here.
}
}
As you can see, this can be used to implement a convenient “Pause” system - the virtual OnUpdate*() functions are only called if the game is unpaused, or if the script has explicitly toggled its “UpdateWhenPaused” property.
The concern I have is; surely there is a reason Unity didn’t implement this type of virtual function hierarchy in the first place? Is there going to be a noticable performance hit by implementing all these functions ‘by default’ in the base class?