using System.Collections.Generic; using UnityEngine; using Facebook.Unity; public class FacebookManager : MonoBehaviour { public bool LoggedIn; public static FacebookManager Instance; public delegate void LoginCall(bool success); public static event LoginCall OnLoginCall; public string UserID; public string FirstName; public string LastName; private void Awake() { Debug.LogError("FacebookManager"); if (!Instance) { Instance = this; DontDestroyOnLoad(gameObject); } else { Destroy(gameObject); } } public void InitFB() { FB.Init(OnInit); } void OnInit() { if (FB.IsLoggedIn) { FB.API("/me?fields=id,first_name,last_name", HttpMethod.GET, GetUserInfo); } LoggedIn = FB.IsLoggedIn; if (OnLoginCall != null) { OnLoginCall(LoggedIn); } } public void FBLogin() { FB.LogInWithReadPermissions(new List() { "public_profile", "email", "user_friends" }, AuthCallback); } void AuthCallback(IResult result) { if (result.Error == null) { if (FB.IsLoggedIn) { Debug.Log("FB Logged in"); FB.API("/me?fields=id,first_name,last_name", HttpMethod.GET, GetUserInfo); //FB.API("/me/picture?width=512&height=512", HttpMethod.GET, GetUserProfilePicture); } else { Debug.Log("FB Login failed"); } } else { Debug.Log(result.Error); } LoggedIn = FB.IsLoggedIn; if (OnLoginCall != null) { OnLoginCall(LoggedIn); } } void GetUserInfo(IResult result) { if (result.Error == null) { UserID = result.ResultDictionary["id"].ToString(); FirstName = result.ResultDictionary["first_name"].ToString(); LastName = result.ResultDictionary["last_name"].ToString(); Debug.Log("FaceBook Got User Data = " + FirstName + " " + LastName); } else { Debug.Log(result.Error); } } }