Hello!
I’ve been reading allot about this and there is very few information on this subject.
I am having trouble managing unity and facebook sdk with the score system.
So, I might be making some mistakes and I hope someone can help me through them.
So, when I open my game, I can either already be connected, or need to press the conenct button.
And this is the code I have so far.
The problem here is that I get an error when trying to POST a score = to 0.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Facebook.MiniJSON;
public class FacebookInit : MonoBehaviour {
// Use this for initialization
void Start () {
FB.Init(OnInitComplete);
}
private void OnInitComplete()
{
if (FB.IsLoggedIn)
{
FB.API("/"+FB.UserId+"/scores", Facebook.HttpMethod.GET, retrieveScore);
gameObject.SetActive(false);
}
else {
gameObject.GetComponent<UILabel>().text = "Connect With Facebook!";
}
}
void retrieveScore(FBResult result)
{
Dictionary<string, object> playerScore = Json.Deserialize(result.Text) as Dictionary<string, object>;
object score;
if (playerScore.TryGetValue("score", out score))
{
Debug.Log(score);
}
else
{
// Player has no score registered, so lets make it 0? Is this right?
var wwwForm = new WWWForm();
wwwForm.AddField("score", "0");
FB.API("/" + FB.UserId + "/scores", Facebook.HttpMethod.POST, Callback, wwwForm);
}
}
void Callback(FBResult result)
{
Debug.Log(result.Error);
}
public void CallFBLogin()
{
FB.Login("email,publish_actions", LoginCallback);
}
void LoginCallback(FBResult result)
{
if (result.Error != null){
Debug.Log("Error Response:\n" + result.Error);
}
else if (!FB.IsLoggedIn)
{
Debug.Log("Login cancelled by Player");
}
else
{
Debug.Log("Login was successful!");
FB.API("/" + FB.UserId + "/scores", Facebook.HttpMethod.GET, retrieveScore);
gameObject.SetActive(false);
}
}
}