简体   繁体   English

Unity Facebook登录不适用于Android设备上的Facebook App

[英]Unity Facebook login not works on Facebook App in the Android device

So i implemented Facebook SDK in my project, here the problem is FBlogin not works when i have facebook app in my android device. 所以我在我的项目中实现了Facebook SDK,这里的问题是当我在我的Android设备上有facebook应用程序时,FBlogin不起作用。 Please my and my team stuck in this stuff. 请我和我的团队坚持这些东西。 For more details please replay to this post. 有关详细信息,请重播此帖。

Here is the Code of my Facebook. 这是我的Facebook代码。 It is working great when i uninstall facebook app in my device. 当我在我的设备中卸载Facebook应用程序时它工作得很好。

With facebook app in my android device : When open my game, facebook window is appear like do you want to continue insted of Login ask, when i click continue its not fetching my details like "Name and Profile Pic" 随着我的Android设备中的Facebook应用程序:当打开我的游戏时,Facebook窗口显示为你想要继续登录登录问,当我点击继续它没有取得我的详细信息,如“名称和配置文件Pic”

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Facebook.Unity;
using Facebook.MiniJSON;
using UnityEngine.UI;
using System.IO;
using System;

public class Login : MonoBehaviour
{
    public string fbname;
    public string get_data;
    public InputField login_name;
    public Image profilePic;
    public Text fbNameText,FriendsText;//BABJI
    public GameObject loginPanel,mainPanel;

    public bool loggedIn;
    void Awake ()
    {
        if (!FB.IsInitialized)
        {
            FB.Init(() =>
                {
                    if (FB.IsInitialized)
                        FB.ActivateApp();
                    else
                        Debug.LogError("Couldn't initialize");
                },
                isGameShown =>
                {
                    if (!isGameShown)
                        Time.timeScale = 0;
                    else
                        Time.timeScale = 1;
                });
        }
        else
            FB.ActivateApp();

    }

    void Start()
    {
        if (GameSaver.instance.isFBLogin) {
            loginPanel.SetActive (false);
            login_name.gameObject.SetActive (true);
            mainPanel.SetActive (true);
//          StartCoroutine (DelayLogIn ());
        } else {
            loginPanel.SetActive (true);
        }
    }

//  IEnumerator DelayLogIn()
//  {
//      yield return new WaitForSeconds (0.1f);
//
//  }
    private void InitCallback ()
    {
        if (FB.IsInitialized) {
            // Signal an app activation App Event
            FB.ActivateApp();
            // Continue with Facebook SDK
            // ...
        } else {
            Debug.Log("Failed to Initialize the Facebook SDK");
        }
    }
    private void OnHideUnity (bool isGameShown)
    {
        if (!isGameShown) {
            // Pause the game - we will need to hide
            Time.timeScale = 0;
        } else {
            // Resume the game - we're getting focus again
            Time.timeScale = 1;
        }
    }

    public void LoginCalled()
    {

        if (!FB.IsLoggedIn) 
        {
            var perms = new List<string> (){ "public_profile", "email" };
            FB.LogInWithReadPermissions (perms, AuthCallback);
        }
        else 
        {
            //

        }
        // you are already logged in, do something
        FB.API("me?fields=name", HttpMethod.GET, GetFacebookData);
        FB.API("me/picture?type=square&height=128&width=128", HttpMethod.GET, GetProfilePicture);
        login_name.gameObject.SetActive (true);
        mainPanel.SetActive (true);
        loginPanel.SetActive (false);
        GameSaver.instance.isFBLogin=true;
        GameSaver.instance.SaveGameData ();
    }

    private void AuthCallback (ILoginResult result) 
    {
        if (FB.IsLoggedIn) 
        {
            // AccessToken class will have session details
            var aToken = Facebook.Unity.AccessToken.CurrentAccessToken;
            // Print current access token's User ID
            Debug.Log(aToken.UserId);
            // Print current access token's granted permissions
            foreach (string perm in aToken.Permissions) 
            {
                Debug.Log(perm);
            }
            FB.API("me?fields=name", Facebook.Unity.HttpMethod.GET, GetFacebookData);
            //FB.API("/me/picture?redirect=false", HttpMethod.GET, GetProfilePicture);
            //BABJI
            FB.API("me/picture?type=square&height=128&width=128", HttpMethod.GET, GetProfilePicture);
            login_name.gameObject.SetActive (true);
            mainPanel.SetActive (true);
            loginPanel.SetActive (false);
            GameSaver.instance.isFBLogin=true;
            GameSaver.instance.SaveGameData ();
            //BABJI
            loggedIn = FB.IsLoggedIn;
        } 
        else 
        {
            Debug.Log("User cancelled login");
        }
    }

    void GetFacebookData(IResult result)
    {
        fbname = result.ResultDictionary["name"].ToString ();
        login_name.text = fbname ;
        login_name.gameObject.SetActive (true);
        fbNameText.text = fbname;
        Debug.Log("fbName: " + fbname);

    }

    private void GetProfilePicture(IGraphResult result)
    {
        if (result.Error == null && result.Texture != null)
        {       
            profilePic.sprite = Sprite.Create (result.Texture, new Rect (0, 0, 128, 128), new Vector2 ());
        }
    }

}

depending on what we have discussed above 取决于我们上面讨论的内容
try this code : 试试这段代码:

public void Awake()
{
        // Init Facebook SDK
        if (!FB.IsInitialized)
        {
            // Initialize the Facebook SDK Configuration
            FB.Init(InitCallback, OnHideUnity);
        }
        else
        {
             // Already initialized, signal an app activation App Event
             FB.ActivateApp();
        }
}

private void InitCallback() {
        if (FB.IsInitialized)
        {
            // Signal an app activation App Event
            FB.ActivateApp();
        }
        else
        {
            Debug.Log("Failed to Initialize the Facebook SDK");
        }
} 
private void AuthCallback(ILoginResult result)
{
        if (FB.IsLoggedIn)
        {
            // Here is the region where u r loggged in from facebook acccount
            // Just put your Logic Here
            // AccessToken class will have session details
            var aToken = Facebook.Unity.AccessToken.CurrentAccessToken;
        }
        else
        {
            Debug.Log("Facebook User cancelled login");
        }
}

and here is what you need to call when u press a button, 这是你按下按钮时需要打电话的,

public void LoginWithFaceBook()
{
        var perms = new List<string>() { "public_profile", "email" };
        FB.LogInWithReadPermissions(perms, AuthCallback);
}

also use this when u logout from your application 当你从你的应用程序注销时也使用它

public void FacebookLogout()
{
        if (FB.IsLoggedIn)
        {
            FB.LogOut();
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM