Guest User

Untitled

a guest
Mar 13th, 2014
239
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. using UnityEngine;
  4. using Prime31;
  5. using System.Collections.Generic;
  6.  
  7. namespace HutongGames.PlayMaker.Actions
  8. {
  9. [ActionCategory("Social/Facebook")]
  10. [Tooltip("Logs a user into Facebook with write permissions. You can see there facebook info and post on their wall")]
  11. public class RequestWrite : FsmStateAction
  12. {
  13. [RequiredField]
  14. [Tooltip("What event to call on success")]
  15. public FsmEvent successEvent;
  16.  
  17. [Tooltip("What event to call on error")]
  18. public FsmEvent errorEvent;
  19.  
  20. [Tooltip("Already Logged In")]
  21. public FsmEvent alreadyLoggedEvent;
  22.  
  23.  
  24. public override void OnExit() {
  25. #if UNITY_ANDROID || UNITY_IOS
  26. FacebookManager.reauthorizationFailedEvent -= reauthorizationFailedEvent;
  27. FacebookManager.reauthorizationSucceededEvent -= reauthorizationSucceededEvent;
  28. #endif
  29. }
  30.  
  31. public override void OnEnter()
  32. {
  33. #if UNITY_ANDROID || UNITY_IOS
  34. FacebookManager.reauthorizationFailedEvent += reauthorizationFailedEvent;
  35. FacebookManager.reauthorizationSucceededEvent += reauthorizationSucceededEvent;
  36. #endif
  37.  
  38. var hasWritePermissions = FsmVariables.GlobalVariables.GetFsmBool("hasWritePermissions").Value;
  39.  
  40. var hasReadPermissions = FsmVariables.GlobalVariables.GetFsmBool("hasReadPermissions").Value;
  41. if(!hasReadPermissions) {
  42. Debug.LogWarning("You must have read permissions to call this function");
  43. Finish();
  44. return;
  45. }
  46.  
  47. if(hasWritePermissions == false)
  48. {
  49. var permissions = new string[] { "publish_actions", "publish_stream" };
  50.  
  51. #if UNITY_ANDROID
  52. FacebookAndroid.reauthorizeWithPublishPermissions( permissions, FacebookSessionDefaultAudience.Everyone );
  53. #elif UNITY_IOS
  54. FacebookBinding.reauthorizeWithPublishPermissions( permissions, FacebookSessionDefaultAudience.Everyone );
  55. #endif
  56. }
  57. else {
  58. Fsm.Event(alreadyLoggedEvent);
  59. }
  60.  
  61. #if UNITY_EDITOR
  62. Finish();
  63. #endif
  64. }
  65.  
  66.  
  67. #if UNITY_ANDROID || UNITY_IOS
  68. void reauthorizationSucceededEvent()
  69. {
  70. Debug.Log ("reauthorizationSucceededEvent");
  71. var globalVariables = FsmVariables.GlobalVariables;
  72. var hasWrite = globalVariables.GetFsmBool("hasWritePermissions");
  73. hasWrite.Value = true;
  74.  
  75. Fsm.Event(successEvent);
  76. Finish();
  77. }
  78.  
  79.  
  80. void reauthorizationFailedEvent( P31Error error )
  81. {
  82. Debug.Log ("loginFailedEvent write");
  83. var globalVariables = FsmVariables.GlobalVariables;
  84. var hasWrite = globalVariables.GetFsmBool("hasWritePermissions");
  85. hasWrite.Value = false;
  86. var fsmError = globalVariables.GetFsmString("SocialError");
  87. fsmError.Value = error.ToString();
  88.  
  89. Fsm.Event(errorEvent);
  90. Finish();
  91. }
  92. #endif
  93.  
  94. }
  95. }
RAW Paste Data