Skip to content
  • Home
  • UNITY TUTORIALS
  • OUR GAMES

Aliasing Games DevBlog

Journey about making games.

Delegates #1 – Introduction

By: wwysocki
On: November 15, 2015
In: FastTrick
Tagged: C#, Unity
With: 4 Comments

Delegates #1 – Introduction

Delegates1_512Delegates – introduction

Have you ever used delegates? If not, the time has come to learn what delegates are. In this quick post we will show you delegates’ basics. As the title says, it will be short introduction which presents only fundamental usage of delegates.

Let’s start with Microsoft’s MSDN C# documentation. There is a short delegate’s definition:

A delegate is a type that safely encapsulates a method, similar to a function pointer in C and C++. Unlike C function pointers, delegates are object-oriented, type safe, and secure. The type of a delegate is defined by the name of the delegate.

The most intuitive way is to treat delegates as pointers to lists of functions. It’s not a mistake – delegate can “have” more than one function! To create a simple delegate just declare:

C#
1
2
private delegate float SimpleDelegate(int par0, string par1, bool par2);
private SimpleDelegate simpleDelegate;

Short explanation: simpleDelegate can point at any function (or functions) which has three parameters (integer, string and boolean) and returns float as result. It can be also passed as argument into another function. To show delegate in action let’s look at this simple code snippet:

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using UnityEngine;
using System.Collections;
 
public class DelegatesTest : MonoBehaviour {
 
private delegate void MultiplyDelegate (float val);
 
private MultiplyDelegate multiplyDelegate;
 
void Awake() {
// assign first method to delegate
multiplyDelegate = DoubleVal;
multiplyDelegate (5f); // execute
 
// assing second method to delegate
multiplyDelegate = TripleVal;
multiplyDelegate (10f);
 
// remove TripleVal method from delegate
multiplyDelegate -= TripleVal;
 
// add both methods
multiplyDelegate += DoubleVal;
multiplyDelegate += TripleVal;
PassDelegateAsArgument (multiplyDelegate);
}
 
private void DoubleVal (float val) {
Debug.Log (string.Format ("2 * {0} = {1}", val, 2f * val));
}
 
private void TripleVal (float val) {
Debug.Log (string.Format ("3 * {0} = {1}", val, 3f * val));
}
 
private void PassDelegateAsArgument(MultiplyDelegate del) {
del (20f);
}
}

To use this script in Unity just create new project, insert empty object into scene and attach script to it. Then press Play and check Console for results. What do you see? If everything is set up correctly you have to see four new lines with multiplications. That’s the power of delegates that you can fire many methods at once and also use them as well as other functions’ arguments.

That’s all for now. In next posts we will show you more practical use of delegates in Unity projects to create clean and robust pieces of code!

Cheers,
Aliasing Games Team!

+12
Tweet
Share6
Share
Shares 8

Related

2015-11-15
Previous Post: Render lines – Unity
Next Post: Delegates #2 – Events
  • Pingback: FastTrick - Delegates #2 - Events - Aliasing Games()

  • Pingback: FastTrick - Delegates #3 - Supplement - Aliasing Games()

  • Pingback: FastTrick - Delegates #4 - Simple game with events! - Aliasing Games()

  • Pingback: Unity Pause Manager implementation - Aliasing Games()

Click to play New Tap Order 2!

New Tap Order 2

Navigate

  • Home
  • UNITY TUTORIALS
  • OUR GAMES

Follow us on Twitter

My Tweets

Designed using Dispatch WordPress Theme. Powered by WordPress.