Skip to content

CallProvider

Chillersanim edited this page Feb 9, 2020 · 4 revisions

CallProvider

Namespace: Unity_Tools.Components

Provides global callbacks for various situations, such as Update, FixedUpdate, OnGui and others.
Allows for dynamic subscription of calls on demand in-game and in-editor.

[ExecuteInEditMode]
public sealed class CallProvider : 
    SingletonBehaviour<CallProvider>

Remarks

Performance

The callback mechanism is optimized for performance and little overhead, even with many listeners.
If you have multiple listeners and need more performance, consider using the PeriodicUpdate over the Update callback.

Singleton Behavior

CallProvider inherits from SingletonBehavior.
Duo to Unitys handling of object destruction in the editor, it cannot be guaranteed that the Instance is accessible while the editor game is shutting down.

Before calling Instance methods, you need to make sure that the Instance can be accessed, by checking CallProvider.CanAccessInstance.
Otherwise calls might lead to InvalidOperationExceptions.

Static methods handle the instance access check internally and are guaranteed to work.

Periodic Update

If a behavior needs Update callbacks, but doesn't need the call to happen in every frame, it is advised to use the PeriodicUpdate instead.
The PeriodicUpdate has an allocated time for invoking calls, and spreads out the invocation over multiple frames if needed.

Subscribed listeners will be called one after another, until the allocated time frame defined by CallProvider.MaxPeriodicUpdateDuration is used up, or all listeners have been called in the same frame.
In the next frame, the invocation will continue where it was paused.

It is guaranteed that a listener will be called at most once per frame.

Editor

Callbacks work in the editor as well.
In the editor (not game), the Update, EditorOnlyUpdate, FixedUpdate and PeriodicUpdate callback will use the Application.Tick, leading to a consistent callback frequency.
The OnGizmos callback only works when the gizmos is enabled for the CallProvider.

Properties

  • MaxPeriodicUpdateDuration : float
    Gets or sets a value defining how much time is allocated per Update for PeriodicUpdate invocations, in seconds.

Methods

  • AddEditorOnlyUpdateListener(Action) : void
    Adds a listener to the EditorOnlyUpdate callback list.
  • AddFixedUpdateListener(Action) : void
    Adds a listener to the FixedUpdate callback list.
  • AddOnGizmosListener(Action) : void
    Adds a listener to the OnGizmos callback list.
  • AddOnGuiListener(Action) : void
    Adds a listener to the OnGui callback list.
  • AddPeriodicUpdateListener(Action) : void
    Adds a listener to the PeriodicUpdate callback list.
  • AddUpdateListener(Action) : void
    Adds a listener to the Update callback list.
  • RemoveEditorOnlyUpdateListener(Action) : void
    Removes a listener from the EditorOnlyUpdate callback list.
  • RemoveFixedUpdateListener(Action) : void
    Removes a listener from the FixedUpdate callback list.
  • RemoveOnGizmosListener(Action) : void
    Removes a listener from the OnGizmos callback list.
  • RemoveOnGuiListener(Action) : void
    Removes a listener from the OnGui callback list.
  • RemovePeriodicUpdateListener(Action) : void
    Removes a listener from the PeriodicUpdate callback list.
  • RemoveUpdateListener(Action) : void
    Removes a listener from the Update callback list.

Thread safety

The CallProvider is not thread safe and is intended to only be used from the main thread.