unity add callback code example

Example 1: unity callback

void callApi(){
        StartCoroutine(FetchData((response, isSuccess) =>
        {
            if (isSuccess)
            {
                Debug.Log(response);
            }
        }));
    }

Example 2: unity callback

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);
    }