Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections;
- using System.Collections.Generic;
- public class FBholder : MonoBehaviour {
- public GameObject UIFBIsLoggedIn;
- public GameObject UIFBNotLoggedIn;
- public GameObject UIFBAvatar;
- public GameObject UIFBUserName;
- //private List<object> scoresList = null;
- //public GameObject ScoreEntryPanel;
- //public GameObject ScoreScrollList;
- public Text ScoreDebug;
- private List<object> scoresList = null;
- private Dictionary<string, string> profile = null;
- void Awake(){
- FB.Init (SetInit, OnHideUnity);
- }
- private void SetInit(){
- Debug.Log ("FB init done.");
- if (FB.IsLoggedIn) {
- Debug.Log ("FB loged in.");
- } else {
- //FBlogin();
- 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 fail");
- DealWithFBMenus(false);
- }
- }
- void DealWithFBMenus(bool isLoggedIn){
- if (isLoggedIn) {
- UIFBIsLoggedIn.SetActive(true);
- UIFBNotLoggedIn.SetActive(false);
- // get profile picture code
- FB.API(Util.GetPictureURL("me",128,128),Facebook.HttpMethod.GET, DealWithProfilePicture);
- // get username code
- FB.API("/me?fields=id,first_name", Facebook.HttpMethod.GET, DealWithUserName);
- } else {
- UIFBIsLoggedIn.SetActive(false);
- UIFBNotLoggedIn.SetActive(true);
- }
- }
- void DealWithProfilePicture(FBResult result){
- if (result.Error != null) {
- Debug.Log("problem with getting profile picture");
- FB.API(Util.GetPictureURL("me",128,128),Facebook.HttpMethod.GET, DealWithProfilePicture);
- return;
- }
- Image UserAvatar = UIFBAvatar.GetComponent<Image> ();
- UserAvatar.sprite = Sprite.Create (result.Texture, new Rect(0,0,128,128), new Vector2(0,0));
- }
- void DealWithUserName(FBResult result){
- if (result.Error != null) {
- Debug.Log("problem with getting username");
- FB.API("/me?fields=id,first_name", Facebook.HttpMethod.GET, DealWithUserName);
- return;
- }
- profile = Util.DeserializeJSONProfile (result.Text);
- Text UserMsg = UIFBUserName.GetComponent<Text> ();
- UserMsg.text = "Hello, " + profile["first_name"];
- }
- public void ShareWithFriends(){
- Debug.Log ("Share");
- /*
- FB.Feed (
- linkCaption:"Day la doan mo ta",
- picture:"http://babyweb.vn/images/baby-hd.png",
- linkName:"Chac day la tieu de",
- link:"http://unity2d.vn/index.php"
- );
- */
- FB.Feed (
- linkCaption:"I'm playing this awesome game.",
- picture:"http://babyweb.vn/images/baby-hd.png",
- linkName:"Check out this game.",
- link:"http://apps.facebook.com/" + FB.AppId + "challenge_brag=" + (FB.IsLoggedIn ? FB.UserId : "guest")
- );
- }
- public void InviteFriends(){
- Debug.Log ("Invite");
- FB.AppRequest(
- message: "This game is awesome, join me, now.",
- title:"Invite your friends to join you"
- );
- }
- public void QueryScores(){
- Debug.Log ("Query Score");
- FB.API ("/app/scores?fields=score,user.limit(30)", Facebook.HttpMethod.GET, ScoresCallback);
- }
- private void ScoresCallback(FBResult result){
- Debug.Log ("Scores Callback " + result.Text);
- //ScoreDebug.text = result.Text;
- ScoreDebug.text = "";
- scoresList = Util.DeserializeScores(result.Text);
- foreach (object score in scoresList) {
- var entry = (Dictionary<string,object>) score;
- var user = (Dictionary<string,object>) entry["user"];
- ScoreDebug.text = ScoreDebug.text + "UN: " + user["name"] + " - " + entry["score"] + ", ";
- }
- }
- public void SetScore(){
- var scoreData = new Dictionary<string,string> ();
- scoreData ["score"] = Random.Range (10, 200).ToString ();
- FB.API ("/me/scores", Facebook.HttpMethod.POST, delegate(FBResult result) {
- Debug.Log ("Score submit result: " + result.Text);
- }, scoreData);
- }
- // All Scores API related Things
- /*
- public void QueryScores()
- {
- FB.API ("/app/scores?fields=score,user.limit(30)", Facebook.HttpMethod.GET, ScoresCallback);
- }
- private 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;
- 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) // if there was an error
- {
- Debug.Log (pictureResult.Error);
- }
- else // if everything was fine
- {
- UserAvatar.sprite = Sprite.Create (pictureResult.Texture, new Rect(0,0,128,128), new Vector2(0,0));
- }
- });
- }
- }
- public void SetScore()
- {
- var scoreData = new Dictionary<string,string> ();
- scoreData ["score"] = Random.Range (10, 200).ToString ();
- FB.API ("/me/scores", Facebook.HttpMethod.POST, delegate(FBResult result) {
- Debug.Log ("Score submit result: " + result.Text);
- }, scoreData);
- }
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment