Unity3d -不能在设备上显示facebook个人资料图像(模拟器工作完美)

本文关键字:图像 个人资料 模拟器 工作 完美 facebook 显示 不能 Unity3d | 更新日期: 2023-09-27 18:09:03

我使用Unity3d (c#)与facebook API。当我在unity模拟器上运行时,我试图在屏幕上显示我的个人资料图像,一切都很完美,我得到了图像。但是当我构建一个版本并在真正的设备上试用时,我什么也得不到。

我知道facebook CDN有问题。请求的结果是302和WWW类无法处理重定向,所以我做了这样的事情(c#对我来说是一门新语言):

WWW www = new WWW(url);
    yield return www;
    if( www.responseHeaders.ContainsKey( "LOCATION" ) ){
        var redirection = www.responseHeaders[ "LOCATION" ];
        WWW wwwRe = new WWW( redirection );
        yield return wwwRe;
        callback( wwwRe.texture, userID );
    }else{
        callback( www.texture, userID );
    }

请帮帮我,我疯了,为什么我所有的个人数据都在设备上,除了完美统一的图像。我做错了什么?

谢谢。

解决方案:

我尝试了许多选项在设备上获得配置文件图像,但没有工作。最后,我将Facebook SDK从6.2.1升级到6.2.2,Unity从5.1升级到5.1.3,再次从设备上删除应用程序(清除所有数据),它就能工作了。这似乎是Facebook的问题(这不是他们第一次发布有漏洞的SDK)。

我接受Umair的答案,即使他的代码有一些语法问题,他真的试图帮助我,基本上他的答案是正确的。

我用下面的代码来测试我的图像:(希望对某人有所帮助)

private void getMyProfileData(){
    // get profile image.
    FB.API( Util.GetPictureURL( "me", 128, 128 ), Facebook.HttpMethod.GET, callBackGetProfilePicture );
}
private void callBackGetProfilePicture( FBResult result ){
    // in case if there some error with the image.
    if( result.Error != null ){
        // call this method again
    }
    string ImageUrl = Util.DeserializePictureURLString( result.Text );  
    StartCoroutine(LoadPictureCoroutune( ImageUrl ));
}
IEnumerator LoadPictureCoroutune(string url){
    var profilePicRequest = new WWW( (string)url );
    yield return profilePicRequest;
    if( profilePicRequest.error == null)
    {
        Texture2D profilePic = profilePicRequest.texture;
            // my test image place
            Image profileImage = FBavatar.GetComponent<Image>(); 
            profileImage.sprite = UnityEngine.Sprite.Create( profilePic, new Rect(0,0,128,128), new Vector2( 0, 0 ));
    }
    else
    {
        Debug.LogError("Error While downloading Picture: " + profilePicRequest.error);
    }
}

Unity3d -不能在设备上显示facebook个人资料图像(模拟器工作完美)

  1. 使用以下代码从Facebook API获取图片数据:

    FB.API ("/v2.4/me/?fields=picture", Facebook.HttpMethod.GET, RequestResponce);
    
  2. RequestResponce方法中:

    void RequestResponce(FBResult result)
    {
        Debug.Log("requestResponce(): " + result.text);
        if (!string.IsNullOrEmpty(result.Error))
        {
            Debug.Log("FB_API:ReadRequest:Failed:"+result.Error);
        }
        else
        {
            var data = SimpleJSON.JSON.Parse(result.Text);
            string url = data["picture"]["data"]["url"];
            StartCoroutine(LoadPictureCoroutune(url));
        }   
    }
    
  3. IEnumerator LoadPictureCoroutune(string url)
    {
        var profilePicRequest = new WWW(url);
        Debug.Log("Going to Load Picture ");
        yield return profilePicRequest;
        if(profilePicRequest.error == null)
        {
            Texture2D profilePic = profilePicRequest.texture;
            if(profilePic != null)
            {
                // Use ProfilePic to wherever you want to.
                //SetPicture(ProfilePic);
            }
        }
        else
        {
            Debug.LogError("Error While downloading Picture: " + profilePicRequest.error);
        }
    }