unity3d - 无法在设备上显示 facebook 个人资料图像(模拟器完美运行)

unity3d - can't show facebook profile image on the device ( simulator works perfect )

我在 Facebook API 中使用 Unity3d (c#)。当我在统一模拟器上 运行 时,我尝试在屏幕上显示我的个人资料图像,一切正常,我得到了图像。但是当我构建一个版本并在真实设备上试用时,我什么也没得到。

我知道 facebook CDN 有问题。请求的结果是 302 并且 WWW class 无法处理重定向所以我做了这样的事情(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);
    }
}
  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);
        }
    }