Repository navigation
Expand file tree
/
Copy pathUsingDelegates.cs
More file actions
60 lines (50 loc) · 2.07 KB
/
Copy pathUsingDelegates.cs
File metadata and controls
60 lines (50 loc) · 2.07 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using UnityEngine;
using System.Collections;
public class UsingDelegates : MonoBehaviour
{
// Since Delegates are types, we are allowed to pass them to functions as though they were variables as well.
// The basic idea of the delegate allows you to pick and choose between functions to assign to another function as an argument.
// This also means that you can assign a function to a variable almost like any other data type.
delegate int MyDelegate();
// A delegate doesn't need to have its function declared ahead of its use.
// just having a function named Del() and declaring it a delegate with return type void is enough to have a signature to work with.
delegate void Del();
// Use this for initialization
void Start ()
{
int a = 10;
// We create a new instance of the delegate with Del d. Then we assign it a task to do when d(); is called.
// This format looks strange, because we are assigning a function inside of a function. This also means that we can reassign the function after it is used.
Del d = delegate()
{
Debug.Log ("delegate calling");
};
d (); // outputs "delegate calling"
d = delegate() // reassigning d with a new function
{
Debug.Log ("a/2 = " + a / 2);
};
HandlesDel (d); // outputs "a/2 = " 5
UseDelegate (GetThree);
}
void HandlesDel(Del del) // Now that Del is a type, it can now be passed between functions as an argument. Instead of dealing with d(); at the end of the Start()
// we can also pass d to a function that has the Del type as an argument variable.
// The function HandlesDel(Del del) can accept d as an argument. This means that a function is a type. As a type it can be passed between functions as data.
// The behavior also has an effect on the data inside of the function. Therefore data is also something that does work, contains logic, and can perform tasks.
{
del ();
}
int GetThree()
{
return 3;
}
void UseDelegate(MyDelegate mDelegate)
{
int gotNumber = mDelegate ();
Debug.Log (gotNumber);
}
// Update is called once per frame
void Update ()
{
}
}