简体   繁体   中英

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

I am using Unity3d (c#) with facebook API. I try to show my profile image on the screen when I run it on the unity simulator everything works perfect and I got the image. but when I build a version and try it on a real device I get nothing.

I know that there is a issue with facebook CDN. the result of the request is 302 and WWW class can't handle with redirects so I did something like this (c# is a new language for me):

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

please help me I loose my mind, why all my personal data comes on the device except the image that works perfect in unity. what I did wrong?

thanks.

The solution:

I tried many options to get profile image on the device nothing worked. finally I upgrade Facebook SDK from 6.2.1 to 6.2.2 and Unity from 5.1 to 5.1.3 remove the app again from the device ( clear all the data ) and it works. it looks like Facebook's issue ( this is not the first time that they release SDK with bugs ).

I accept Umair's answer even that his code have some syntax issues, he really tried to help me and basically his answer is right.

I used this code to test my image: ( hope it will help for someone )

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. Use following Code to get Picture data from Facebook API:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM