Search Unity

About Facebook score posting, or friends score showing.

Discussion in 'Scripting' started by BrokenhearT, Jul 7, 2015.

  1. BrokenhearT

    BrokenhearT

    Joined:
    May 18, 2015
    Posts:
    69
    Hello everyone, this is actually driving me insane, when I use it on my own device and hit my score and friends score it shows my score as it is in game, but for others it doesn't show their scores it shows 0, on my other Facebook account or my sister's Facebook account it shows that she has 0 score even though she's got more than that, here's the score script and the FB manager script.

    Code (CSharp):
    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 UIFBIsNotLoggedIn;
    10.     public GameObject UIFBAvatar;
    11.     public GameObject UIFUserName;
    12.    
    13.     private List<object> scoresList = null;
    14.    
    15.     public GameObject ScoreEntryPanel;
    16.     public GameObject ScoreScrollList;
    17.    
    18.     public static int score;
    19.    
    20.     private Dictionary<string, string> profile = null;
    21.    
    22.     void Awake()
    23.     {
    24.         FB.Init (SetInit, OnHideUnity);
    25.     }
    26.    
    27.     void Start()
    28.     {
    29.         score = PlayerPrefs.GetInt("CurrentPlayerScore");
    30.     }
    31.    
    32.    
    33.     private void SetInit()
    34.     {
    35.         Debug.Log ("FB Init done.");
    36.        
    37.         if(FB.IsLoggedIn)
    38.         {
    39.             DealWithFBMenus(true);
    40.             Debug.Log ("FB Logged In");
    41.         }
    42.         else
    43.         {
    44.             DealWithFBMenus(false);
    45.         }
    46.        
    47.     }
    48.    
    49.     private void OnHideUnity(bool isGameShown)
    50.     {
    51.        
    52.         if(!isGameShown)
    53.         {
    54.             Time.timeScale =0;
    55.         }
    56.         else
    57.         {
    58.             Time.timeScale =1;
    59.         }
    60.        
    61.        
    62.     }
    63.    
    64.     public void FBlogin()
    65.     {
    66.         FB.Login ("email,publish_actions", AuthCallback);
    67.     }
    68.    
    69.     void AuthCallback (FBResult result)
    70.     {
    71.         if (FB.IsLoggedIn)
    72.         {
    73.             Debug.Log ("FB Login worked!");
    74.             DealWithFBMenus(true);
    75.         }
    76.         else
    77.         {
    78.             Debug.Log ("Fb Login fail");
    79.             DealWithFBMenus(false);
    80.         }
    81.     }
    82.    
    83.     void DealWithFBMenus (bool isLoggedIn)
    84.     {
    85.         if(isLoggedIn)
    86.         {
    87.             UIFBIsLoggedIn.SetActive(true);
    88.             UIFBIsNotLoggedIn.SetActive(false);
    89.            
    90.             FB.API (Util.GetPictureURL("me", 128, 128), Facebook.HttpMethod.GET, DealWithProfilePicture);
    91.             FB.API ("/me?field=id,first_name",Facebook.HttpMethod.GET, DealWiththeUserName);
    92.         }
    93.         else
    94.         {
    95.             UIFBIsLoggedIn.SetActive(false);
    96.             UIFBIsNotLoggedIn.SetActive(true);
    97.         }
    98.     }
    99.    
    100.     void DealWithProfilePicture(FBResult result)
    101.     {
    102.         if (result.Error != null)
    103.         {
    104.             Debug.Log ("Problem with getting profile picture");
    105.            
    106.             FB.API (Util.GetPictureURL("me", 128, 128), Facebook.HttpMethod.GET, DealWithProfilePicture);
    107.             return;
    108.         }
    109.        
    110.         Image UserAvatar = UIFBAvatar.GetComponent<Image>();
    111.         UserAvatar.sprite = Sprite.Create (result.Texture, new Rect (0,0,128, 128), new Vector2 (0,0));
    112.     }
    113.    
    114.     void DealWiththeUserName (FBResult result)
    115.     {
    116.         if (result.Error != null)
    117.         {
    118.             Debug.Log ("Problem with getting user name");
    119.            
    120.             FB.API ("/me?field=id,first_name",Facebook.HttpMethod.GET, DealWiththeUserName);
    121.             return;
    122.         }
    123.        
    124.         profile = Util.DeserializeJSONProfile(result.Text);
    125.        
    126.         Text UserMsg = UIFUserName.GetComponent<Text>();
    127.        
    128.         UserMsg.text = "Hello, " + profile["first_name"];
    129.     }
    130.    
    131.     public void ShareWithFriends()
    132.     {
    133.         FB.Feed (
    134.             linkCaption: "I'm Playing this awesome game check it out!",
    135.             picture: "http://stormyx.com/MyGameIcon.png",
    136.             linkName: "Come join me in this game now!",
    137.             link: "http://app.facebook.com/" + FB.AppId + "/?challenge_brag=" + (FB.IsLoggedIn ? FB.UserId : "guest")
    138.            
    139.            
    140.             );
    141.     }
    142.    
    143.     public void InviteFriends()
    144.     {
    145.         FB.AppRequest
    146.             (
    147.                 message: "This game is Fantastic!, join me now!",
    148.                 title: "Invite your friends to join you"
    149.                 );
    150.     }
    151.    
    152.     public void FriendsScore()
    153.     {
    154.         FB.API ("/app/scores?fields=score,user.limit(30)", Facebook.HttpMethod.GET,ScoresCallback);
    155.     }
    156.    
    157.     private void ScoresCallback (FBResult result)
    158.     {
    159.         Debug.Log ("Scores callback: " + result.Text);
    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.             var entry = (Dictionary<string,object>) score;
    171.             var user = (Dictionary<string,object>) entry ["user"];
    172.            
    173.            
    174.             GameObject ScorePanel;
    175.             ScorePanel = Instantiate (ScoreEntryPanel) as GameObject;
    176.             ScorePanel.transform.parent = ScoreScrollList.transform;
    177.            
    178.             Transform ThisScoreName = ScorePanel.transform.Find ("FriendName");
    179.             Transform ThisScoreScore = ScorePanel.transform.Find ("FriendScore");
    180.             Text ScoreName = ThisScoreName.GetComponent<Text>();
    181.             Text ScoreScore = ThisScoreScore.GetComponent<Text>();
    182.            
    183.            
    184.             ScoreName.text = user["name"].ToString();
    185.             ScoreScore.text = entry["score"].ToString();
    186.            
    187.             ScoreName.text = user["name"].ToString();
    188.             ScoreScore.text = entry["score"].ToString();
    189.            
    190.             Transform TheUserAvatar = ScorePanel.transform.Find ("FriendAvatar");
    191.             Image UserAvatar = TheUserAvatar.GetComponent<Image>();
    192.            
    193.             FB.API (Util.GetPictureURL(user["id"].ToString(), 128,128), Facebook.HttpMethod.GET,delegate(FBResult pictureResult)
    194.                     {
    195.                 if(pictureResult.Error != null)
    196.                 {
    197.                     Debug.Log (pictureResult.Error);
    198.                 }
    199.                 else
    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.     public void MyScore()
    210.     {
    211.         var scoreData = new Dictionary<string,string>();
    212.         scoreData ["score"] = (score).ToString();
    213.         FB.API ("/me/scores", Facebook.HttpMethod.POST, delegate(FBResult result)
    214.                 {
    215.             Debug.Log ("Score submit result: " + result.Text);
    216.         }, scoreData);
    217.     }
    218.    
    219.     public void StartGame ()
    220.     {
    221.         PlayerPrefs.SetInt("CurrentPlayerScore",0);
    222.         Application.LoadLevel (1);
    223.     }
    224.    
    225.     public void Quit ()
    226.     {
    227.         Application.Quit ();
    228.     }
    229.    
    230.    
    231.    
    232. }
    233.  


    and this is the score manager script.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class ScoreManager : MonoBehaviour {
    6.  
    7.     public static int score;
    8.  
    9.     Text scoretext;
    10.  
    11.     void Start ()
    12.     {
    13.         scoretext = GetComponent<Text> () ;
    14.         //score = 0;
    15.  
    16.         score = PlayerPrefs.GetInt("CurrentPlayerScore");
    17.     }
    18.    
    19.     void Update ()
    20.     {
    21.         if (score <0)
    22.             score = 0;
    23.         scoretext.text = "" + score;
    24.     }
    25.  
    26.     public static void AddPoints (int pointsToAdd)
    27.     {
    28.         score +=pointsToAdd;
    29.         PlayerPrefs.SetInt ("CurrentPlayerScore", score);
    30.     }
    31.  
    32.     public static void Rest()
    33.     {
    34.         score = 0;
    35.         PlayerPrefs.SetInt("CurrentPlayerScore", score);
    36.     }
    37. }
    38.