xxxxxxxxxx
public delegate void Callback(/* Paramaters if needed*/);
public void ChangeStuff(Callback callback) {
// Do stuff
callback();
}
// In the executed code:
// Executes the 'ChangeStuff' function
ChangeStuff(( /* Necessary paramters if needed */) => {
// Executes the code when 'callback' of the 'ChangeStuff'
// function is called
})
// The above code should obviously be in a class
xxxxxxxxxx
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CallbackUsingDelegateDemo : MonoBehaviour {
public delegate void WorkCompletedCallBack(string result);
public void DoWork(WorkCompletedCallBack callback){
bool isWorkDone;
//do work here
isWorkDone = true;
if (isWorkDone)
callback ("yes work done successfully...");
else
callback ("sorry work not done...");
}
void Start(){
WorkCompletedCallBack workResultCallback = ShowWorkResult;
DoWork (workResultCallback);
}
//this is the callback which is called when the work is done in DoWork()
public void ShowWorkResult(string result){
Debug.Log (result);
}
}