thieumao

FBholder.cs - Facebook SDK Demo 2

Aug 19th, 2015
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.04 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5.  
  6. public class FBholder : MonoBehaviour {
  7.  
  8.     public GameObject UIFBIsLoggedIn;
  9.     public GameObject UIFBNotLoggedIn;
  10.     public GameObject UIFBAvatar;
  11.     public GameObject UIFBUserName;
  12.  
  13.     //private List<object> scoresList = null;
  14.    
  15.     //public GameObject ScoreEntryPanel;
  16.     //public GameObject ScoreScrollList;
  17.     public Text ScoreDebug;
  18.     private List<object> scoresList = null;
  19.  
  20.     private Dictionary<string, string> profile = null;
  21.  
  22.     void Awake(){
  23.         FB.Init (SetInit, OnHideUnity);
  24.     }
  25.  
  26.     private void SetInit(){
  27.         Debug.Log ("FB init done.");
  28.  
  29.         if (FB.IsLoggedIn) {
  30.             Debug.Log ("FB loged in.");
  31.         } else {
  32.             //FBlogin();
  33.             DealWithFBMenus(false);
  34.         }
  35.     }
  36.  
  37.     private void OnHideUnity(bool isGameShown){
  38.         if (!isGameShown) {
  39.             Time.timeScale = 0;
  40.         } else {   
  41.             Time.timeScale = 1;
  42.         }
  43.        
  44.     }
  45.  
  46.     public void FBlogin(){
  47.         FB.Login ("email,publish_actions", AuthCallback);
  48.     }
  49.  
  50.     void AuthCallback(FBResult result){
  51.         if (FB.IsLoggedIn) {
  52.             Debug.Log ("FB login worked!");
  53.             DealWithFBMenus(true);
  54.         } else {
  55.             Debug.Log ("FB login fail");
  56.             DealWithFBMenus(false);
  57.         }
  58.     }
  59.  
  60.     void DealWithFBMenus(bool isLoggedIn){
  61.         if (isLoggedIn) {
  62.             UIFBIsLoggedIn.SetActive(true);
  63.             UIFBNotLoggedIn.SetActive(false);
  64.  
  65.             // get profile picture code
  66.             FB.API(Util.GetPictureURL("me",128,128),Facebook.HttpMethod.GET, DealWithProfilePicture);
  67.             // get username code
  68.             FB.API("/me?fields=id,first_name", Facebook.HttpMethod.GET, DealWithUserName);
  69.         } else {
  70.             UIFBIsLoggedIn.SetActive(false);
  71.             UIFBNotLoggedIn.SetActive(true);
  72.         }
  73.  
  74.     }
  75.  
  76.     void DealWithProfilePicture(FBResult result){
  77.         if (result.Error != null) {
  78.             Debug.Log("problem with getting profile picture");
  79.             FB.API(Util.GetPictureURL("me",128,128),Facebook.HttpMethod.GET, DealWithProfilePicture);
  80.             return;
  81.         }
  82.  
  83.         Image UserAvatar = UIFBAvatar.GetComponent<Image> ();
  84.         UserAvatar.sprite = Sprite.Create (result.Texture, new Rect(0,0,128,128), new Vector2(0,0));
  85.     }
  86.  
  87.     void DealWithUserName(FBResult result){
  88.         if (result.Error != null) {
  89.             Debug.Log("problem with getting username");
  90.             FB.API("/me?fields=id,first_name", Facebook.HttpMethod.GET, DealWithUserName);
  91.             return;    
  92.         }
  93.         profile = Util.DeserializeJSONProfile (result.Text);
  94.  
  95.         Text UserMsg = UIFBUserName.GetComponent<Text> ();
  96.         UserMsg.text = "Hello, " + profile["first_name"];
  97.     }
  98.  
  99.     public void ShareWithFriends(){
  100.         Debug.Log ("Share");
  101.         /*
  102.         FB.Feed (
  103.             linkCaption:"Day la doan mo ta",
  104.             picture:"http://babyweb.vn/images/baby-hd.png",
  105.             linkName:"Chac day la tieu de",
  106.             link:"http://unity2d.vn/index.php"
  107.         );
  108.         */
  109.         FB.Feed (
  110.             linkCaption:"I'm playing this awesome game.",
  111.             picture:"http://babyweb.vn/images/baby-hd.png",
  112.             linkName:"Check out this game.",
  113.             link:"http://apps.facebook.com/" + FB.AppId + "challenge_brag=" + (FB.IsLoggedIn ? FB.UserId : "guest")
  114.         );
  115.     }
  116.  
  117.     public void InviteFriends(){
  118.         Debug.Log ("Invite");
  119.         FB.AppRequest(
  120.             message: "This game is awesome, join me, now.",
  121.             title:"Invite your friends to join you"
  122.         );
  123.     }
  124.  
  125.     public void QueryScores(){
  126.         Debug.Log ("Query Score");
  127.         FB.API ("/app/scores?fields=score,user.limit(30)", Facebook.HttpMethod.GET, ScoresCallback);
  128.     }
  129.  
  130.     private void ScoresCallback(FBResult result){
  131.         Debug.Log ("Scores Callback " + result.Text);
  132.         //ScoreDebug.text = result.Text;
  133.         ScoreDebug.text = "";
  134.         scoresList = Util.DeserializeScores(result.Text);
  135.         foreach (object score in scoresList) {
  136.             var entry = (Dictionary<string,object>) score;
  137.             var user = (Dictionary<string,object>) entry["user"];
  138.             ScoreDebug.text = ScoreDebug.text + "UN: " + user["name"] + " - " + entry["score"] + ", ";
  139.         }
  140.     }
  141.  
  142.     public void SetScore(){
  143.         var scoreData = new Dictionary<string,string> ();
  144.         scoreData ["score"] = Random.Range (10, 200).ToString ();
  145.         FB.API ("/me/scores", Facebook.HttpMethod.POST, delegate(FBResult result) {
  146.             Debug.Log ("Score submit result: " + result.Text);
  147.         }, scoreData);
  148.     }
  149.     // All Scores API related Things
  150. /* 
  151.     public void QueryScores()
  152.     {
  153.         FB.API ("/app/scores?fields=score,user.limit(30)", Facebook.HttpMethod.GET, ScoresCallback);
  154.     }
  155.    
  156.     private void ScoresCallback(FBResult result)
  157.     {
  158.         Debug.Log ("Scores callback: " + result.Text);
  159.        
  160.        
  161.         scoresList = Util.DeserializeScores (result.Text);
  162.        
  163.         foreach (Transform child in ScoreScrollList.transform)
  164.         {
  165.             GameObject.Destroy (child.gameObject);
  166.         }
  167.        
  168.         foreach (object score in scoresList)
  169.         {
  170.            
  171.             var entry = (Dictionary<string,object>) score;
  172.             var user = (Dictionary<string,object>) entry["user"];
  173.            
  174.            
  175.            
  176.            
  177.             GameObject ScorePanel;
  178.             ScorePanel = Instantiate (ScoreEntryPanel) as GameObject;
  179.             ScorePanel.transform.parent = ScoreScrollList.transform;
  180.            
  181.             Transform ThisScoreName = ScorePanel.transform.Find ("FriendName");
  182.             Transform ThisScoreScore = ScorePanel.transform.Find ("FriendScore");
  183.             Text ScoreName = ThisScoreName.GetComponent<Text>();
  184.             Text ScoreScore = ThisScoreScore.GetComponent<Text>();
  185.            
  186.             ScoreName.text = user["name"].ToString();
  187.             ScoreScore.text = entry["score"].ToString();
  188.            
  189.             Transform TheUserAvatar = ScorePanel.transform.Find ("FriendAvatar");
  190.             Image UserAvatar = TheUserAvatar.GetComponent<Image>();
  191.            
  192.            
  193.             FB.API (Util.GetPictureURL(user["id"].ToString (), 128,128), Facebook.HttpMethod.GET, delegate(FBResult pictureResult){
  194.                
  195.                 if(pictureResult.Error != null) // if there was an error
  196.                 {
  197.                     Debug.Log (pictureResult.Error);
  198.                 }
  199.                 else // if everything was fine
  200.                 {
  201.                     UserAvatar.sprite = Sprite.Create (pictureResult.Texture, new Rect(0,0,128,128), new Vector2(0,0));
  202.                 }
  203.                
  204.             });
  205.            
  206.            
  207.            
  208.         }
  209.        
  210.        
  211.     }
  212.    
  213.     public void SetScore()
  214.     {
  215.         var scoreData = new Dictionary<string,string> ();
  216.         scoreData ["score"] = Random.Range (10, 200).ToString ();
  217.         FB.API ("/me/scores", Facebook.HttpMethod.POST, delegate(FBResult result) {
  218.             Debug.Log ("Score submit result: " + result.Text);
  219.         }, scoreData);
  220.     }
  221.     */
  222. }
Advertisement
Add Comment
Please, Sign In to add comment