Action Callback in Unity — Code Saying

What is an Action?

void Start() { 
System.Action<string> printAction = new System.Action<string>(Display);
printAction("Welcome to CodeSaying");
}
void Display(string message) {
Debug.Log(message);
}
System.Action<string> printAction = Display; 
printAction("Welcome to CodeSaying");

Using Lambda Expressions with Action in Unity

void Start { 
Action<int> printAction = i => Debug.Log(i);
printAction(10);
}

Using Action Callback in Unity

void callApi() { 
StartCoroutine(FetchData(ApiAction));
}
string url = "https://postman-echo.com/get?foo1=bar1";
IEnumerator FetchData(System.Action<string, bool> callback) {
UnityWebRequest getData = UnityWebRequest.Get(url);
yield return getData.SendWebRequest();
if (getData.isDone == false || getData.error != null)
callback(getData.error, false);
else
callback(getData.downloadHandler.text, true);
}
void ApiAction(string response, bool isSuccess) {
if (isSuccess)
Debug.Log(response);
}
void callApi(){ 
StartCoroutine(FetchData((response, isSuccess) => {
if (isSuccess) {
Debug.Log(response);
}
}));
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store