Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

c# callback using delegate

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);
	}
}
 
PREVIOUS NEXT
Tagged: #callback #delegate
ADD COMMENT
Topic
Name
4+3 =