Hi all.
I followed the tutorial on setting up the Facebook Scores API that Greyzoned spelled out on YouTube, and had no trouble setting it up. When I use test users, the test users can see each other’s scores. I then submitted a request to Facebook to allow publish_actions, which was then approved. Now, all of my users are able to see their own scores posted on a leaderboard, but it doesn’t show the scores of anybody else on their friend list, as I understand it should.
I’m sure I’m missing something, but I can’t for the life of me figure out what.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class FBMain : MonoBehaviour {
public GameObject FBLoggedIn;
public GameObject FBNotLoggedIn;
public GameObject ScoreEntryPanel;
public GameObject ScoreScrollList;
public GameObject highScoresPanel;
public Text HighScoreText;
private Dictionary<string, string> profile =null;
public GameController gameController;
private List<object> scoresList = null;
private List<object> myScore = null;
public int myHighScore;
public void Start()
{
if (FB.IsLoggedIn) {
FB.API ("/me/scores", Facebook.HttpMethod.GET, delegate(FBResult result) {
if (result == null) {
HighScoreText.text = "High Score: 0";
} else {
myScore = Util.DeserializeScores (result.Text);
Debug.Log (result.Text);
foreach (object score in myScore) {
var entry = (Dictionary<string, object>)score;
Debug.Log(score);
HighScoreText.text = "High Score: " + entry ["score"].ToString ();
myHighScore = int.Parse (entry ["score"].ToString ());
}
}
});
} else
{
HighScoreText.text = "High Score Unknown";
}
}
void Awake()
{
FB.Init (SetInit, OnHideUnity);
}
private void SetInit()
{
Debug.Log ("FB Init Done.");
if (FB.IsLoggedIn) {
Debug.Log("FB Logged In");
DealWithFBMenus(true);
} else {
DealWithFBMenus(false);
}
}
private void OnHideUnity(bool isGameShown)
{
if (!isGameShown) {
Time.timeScale = 0;
} else {
Time.timeScale = 1;
}
}
public void FBLogin()
{
FB.Login ("email,publish_actions",AuthCallback);
}
void AuthCallback (FBResult result)
{
if (FB.IsLoggedIn) {
Debug.Log ("FB Login worked");
DealWithFBMenus(true);
} else {
Debug.Log ("FB Login Failed");
DealWithFBMenus(false);
}
}
void DealWithFBMenus(bool isLoggedIn)
{
if (isLoggedIn) {
FBLoggedIn.SetActive(true);
FBNotLoggedIn.SetActive (false);
} else {
FBLoggedIn.SetActive(false);
FBNotLoggedIn.SetActive (true);
}
}
public void FBShare()
{
GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
if (gameControllerObject != null) {
gameController = gameControllerObject.GetComponent <GameController> ();
}
if (gameController == null) {
Debug.Log ("Cannot find 'GameController' script");
}
FB.Feed (
linkCaption: "I just got a score of " + gameControllerObject.GetComponent<GameController>().score + " in Crystal Cracker! Can you beat me?",
linkName: "Play Crystal Cracker Now",
link: "http://roamsoftstudios.com/crystalcracker",
picture: "http://roamsoftstudios.com/wp-content/uploads/2015/05/crystalcrackerscreen.png"
);
}
public void InviteFriends()
{
FB.AppRequest (
message: "Give Crystal Cracker a try on iPhone or Android! It's too fun!",
title: "Invite your friends to play Crystal Cracker"
);
}
//All Scores API related items
public void QueryScores()
{
highScoresPanel.SetActive (true);
FB.API ("app/scores?fields=score,user.limit(30)", Facebook.HttpMethod.GET, ScoresCallback);
}
public void ScoresCallback(FBResult result)
{
Debug.Log ("Scores Callback: " + result.Text);
scoresList = Util.DeserializeScores (result.Text);
foreach (Transform child in ScoreScrollList.transform)
{
GameObject.Destroy (child.gameObject);
}
foreach (object score in scoresList) {
var entry = (Dictionary<string, object>) score;
var user = (Dictionary<string, object>) entry["user"];
GameObject ScorePanel;
ScorePanel = Instantiate (ScoreEntryPanel) as GameObject;
ScorePanel.transform.parent = ScoreScrollList.transform;
ScorePanel.transform.localScale = new Vector2(1,1);
Transform ThisScoreName = ScorePanel.transform.Find ("FriendName");
Transform ThisScoreScore = ScorePanel.transform.Find ("FriendScore");
Text ScoreName = ThisScoreName.GetComponent<Text>();
Text ScoreScore = ThisScoreScore.GetComponent<Text>();
ScoreName.text = user["name"].ToString();
ScoreScore.text = entry["score"].ToString();
Transform TheUserAvatar = ScorePanel.transform.Find("FriendAvatar");
Image UserAvatar = TheUserAvatar.GetComponent<Image>();
FB.API (Util.GetPictureURL(user["id"].ToString(), 128, 128), Facebook.HttpMethod.GET, delegate(FBResult pictureResult) {
if (pictureResult.Error != null)
{
Debug.Log (pictureResult.Error);
}
else
{
UserAvatar.sprite = Sprite.Create (pictureResult.Texture, new Rect(0,0,128,128), new Vector2(0,0));
}
});
}
}
public void SetScore()
{
if (FB.IsLoggedIn) {
GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
if (gameControllerObject != null) {
gameController = gameControllerObject.GetComponent <GameController> ();
}
if (gameController == null) {
Debug.Log ("Cannot find 'GameController' script");
}
var scoreData = new Dictionary<string,string> ();
scoreData ["score"] = gameController.score.ToString ();
FB.API ("/me/scores", Facebook.HttpMethod.POST, delegate(FBResult result) {
Debug.Log ("Score Submit Result: " + result.Text);
}, scoreData);
}
}
}

