Callbacks
Understanding the concepts of a callback as it relates to software engineering and programming in C#

Introduction

This article will touch on the concepts of a callback as it relates to programming in C# for Unity. This article is not intended to be a primary teaching source, rather an exploration of the feature as it relates to typical approaches you might see in Unity scripts.

Learning First

While there are many "code free" tools to help prototype, and many would claim fully create a game with no programming required, you and your users will benefit greatly from a basic level of programming skill. In particular this will be important to achieve a level of quality in terms of your user experience in particular around the use of callbacks, delegates and event driven architectures.
Learning the basics shouldn't be scary at all, and in our opinion can often be done faster and to a higher degree of mastery easier; than the effort involved in learning many of the "Visual Programming" tools. At the end of the day you probably already know how to type, so you already know more about programming than you do about some visual scripting tool since creating code is just literally typing words.
Junior Programmer - Unity Learn
Unity Learn
Unity Learn is a site provided by Unity that helps teach various skills around Unity game development, and the Junior Programmer path is a great starting place for absolutely anyone who will be using Unity at all.

Understanding

If you want a more primary source of learning around the concept, please review sources such as Microsoft's documentation on the subject
Action<T> Delegate (System)
docsmsft
For our purposes the term callback refers to any pattern where in calling some function causes a "callback" to a desired function.
Example
1
public void Foo1()
2
{
3
Debug.Log("Foo1 processing, calling Foo2");
4
}
5
6
public void Foo2(Action callback)
7
{
8
Debug.Log("Foo2 processing, invoking callback");
9
callback?.Invoke();
10
}
11
12
public void Foo3()
13
{
14
Debug.Log("Foo3 processing");
15
}
Copied!
The above code would result in output such as:
1
Foo1 processing, calling Foo2
2
Foo2 processing, invoking callback
3
Foo3 processing
Copied!
Notice in the above code how we call the callback
1
callback?.Invoke();
Copied!
The question mark there (?) tells the compiler to check for null, if null return null, else call Invoke(); this is the same as writing
1
if(callback != null)
2
callback();
Copied!

Examples

Callbacks are typically used with asynchronous calls, such as calls to web services, other processes or multi-threaded calls. A callback operates much like an event however a callback is for that purpose only where as event is registered to aka "listened to" and may be invoke by any range of processes where a callback is passed into the call that will ultimately invoke it.

Coroutine Example

1
public IEnumerator DoPsedoAsyncWorkInUnity(Action callback)
2
{
3
yield return new WaitForEndOfFrame();
4
5
callback?.Invoke();
6
}
Copied!
This example would be used with Unity's concept of a coroutine where you would pass in a function to be called when the process completed.
Calling this might look like
1
StartCoroutine(DoPsedoAsyncWorkInUnity(HandleWorkDone));
Copied!
Where HandleWorkDone is some function in your code.

Passing Parameters

Callbacks can take up to 16 parameters, for example
1
public void Foo(Action<string, int, float> callback)
2
{
3
callback?.Invoke("Hello World", 42, 6.9f);
4
}
Copied!
Last modified 27d ago