FacebookManager.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using Facebook.Unity;
  4. public class FacebookManager : MonoBehaviour
  5. {
  6. public bool LoggedIn;
  7. public static FacebookManager Instance;
  8. public delegate void LoginCall(bool success);
  9. public static event LoginCall OnLoginCall;
  10. public string UserID;
  11. public string FirstName;
  12. public string LastName;
  13. private void Awake()
  14. {
  15. Debug.LogError("FacebookManager");
  16. if (!Instance)
  17. {
  18. Instance = this;
  19. DontDestroyOnLoad(gameObject);
  20. }
  21. else
  22. {
  23. Destroy(gameObject);
  24. }
  25. }
  26. public void InitFB()
  27. {
  28. FB.Init(OnInit);
  29. }
  30. void OnInit()
  31. {
  32. if (FB.IsLoggedIn)
  33. {
  34. FB.API("/me?fields=id,first_name,last_name", HttpMethod.GET, GetUserInfo);
  35. }
  36. LoggedIn = FB.IsLoggedIn;
  37. if (OnLoginCall != null)
  38. {
  39. OnLoginCall(LoggedIn);
  40. }
  41. }
  42. public void FBLogin()
  43. {
  44. FB.LogInWithReadPermissions(new List<string>() { "public_profile", "email", "user_friends" }, AuthCallback);
  45. }
  46. void AuthCallback(IResult result)
  47. {
  48. if (result.Error == null)
  49. {
  50. if (FB.IsLoggedIn)
  51. {
  52. Debug.Log("FB Logged in");
  53. FB.API("/me?fields=id,first_name,last_name", HttpMethod.GET, GetUserInfo);
  54. //FB.API("/me/picture?width=512&height=512", HttpMethod.GET, GetUserProfilePicture);
  55. }
  56. else
  57. {
  58. Debug.Log("FB Login failed");
  59. }
  60. }
  61. else
  62. {
  63. Debug.Log(result.Error);
  64. }
  65. LoggedIn = FB.IsLoggedIn;
  66. if (OnLoginCall != null)
  67. {
  68. OnLoginCall(LoggedIn);
  69. }
  70. }
  71. void GetUserInfo(IResult result)
  72. {
  73. if (result.Error == null)
  74. {
  75. UserID = result.ResultDictionary["id"].ToString();
  76. FirstName = result.ResultDictionary["first_name"].ToString();
  77. LastName = result.ResultDictionary["last_name"].ToString();
  78. Debug.Log("FaceBook Got User Data = " + FirstName + " " + LastName);
  79. }
  80. else
  81. {
  82. Debug.Log(result.Error);
  83. }
  84. }
  85. }