- Home /
Getting multiple photos at once but in the correct order with Facebook SDK
I'm trying to implement a facebook friend leaderboard into my game. It currently gets all scores posted and displays them correctly with the user who posted them. I can also grab the profile photos of the people who posted said scores. However, the photos are not assigned to the correct prefab (my guess is because of the varying speeds of download) so the photos are all wrong.
Here is the call to instantiate the score prefabs and to set the values:
void getScoreCallBack(IResult result)
{
IDictionary data = result.ResultDictionary["scores"] as IDictionary;
List<object> scoreList = (List<object>)data["data"];
foreach(object obj in scoreList)
{
if (tempPic.sprite == null)
{
var entry = (Dictionary<string, object>)obj;
var user = (Dictionary<string, object>)entry["user"];
scorePanel = Instantiate(scorePrefab) as GameObject;
scorePanel.transform.SetParent(scrollScoreList.transform, false);
Text friendName = scorePanel.transform.Find("Name").GetComponent<Text>();
Text friendScore = scorePanel.transform.Find("Score").GetComponent<Text>();
Image friendPic = scorePanel.transform.Find("Image").GetComponent<Image>();
friendName.text = user["name"].ToString();
friendScore.text = entry["score"].ToString();
Debug.Log(user["id"].ToString());
FB.API(user["id"].ToString() + "/picture?redirect=false", HttpMethod.GET, ProfilePhotoCallback);
StartCoroutine(checkIfPicSet(friendPic));
Debug.Log(tempPic.sprite.ToString());
}
tempPic.sprite = null;
}
}
I tried to hack a solution in using a coroutine the check if the a sprite had been downloaded yet, here:
private IEnumerator checkIfPicSet(Image pic)
{
yield return new WaitUntil(() => tempPic.sprite != null);
Debug.Log(tempPic.sprite.ToString());
pic.sprite = tempPic.sprite;
tempPic.sprite = null;
}
I think it has to do with it assigning the first photo downloaded to the first instantiated prefab and so on, but I have no idea how to go about controlling this bug. Any ideas or suggestions will be hugely appreciated!
EDIT - Here are some pictures so you have a better idea of what I mean. Because I'm only using 2 entries at the moment it sometimes work if the 2nd entry has its photo downloaded 2nd as so:
Then here is a photo of it not working, you can clearly see "Dave" is logged in but the picture is attached to "Lewis":
Answer by upasnavig90 · Mar 02, 2018 at 07:03 AM
hey, you can do one thing here, just add the API for downloading image from fb and the coroutine in a new script and attach it to the image gameObject of your prefab.
Then just pass the userid parameter to the new script component of image of "scorePanel".
what will happen with this is:
when you assign id's individually, every image gameObject of your prefab will download its own image having its own id no matter what delay will be there in downloading.
i hope you understand the scenerio.
Your answer
Follow this Question
Related Questions
How to disable a script on a bunch of instantiated objects with tag ? 1 Answer
Use of “@import” when C++ modules are disabled @import FBSDKCoreKit error 1 Answer
Distribute terrain in zones 3 Answers
Facebook Deep Link in Unity3D equal to null 0 Answers
Having multiple objects fire prefabs in different times C# 0 Answers