Created
March 19, 2013 20:50
-
-
Save jmcguirk/5200019 to your computer and use it in GitHub Desktop.
Wrappers for Prime 31 IOS and Android FB Integration Uses 3.18 version of these plugins http://prime31.com/docs#androidSocial
http://prime31.com/docs#iosSocial
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| #if UNITY_ANDROID | |
| public class AndroidFacebookManager : IFacebookManager | |
| { | |
| public event LoginSuccessHandler OnLoginSuccess; | |
| public event LoginFailedHandler OnLoginFailed; | |
| public bool supported { | |
| get { | |
| return true; | |
| } | |
| } | |
| public void init() { | |
| FacebookManager.sessionOpenedEvent += onInternalSessionOpened; | |
| FacebookManager.loginFailedEvent += onLoginFailedEvent; | |
| FacebookAndroid.init(); | |
| } | |
| protected void onLoginFailedEvent(string obj) { | |
| if(OnLoginFailed != null){ | |
| OnLoginFailed(obj); | |
| } | |
| } | |
| protected void onInternalSessionOpened() { | |
| if(OnLoginSuccess != null){ | |
| OnLoginSuccess(); | |
| } | |
| } | |
| public string getAppLaunchUrl() { // JSM - not supported via android bindings | |
| return null; | |
| } | |
| public bool isSessionValid() { | |
| return FacebookAndroid.isSessionValid(); | |
| } | |
| public string getAccessToken() { | |
| return FacebookAndroid.getAccessToken(); | |
| } | |
| public void graphRequest(string path, GraphResponseHandler complete){ | |
| Facebook.instance.graphRequest(path, delegate(string respath, object result){complete(respath, result);}); | |
| } | |
| public void login() { | |
| GameLog.debug("Facebook login"); | |
| FacebookAndroid.login(); | |
| } | |
| public void loginWithReadPermissions(string[] permissions) { | |
| GameLog.debug("Facebook login"); | |
| FacebookAndroid.loginWithReadPermissions(permissions); | |
| } | |
| } | |
| #else | |
| public class AndroidFacebookManager : MockFacebookManager | |
| { | |
| } | |
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| public delegate void LoginSuccessHandler(); | |
| public delegate void LoginFailedHandler(string result); | |
| public delegate void GraphResponseHandler(string path, object result); | |
| /// <summary> | |
| /// Interface for a platform specific facebook manager | |
| /// </summary> | |
| public interface IFacebookManager | |
| { | |
| /// <summary> | |
| /// Occurs when player logs into facebook | |
| /// </summary> | |
| event LoginSuccessHandler OnLoginSuccess; | |
| /// <summary> | |
| /// Occurs when a login fails, likely due to cancelling | |
| /// </summary> | |
| event LoginFailedHandler OnLoginFailed; | |
| /// <summary> | |
| /// Gets a value indicating whether this <see cref="IFacebookManager"/> is supported on the current platform | |
| /// </summary> | |
| /// <value> | |
| /// <c>true</c> if supported; otherwise, <c>false</c>. | |
| /// </value> | |
| bool supported { get; } | |
| /// <summary> | |
| /// Inits the underlying implementation | |
| /// </summary> | |
| void init(); | |
| /// <summary> | |
| /// Fetches the url which caused our runtime to launch, only available on certain platforms | |
| /// </summary> | |
| /// <returns></returns> | |
| string getAppLaunchUrl(); | |
| /// <summary> | |
| /// Determines whether the current session is valid | |
| /// </summary> | |
| /// <returns> | |
| /// <c>true</c> if [is session valid]; otherwise, <c>false</c>. | |
| /// </returns> | |
| bool isSessionValid(); | |
| /// <summary> | |
| /// Gets the current access token. | |
| /// </summary> | |
| /// <returns></returns> | |
| string getAccessToken(); | |
| /// <summary> | |
| /// Issues a graph request | |
| /// </summary> | |
| /// <param name="path">The path to request.</param> | |
| /// <param name="complete">Completion callback.</param> | |
| void graphRequest(string path, GraphResponseHandler complete); | |
| /// <summary> | |
| /// Attempts to log the current player in | |
| /// </summary> | |
| void login(); | |
| /// <summary> | |
| /// Attempts to log the current player in the given permissions requested | |
| /// </summary> | |
| /// <param name="permissions">The permissions.</param> | |
| void loginWithReadPermissions(string[] permissions); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| if (Application.platform == RuntimePlatform.IPhonePlayer) { | |
| ServiceLocator.facebook = new IOSFacebookManager(); | |
| } | |
| else if (Application.platform == RuntimePlatform.Android) { | |
| ServiceLocator.facebook = new AndroidFacebookManager(); | |
| } else{ | |
| ServiceLocator.facebook = new MockFacebookManager(); | |
| } | |
| ServiceLocator.facebook.init(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| #if UNITY_IPHONE | |
| public class IOSFacebookManager : IFacebookManager | |
| { | |
| public event LoginSuccessHandler OnLoginSuccess; | |
| public event LoginFailedHandler OnLoginFailed; | |
| public bool supported { | |
| get { | |
| return true; | |
| } | |
| } | |
| public void init() { | |
| FacebookManager.sessionOpenedEvent += onInternalSessionOpened; | |
| FacebookManager.loginFailedEvent += onLoginFailedEvent; | |
| FacebookBinding.init(); | |
| } | |
| protected void onLoginFailedEvent(string obj) { | |
| if(OnLoginFailed != null){ | |
| OnLoginFailed(obj); | |
| } | |
| } | |
| protected void onInternalSessionOpened() { | |
| if(OnLoginSuccess != null){ | |
| OnLoginSuccess(); | |
| } | |
| } | |
| public string getAppLaunchUrl() { | |
| return FacebookBinding.getAppLaunchUrl(); | |
| } | |
| public bool isSessionValid() { | |
| return FacebookBinding.isSessionValid(); | |
| } | |
| public string getAccessToken() { | |
| return FacebookBinding.getAccessToken(); | |
| } | |
| public void graphRequest(string path, GraphResponseHandler complete){ | |
| Facebook.instance.graphRequest(path, delegate(string respath, object result){complete(respath, result);}); | |
| } | |
| public void login() { | |
| GameLog.debug("Facebook login"); | |
| FacebookBinding.login(); | |
| } | |
| public void loginWithReadPermissions(string[] permissions) { | |
| GameLog.debug("Facebook login"); | |
| FacebookBinding.loginWithReadPermissions(permissions); | |
| } | |
| } | |
| #else | |
| public class IOSFacebookManager : MockFacebookManager | |
| { | |
| } | |
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using Prime31; | |
| public class MockFacebookManager : IFacebookManager | |
| { | |
| public event LoginSuccessHandler OnLoginSuccess; | |
| public event LoginFailedHandler OnLoginFailed; | |
| public bool supported { | |
| get { | |
| return false; | |
| } | |
| } | |
| public void graphRequest(string path, GraphResponseHandler complete) { | |
| GameLog.debug("Graph request for " + path); | |
| } | |
| public void init() { | |
| GameLog.debug("Facebook init"); | |
| } | |
| public string getAppLaunchUrl() { | |
| return null; | |
| } | |
| public bool isSessionValid() { | |
| return true; | |
| } | |
| public string getAccessToken() { | |
| return null; | |
| } | |
| public void login() { | |
| GameLog.debug("Facebook login"); | |
| } | |
| public void loginWithReadPermissions(string[] permissions) { | |
| GameLog.debug("Facebook login with read perms"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Use this for initialization | |
| public override void OnStart() { | |
| loginButton.GetComponent<tk2dButton>().ButtonPressedEvent += attemptLogin; | |
| ServiceLocator.facebook.OnLoginFailed += facebook_OnLoginFailed; | |
| ServiceLocator.facebook.OnLoginSuccess += facebook_OnLoginSuccess; | |
| } | |
| protected void attemptLogin(tk2dButton source) { | |
| ServiceLocator.facebook.login(); | |
| } | |
| void facebook_OnLoginSuccess() { | |
| GameLog.fatal("FACEBOOK LOGIN SUCCEEEDED!"); | |
| ServiceLocator.facebook.graphRequest("/me", onMeCompleteRequest); | |
| } | |
| protected void onMeCompleteRequest(string path, object result) { | |
| Hashtable hashResult = result as Hashtable; | |
| if (hashResult != null) { | |
| SocialNetworkUser currentUser = SocialNetworkUser.newFromIOSHashTable(hashResult); | |
| currentUser.PlatformID = GameConstants.FACEBOOK_PLATFORM; | |
| GameLog.fatal("Current user " + currentUser.ToString()); | |
| } | |
| } | |
| void facebook_OnLoginFailed(string result) { | |
| GameLog.fatal("FACEBOOK LOGIN FAILED " + result); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment