I am not sure what is the cause of this problem but here it goes
i am using facebook-sdk-unity to get user’s profile picture. instead of getting it from FB API, i am hitting a url(which is aquired from graph api request for profile picture) with WWW class object and instead of using yield return expression i am just running a loop untill i get www.isDone = true
But the problem is it is never getting out of the loop. so my code is freezing.
in unity editor it works fine. this problem only occurs on devices.
i have cross checked hitting image url in browser. i am getting an image in browser already, nothing wrong there.
here is my code :-
public GameObject cube;
void Start ()
{
try
{
saveUserProfilePICFromURL("https://graph.facebook.com/{facebook-id of user}/picture");
WWW www = new WWW ("file://"+Application.persistentDataPath + "/UserPIC.png");
while(!www.isDone);
cube.renderer.material.mainTexture = www.texture;
}
catch(System.Exception e)
{
Debug.Log("Exception: WebServices_Start "+e.Message);
}
}
internal void saveUserProfilePICFromURL(string targetURL)
{
targetURL = targetURL.Replace(@"\/","/");
Debug.Log ("target url "+targetURL);
WWW www = new WWW(targetURL);
//yield return www;
Debug.Log ("www.isStarted ");
int i = 0;
while(!www.isDone)
{
Debug.Log ("www.isInProgress "+i);// program gets trapped in this loop
i++;
};
Debug.Log ("www.isDone ");
Texture2D PIC = www.texture;
//cube.renderer.material.mainTexture = PIC;
byte[] bytes = PIC.EncodeToPNG();
Debug.Log ("PIC data length "+bytes.Length);
string path = Application.persistentDataPath + "/UserPIC.png";
Debug.Log ("path "+path);
System.IO.File.WriteAllBytes(path, bytes);
}
So the problem is, program never gets out of the loop.
how ever if i use the following code for downloading image. i am getting the image but its always empty. :-
internal void saveUserProfilePICFromURL(string targetURL)
{
string destinationURL = Application.persistentDataPath + "/UserPIC.png";
targetURL = targetURL.Replace(@"\/","/");
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
webClient.DownloadFileAsync (new Uri (targetURL), destinationURL);
}