DELEGATES IN UNITY:
A delegate is a reference pointer to a method. When it is called, it gives notification to all methods that reference the delegate. It allows us to treat a method as a variable and pass method as a variable for a callback. The basic idea of a delegate is exactly the same as a subscription magazine. Anyone can subscribe to the services and they will receive the update at the right time automatically.
TYPES of Delegate:
1.Single Delegate:- It can reference to only single method at a time.
2.Multicast Delegate:- It can store the reference of multiple methods at a time.
SYNTAX :
//Defining Delegates
public delegate void OnMouseClickDelegte();
public static OnMouseClickDelegte mouseClickDelegate;
//Subscribing to a delegate
// eg. of singlecast delegate
mouseClickDelegate = myExampleMethod;
//eg. of multicast delegate
mouseClickDelegate += myExampleMethod;
mouseClickDelegate +=myAnotherExampleMethod;
//Calling delegates
mouseClickDelegate();
//UnSubscribing to delegates
mouseClickDelegate -= myExampleMethod;
mouseClickDelegate -=myAnotherExampleMethod;
0 Comment(s)