Search code examples
c#apiunity-game-engineimgur

how to upload my players profile pics in unity c# to imgur privately


i am making a game in unity
it's quite challenging game to my normal skills
i've successfully done the registration code :D

but i am struggling at uploading players pics to server i've already tried imgur and followed all steps at this github repo

but it seems like it's not uploading. then i did some debugging i found out that it's uploading but anomalously that's why i didn't get info such as:(title, description, tags ... etc) back.

here is what i've got so far >> here <<

and here what i think that is the buggy part

public void UploadImage(string base64Image){
    Upload(base64Image, (response) =>{
        if (OnImageUploaded != null){
            OnImageUploaded(this, new OnImageUploadedEventArgs(response));
            Debug.Log("uploading completed!");
        }else{
            Debug.Log("OnImageUploaded = null");
        }
    });
}


private void Upload(string base64Image, Action<ImgurUploadResponse> OnUploadCompleted){
    Thread t = new Thread(() =>{
        using (WebClient wclient = new WebClient()){
            wclient.Headers.Add("Authorization", "Client-ID " + _clientId);
            NameValueCollection parameters = new NameValueCollection(){
                { "image", base64Image }
            };

            byte[] response = wclient.UploadValues(_baseUploadUrl, parameters);
            string json = Encoding.UTF8.GetString(response);

            Debug.Log("completed "+json);                       // it's here this debug never called
            OnUploadCompleted(JsonUtility.FromJson<ImgurUploadResponse>(json));
        }
    })
    {IsBackground = true};

    t.Start();
    Debug.Log("uploading started!");
}

Solution

  • Unity works with coroutines, not with Tasks. You can change your script like so:

    private IEnumerator Upload(string base64Image, Action<ImgurUploadResponse> OnUploadCompleted){
            using (WebClient wclient = new WebClient()){
                wclient.Headers.Add("Authorization", "Client-ID " + _clientId);
                NameValueCollection parameters = new NameValueCollection(){
                    { "image", base64Image }
                };
    
                byte[] response = wclient.UploadValues(_baseUploadUrl, parameters);
                string json = Encoding.UTF8.GetString(response);
    
                Debug.Log("completed "+json);
                OnUploadCompleted(JsonUtility.FromJson<ImgurUploadResponse>(json));
            }
    }
    

    And call it using StartCoroutine

    public void UploadImage(string base64Image){
        StartCoroutine(
        Upload(base64Image, (response) =>{
            if (OnImageUploaded != null){
                OnImageUploaded(this, new OnImageUploadedEventArgs(response));
                Debug.Log("uploading completed!");
            }else{
                Debug.Log("OnImageUploaded = null");
            }
        }));
    }
    

    If this does not work, then you might want to take a look at WWWForm