Advertisement
Guest User

Untitled

a guest
Mar 13th, 2014
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.58 KB | None | 0 0
  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_IOS
  26.             FacebookManager.reauthorizationFailedEvent -= reauthorizationFailedEvent;
  27.             FacebookManager.reauthorizationSucceededEvent -= reauthorizationSucceededEvent;
  28. #endif
  29.         }
  30.  
  31.         public override void OnEnter()
  32.         {
  33.  
  34. #if UNITY_IOS
  35.             FacebookManager.reauthorizationFailedEvent += reauthorizationFailedEvent;
  36.             FacebookManager.reauthorizationSucceededEvent += reauthorizationSucceededEvent;
  37.             Debug.Log ("Logging into Facebook with write permissions");
  38.            
  39.             var hasWritePermissions = FsmVariables.GlobalVariables.GetFsmBool("hasWritePermissions").Value;
  40.  
  41.             var hasReadPermissions = FsmVariables.GlobalVariables.GetFsmBool("hasReadPermissions").Value;
  42.             if(!hasReadPermissions) {
  43.                 Debug.LogWarning("You must have read permissions to call this function");
  44.                 Finish();
  45.                 return;
  46.             }
  47.  
  48.             if(hasWritePermissions == false)
  49.             {
  50.                 Debug.Log ("Requesting write permissions");
  51.                 var permissions = new string[] { "publish_actions", "publish_stream" };
  52.                 FacebookBinding.reauthorizeWithPublishPermissions( permissions, FacebookSessionDefaultAudience.Everyone);
  53.             }
  54.             else {
  55.                 Debug.Log ("Already hass write permissions");
  56.                 Fsm.Event(alreadyLoggedEvent);
  57.             }
  58. #endif
  59.  
  60. #if UNITY_EDITOR
  61.             Finish();
  62. #endif
  63.         }
  64.  
  65.  
  66. #if UNITY_IOS
  67.         void reauthorizationSucceededEvent()
  68.         {
  69.             Debug.Log ("reauthorizationSucceededEvent");
  70.             var globalVariables = FsmVariables.GlobalVariables;
  71.             var hasWrite = globalVariables.GetFsmBool("hasWritePermissions");
  72.             hasWrite.Value = true;
  73.            
  74.             Fsm.Event(successEvent);
  75.             Finish();
  76.         }
  77.        
  78.        
  79.         void reauthorizationFailedEvent( P31Error error )
  80.         {
  81.             Debug.Log ("loginFailedEvent write");
  82.             var globalVariables = FsmVariables.GlobalVariables;
  83.             var hasWrite = globalVariables.GetFsmBool("hasWritePermissions");
  84.             hasWrite.Value = false;
  85.             var fsmError = globalVariables.GetFsmString("SocialError");
  86.             fsmError.Value = error.ToString();
  87.            
  88.             Fsm.Event(errorEvent);
  89.             Finish();
  90.         }
  91. #endif
  92.  
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement