Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using UnityEngine;
- using Facebook.Unity;
- using GameSparks.Api.Requests;
- public class AuthManagerFB {
- static bool loggedIn;
- /// <summary>
- /// Pass all actions that require authenticated user as a callback.
- /// </summary>
- /// <param name="onSuccess">Called if/once user has authenticated</param>
- /// <param name="onError">Called on failure</param>
- /// <param name="showDialog">Whether or not to show the Facebook-login dialog if user has not authenticated</param>
- /// <example>
- /// <code>
- /// ifAuthenticated(() => { /* User authenticated, do something */ },
- /// onError: msg => Debug.Log("Error: " + msg),
- /// showDialog: true);
- /// </code>
- /// </example>
- public void ifAuthenticated(Action onSuccess, Action<string> onError = null, bool showDialog = false) {
- if (loggedIn) {
- onSuccess();
- return;
- }
- if (onError == null) onError = s => {};
- Action doGameSparksLogin = () => {
- new FacebookConnectRequest().SetAccessToken(AccessToken.CurrentAccessToken.TokenString).Send(response => {
- if (response.HasErrors) {
- onError("GameSpark login failed");
- } else {
- Debug.Log("Logged in as " + response.UserId);
- loggedIn = true;
- onSuccess();
- }
- });
- };
- InitDelegate doFacebookLogin = () => {
- if (FB.IsLoggedIn)
- doGameSparksLogin();
- else if (showDialog) {
- FB.LogInWithReadPermissions(new string[] { "public_profile", "email", "user_friends" }, loginResult => {
- if (FB.IsLoggedIn)
- doGameSparksLogin();
- else {
- Debug.Log("Facebook login error: " + loginResult.Error);
- onError("Facebook login failed");
- }
- });
- } else
- onError("Not logged in");
- };
- if (FB.IsInitialized)
- doFacebookLogin();
- else {
- try {
- FB.Init(doFacebookLogin);
- } catch (Exception e) {
- Debug.LogWarning("Facebook ERROR: " + e.Message);
- onError("Facebook login failed");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement