如何将Unity C#中的球员个人资料照片上传到imgur私下

时间:2018-08-23 11:26:06

标签: c# api unity3d imgur

我正在统一制作游戏
对我的普通技能来说是非常具有挑战性的游戏
我已经成功完成了注册代码:D

但是我正在努力将玩家照片上传到服务器 我已经尝试过imgur并按照此github repo

的所有步骤进行操作

,但似乎没有上传。然后我做了一些调试,发现 它正在上传,但异常是为什么我没有得到诸如:(标题,描述,标签等)的信息。

  

这是我到目前为止所获得的>> here <<< / p>

在这里,我认为这是越野车部分

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

1 个答案:

答案 0 :(得分:0)

Unity适用于协同程序,而不适用于Tasks。 您可以这样更改脚本:

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

并使用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");
        }
    }));
}

如果这不起作用,那么您可能想看看WWWForm