Delegates
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. A Delegate can be thought of as a reference pointer to an object/method. When it gets called, it notifies all methods that reference the delegate.
Delegate Multicast Example (+=):
using UnityEngine; using System.Collections; public class MulticastScript : MonoBehaviour { delegate void MultiDelegate(); MultiDelegate myMultiDelegate; void Start () { myMultiDelegate += PowerUp; myMultiDelegate += TurnRed; if(myMultiDelegate != null) { myMultiDelegate(); } } void PowerUp() { print ("Orb is powering up!"); } void TurnRed() { renderer.material.color = Color.red; } }
Action Delegates
You can use the Action(Of T) delegate to pass a method as a parameter without explicitly declaring a custom delegate. The benefit here is you don’t have to declare a delegate. The compiler is smart enough to figure out the proper types.
However there is a limitation, the corresponding method action must not return a value. (In C#, the method must return void.)
Action Delegate Example:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ModernLanguageConstructs { class Program { static void Main(string[] args) { // Part 1 - First action that takes an int and converts it to hex Action<int> displayHex = delegate(int intValue) { Console.WriteLine(intValue.ToString("X")); }; // Part 2 - Second action that takes a hex string and // converts it to an int Action<string> displayInteger = delegate(string hexValue) { Console.WriteLine(int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber)); }; // Part 3 - exercise Action methods displayHex(16); displayInteger("10"); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ModernLanguageConstructs { class Program { static void Main(string[] args) { // Part 1 - First Func<> that takes an int and returns a string Func<int, string> displayHex = delegate(int intValue) { return (intValue.ToString("X")); }; // Part 2 - Second Func<> that takes a hex string and // returns an int Func<string, int> displayInteger = delegate(string hexValue) { return (int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber)); }; // Part 3 - exercise Func<> delegates Console.WriteLine(displayHex(16)); Console.WriteLine(displayInteger("10")); } } }
private IEnumerator waitThenCallback(float time, Action callback) { yield return new WaitForSeconds(time); callback(); } void Start() { splashScreen.show(); StartCoroutine(waitThenCallback(5, () => { Debug.Log("Five seconds have passed!"); })); StartCoroutine(waitThenCallback(10, () => { Debug.Log("Ten seconds have passed!"); })); StartCoroutine(waitThenCallback(20, () => { Debug.Log("Twenty seconds have passed!"); splashScreen.hide(); })); }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ModernLanguageConstructs { class Program { static void Main(string[] args) { // Part 1 - An action and a lambda Action<int> displayHex = intValue => { Console.WriteLine(intValue.ToString("X")); }; Action<string> displayInteger = hexValue => { Console.WriteLine(int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber)); }; // Part 2 - Use the lambda expressions displayHex(16); displayInteger("10"); } } }