Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Logged Out Of Facebook, Android Game Still Logged In

Discussion in 'Scripting' started by Beyerdynamic, Nov 22, 2014.

  1. Beyerdynamic

    Beyerdynamic

    Joined:
    Dec 9, 2012
    Posts:
    27
    Hi guys,

    My problem is when I load my game I can connect to Facebook by it's log in page, that side works fine. I made it like Candy Crush where the user will have to open the Facebook app to log out. Again this works fine, but my game still thinks Facebook is logged in.

    My script below shows what I've done so far:
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using Facebook.MiniJSON;
    6. using Facebook;
    7. using System.Linq;
    8. using System;
    9.  
    10. public class SignIn : MonoBehaviour {
    11.  
    12.     public Texture2D aTexture;
    13.     public Texture2D Login;
    14.     public Texture2D LogOut;
    15.     public Texture2D closeTexture;
    16.  
    17.     public GUIStyle buttonsStyle;
    18.     public GUIStyle boxStyle;
    19.     public GUIStyle titleFont;
    20.     public GUIStyle versionFont;
    21.     public GUIStyle closeStyle;
    22.     public GUIStyle messageStyle;
    23.  
    24.     public static bool showAbout = false;
    25.    
    26.     public StartNow startNOW;
    27.     public ContinueExit ContExit;
    28.  
    29.     private string[] facebookArray = new string[] { "" +
    30.         "Login to facebook and share\n" +
    31.         "your rex rampage achievements.\n\n" +
    32.         "Why not ask them to join in\n" +
    33.         "the fun and see if they can\n" +
    34.         "beat your timings."};
    35.    
    36.     void Awake() {
    37.  
    38.         // Initialize FB SDK            
    39.         enabled = false;                
    40.         FB.Init(SetInit, OnHideUnity);
    41.  
    42.         startNOW = GetComponent<StartNow> ();
    43.         ContExit = GetComponent<ContinueExit> ();
    44.  
    45.     }
    46.  
    47.     private void SetInit()                                                                      
    48.     {                                                                                          
    49.  
    50.         Debug.Log("SetInit");                                                                
    51.         enabled = true; // "enabled" is a property inherited from MonoBehaviour                
    52.         if (FB.IsLoggedIn)                                                                      
    53.         {                                                                                      
    54.             Debug.Log("Already logged in");                                                  
    55.             OnLoggedIn();                                                                      
    56.         }
    57.  
    58.     }
    59.  
    60.     void Update() {
    61.        
    62.         if(CFInput.GetKeyDown (KeyCode.F)) {
    63.            
    64.             showAbout = true;
    65.             startNOW.enabled = false;
    66.             ContExit.enabled = false;
    67.            
    68.         }
    69.        
    70.     }
    71.    
    72.     void OnGUI() {
    73.  
    74.         if(showAbout) {
    75.  
    76.             GUI.depth = -3;
    77.            
    78.             int groupWidth = 768;
    79.             int groupHeight = 600;
    80.            
    81.             int screenWidth = Screen.width;
    82.             int screenHeight = Screen.height;
    83.            
    84.             int groupX = ( screenWidth - groupWidth ) / 2;
    85.             int groupY = ( screenHeight - groupHeight ) / 2;
    86.            
    87.             GUI.BeginGroup(new Rect( groupX, groupY, groupWidth, groupHeight ) );
    88.             GUI.Box(new Rect( 0, 0, groupWidth, groupHeight ), aTexture, boxStyle );
    89.  
    90.             GUI.Label (new Rect(180, 110, 60, 0), "CONNECT TO FACEBOOK", titleFont);
    91.  
    92.             GUI.Label (new Rect(180, 220, 60, 0), facebookArray[0], messageStyle);
    93.  
    94.             if (!FB.IsLoggedIn)                                                                                            
    95.             {
    96.  
    97.                 if(GUI.Button (new Rect(300, 470, 170, 210), Login, buttonsStyle) ) {
    98.  
    99.                     FB.Login("email,publish_actions", LoginCallback);
    100.                     OnHideUnity(false);
    101.  
    102.                 }
    103.  
    104.             }
    105.  
    106.             if (FB.IsLoggedIn) {
    107.                
    108.                 //if(GUI.Button (new Rect(300, 470, 170, 210), LogOut, buttonsStyle) ) {
    109.                    
    110.                     //OnHideUnity(false);
    111.                     //OnLoggedOut
    112.                     GUI.Label (new Rect(180, 330, 60, 0),
    113.                                    "YOU ARE ALREADY LOGGED IN TO\n" +
    114.                                    "FACEBOOK. TO LOG OUT PLEASE\n" +
    115.                                    "USE YOUR FACEBOOK APP.", messageStyle);
    116.                    
    117.                 //}
    118.                
    119.             }
    120.  
    121.             if(GUI.Button (new Rect(670, 40, 50, 50), closeTexture, closeStyle) ) {
    122.                
    123.                 toggleMenu();
    124.                
    125.             }
    126.            
    127.             GUI.EndGroup();
    128.        
    129.         }
    130.        
    131.     }
    132.  
    133.     private static void OnHideUnity(bool isGameShown)                                                  
    134.     {                                                                                          
    135.         Debug.Log("OnHideUnity");                                                            
    136.         if (!isGameShown)                                                                      
    137.         {                                                                                      
    138.             // pause the game - we will need to hide                                            
    139.             Time.timeScale = 0;                                                                
    140.         }                                                                                      
    141.         else                                                                                    
    142.         {                                                                                      
    143.             // start the game back up - we're getting focus again                              
    144.             Time.timeScale = 1;                                                                
    145.         }                                                                                      
    146.     }
    147.  
    148.     void LoginCallback(FBResult result)                                                      
    149.     {                                                                                        
    150.         Debug.Log("LoginCallback");                                                        
    151.        
    152.         if (FB.IsLoggedIn)                                                                    
    153.         {                                                                                    
    154.             OnLoggedIn();                                                                    
    155.         }                                                                                    
    156.     }                                                                                        
    157.    
    158.     void OnLoggedIn()                                                                        
    159.     {                                                                                        
    160.         Debug.Log("Logged in. ID: " + FB.UserId);
    161.         OnHideUnity(true);
    162.     }                                                                                      
    163.  
    164.     /*public static void OnLoggedOut()                                                                        
    165.     {                                                                                        
    166.         FB.Logout();
    167.         Debug.Log("Logged out. ID: " + FB.UserId);
    168.         OnHideUnity(true);
    169.     }*/
    170.  
    171.     void toggleMenu() {
    172.        
    173.         showAbout = false;
    174.         startNOW.enabled = true;
    175.         ContExit.enabled = true;
    176.        
    177.     }
    178.  
    179. }
    180.  
    As you may noticed I have the code for the button version but after reading some threads I'm trying to steer clear of that and go for the logging out of Facebook instead.

    My only guess is FB.IsLoggedIn is still thinking i'm logged in becuase my game session is not cleared?