Skip to content

Instantly share code, notes, and snippets.

@JWardee JWardee/Demo.cs
Created Oct 25, 2015

Embed
What would you like to do?
Helper class for using the Facebook API with Unity v5, makes use of the excellent SimpleJSON class to decode JSON objects https://gist.github.com/darktable/1411710
using UnityEngine;
using System;
public class Demo : MonoBehaviour {
void Start() {
new FB_Query("/me", callback);
}
public void callback(SimpleJSON.JSONNode result) {
Debug.Log("Name: "+result["full_name"]);
}
}
using UnityEngine;
using System;
using System.Collections.Generic;
using Facebook.Unity;
using SimpleJSON;
public class FB_Query : MonoBehaviour {
static private List<string> perms = new List<string>() { "public_profile", "user_friends" };
private string query;
private Action<SimpleJSON.JSONNode> callback;
public FB_Query(string _Query, Action<SimpleJSON.JSONNode> Callback) {
this.query = _Query;
this.callback = Callback;
if (!FB.IsInitialized) {
FB.Init(CheckLoggedIn);
} else {
CheckLoggedIn();
}
}
private void CheckLoggedIn() {
if (!FB.IsLoggedIn) {
FB.LogInWithReadPermissions(perms, MakeAPICall);
} else {
MakeAPICall();
}
}
private void MakeAPICall() {
FB.API(this.query, HttpMethod.GET, Result);
}
private void MakeAPICall(ILoginResult result) {
MakeAPICall();
}
private void Result(IResult result) {
var data = JSON.Parse(result.RawResult);
callback(data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.