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; | |
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