Created
December 14, 2015 08:18
-
-
Save congnt24/a15449b0ad296a77db0a to your computer and use it in GitHub Desktop.
Using FB sdk in unity to get Information, share, get and set score
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using UnityEngine.UI; | |
using Facebook.Unity; | |
using System.Collections.Generic; | |
using Facebook.MiniJSON; | |
public class FBController : MonoBehaviour { | |
Button[] btn = new Button[4]; | |
Image avatar; | |
Text txt_username; | |
// Use this for initialization | |
void Awake () { | |
Transform group = transform.FindChild ("ButtonGroup"); | |
avatar = transform.FindChild ("Img_Avatar").GetComponent<Image> (); | |
txt_username = transform.FindChild ("Txt_Username").GetComponent<Text> (); | |
int i = 0; | |
foreach (Transform item in group) { | |
btn [i++] = item.GetComponent<Button> (); | |
Debug.Log (""+btn[i-1]); | |
} | |
InitFB (); | |
} | |
void Start () | |
{ | |
btn[0].onClick.AddListener (()=>InitFB()); | |
btn[1].onClick.AddListener (()=>LoginFB()); | |
btn [2].onClick.AddListener (() => ShareWithFriend ()); | |
btn [3].onClick.AddListener (() => QueryScores ()); | |
} | |
void OnGUI () | |
{ | |
} | |
void InitFB () | |
{ | |
FB.Init (OnInitComplete, OnHideUnity); | |
} | |
void LoginFB () | |
{ | |
FB.LogInWithReadPermissions(new List<string>() { "public_profile", "email", "user_friends" }, this.HandleResult); | |
} | |
private void CallFBLoginForPublish() | |
{ | |
FB.LogInWithPublishPermissions(new List<string>() { "publish_actions" }, this.HandleResult); | |
} | |
void HandleResult (ILoginResult result) | |
{ | |
if (result == null) | |
{ | |
Debug.Log ("RESULT NULL"); | |
return; | |
} | |
// Some platforms return the empty string instead of null. | |
if (!string.IsNullOrEmpty(result.Error)) | |
{ | |
Debug.Log ("Error " +result.Error); | |
} | |
else if (result.Cancelled) | |
{ | |
Debug.Log ("Cancelled Response: " +result.RawResult); | |
} | |
else if (!string.IsNullOrEmpty(result.RawResult)) | |
{ | |
Debug.Log ("Success Response: " +result.RawResult); | |
DealWithCallBack (); | |
} | |
else | |
{ | |
Debug.Log ("Empty Response"); | |
} | |
} | |
void DealWithCallBack () | |
{ | |
if (FB.IsLoggedIn) { | |
FB.API ("/me/picture?type=square&height=128&width=128", HttpMethod.GET, GetPicture); | |
FB.API ("/me/?fields=id,first_name", HttpMethod.GET, GetUsername); | |
} | |
} | |
void GetPicture (IGraphResult result) | |
{ | |
if (result.Error == null) { | |
avatar.sprite = Sprite.Create (result.Texture, new Rect (0, 0, 128, 128), new Vector2 ()); | |
} | |
} | |
void GetUsername (IGraphResult result) | |
{ | |
if (result.Error != null) { | |
FB.API ("/me/?fields=id,first_name", HttpMethod.GET, GetUsername); | |
return; | |
} | |
var objs = Json.Deserialize (result.RawResult) as Dictionary<string, object>; | |
txt_username.text = "Hello, "+objs["first_name"]; | |
} | |
private void OnInitComplete() | |
{ | |
string logMessage = string.Format( | |
"OnInitCompleteCalled IsLoggedIn='{0}' IsInitialized='{1}'", | |
FB.IsLoggedIn, | |
FB.IsInitialized); | |
Debug.Log (logMessage); | |
} | |
private void OnHideUnity(bool isGameShown) | |
{ | |
string logMessage = string.Format("Success Response: OnHideUnity Called {0}\n", isGameShown); | |
Debug.Log (logMessage); | |
} | |
public void ShareWithFriend(){ | |
FB.FeedShare ("", null, "Name", "CAP", "DES", null, "", null); | |
} | |
// All Scores API related Things | |
public void QueryScores() | |
{ | |
FB.API ("/app/scores?fields=score,user.limit(30)", HttpMethod.GET, ScoresCallback); | |
} | |
private void ScoresCallback(IResult result) | |
{ | |
Debug.Log ("Scores callback: " + result.RawResult); | |
} | |
public void SetScore() | |
{ | |
var scoreData = new Dictionary<string,string> (); | |
scoreData ["score"] = Random.Range (10, 200).ToString (); | |
FB.API ("/me/scores", HttpMethod.POST, delegate(IGraphResult result) { | |
Debug.Log ("Score submit result: " + result.RawResult); | |
}, scoreData); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment