Advertisement
Guest User

Untitled

a guest
Mar 21st, 2016
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. using UnityEngine;
  2. using Facebook.Unity;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5.  
  6. public class FacebookScript : MonoBehaviour
  7. {
  8. IEnumerable<string> facebook_permissions = new string[] { "public_profile", "email", "user_friends", "publish_actions" };
  9.  
  10. // Use this for initialization
  11. void Awake()
  12. {
  13. FB.Init(SetInit, OnHideUnity);
  14. Debug.Log("FB Init done");
  15. }
  16.  
  17. private void SetInit()
  18. {
  19. if (FB.IsLoggedIn == true)
  20. {
  21. Debug.Log("Logged in");
  22. }
  23.  
  24. }
  25.  
  26. private void OnHideUnity(bool isGameShown)
  27. {
  28. if (isGameShown == false)
  29. {
  30. Time.timeScale = 0;
  31. }
  32.  
  33. else
  34. {
  35. Time.timeScale = 1;
  36. }
  37. }
  38.  
  39. public void FBLogin()
  40. {
  41. FB.LogInWithReadPermissions(facebook_permissions, AuthCallback);
  42.  
  43. }
  44.  
  45. private void AuthCallback(ILoginResult result)
  46. {
  47. if (FB.IsLoggedIn) // Logged in.
  48. {
  49. // AccessToken class will have session details
  50. var aToken = Facebook.Unity.AccessToken.CurrentAccessToken;
  51. // Print current access token's User ID
  52. Debug.Log(aToken.UserId);
  53. // Print current access token's granted permissions
  54. foreach (string perm in aToken.Permissions)
  55. {
  56. Debug.Log(perm);
  57. }
  58. APIPost();
  59. Share();
  60. }
  61.  
  62. else
  63. {
  64. Debug.Log("User cancelled login");
  65. }
  66. }
  67.  
  68. public void APIPost()
  69. {
  70. Dictionary<string, string> ScoreDictionary = new Dictionary<string, string>();
  71. ScoreDictionary.Add("score", PlayerPrefs.GetInt("Highscore", 0).ToString());
  72.  
  73. FB.API("/me/scores", HttpMethod.POST, POSTCallback, ScoreDictionary);
  74. }
  75. public void POSTCallback(IGraphResult result)
  76. {
  77. Debug.Log(result.Error);
  78. NPBinding.UI.ShowToast("POSTCallback Error: " + result.Error, VoxelBusters.NativePlugins.eToastMessageLength.LONG);
  79. }
  80.  
  81.  
  82.  
  83. public void Share()
  84. {
  85. FB.ShareLink(new System.Uri("https://play.google.com/store/apps/details?id=org.zewde.JumpyBall"),
  86. "I reached " + PlayerPrefs.GetInt("Highscore",0).ToString() + " on #JumpyBall. Can you beat me ? Try me!",
  87. "https://play.google.com/store/apps/details?id=org.zewde.JumpyBall",
  88. callback: ShareCallback);
  89. }
  90.  
  91. private void ShareCallback(IShareResult result)
  92. {
  93. if (result.Cancelled || !string.IsNullOrEmpty(result.Error))
  94. {
  95. Debug.Log("ShareLink Error: " + result.Error);
  96. }
  97. else if (!string.IsNullOrEmpty(result.PostId))
  98. {
  99. // Print post identifier of the shared content
  100. Debug.Log(result.PostId);
  101. }
  102. else
  103. {
  104. // Share succeeded without postID
  105. Debug.Log("ShareLink success!");
  106. }
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement