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”: