123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- 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<string>() { "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);
- }
- }
- }
|