Sign In Register

How can we help you today?

Start a new topic
Answered

Newbie question : Learning to use the platform

Just started getting to grips with the platform. 


    My first port of call will most likely be Unity /Facebook auth and then leader boards. It's been flagged in another post that the facebook sdk used is of an older version in the tutorials. 

    Ill use that version  to learn the ins and outs of api  auth access tokens and loginRequests. However, how would one implement with the new sdk. As , specifically,  FBResult has been replaced. I'm pretty confused. Any help etc. would be awesome as I'm more used to front end development concepts so this would be a decent time sync and project deadlines wait for no man or woman .  A point in the right direction would be great :) 

Thanks, Patrick.      


Best Answer
Hi,
Here is how I do Facebook / GameSparks authentication with the latest SDK
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");
			}
		}
	}
}

 It's different from the tutorial but it might give you an idea how it's done.


Answer
Hi,
Here is how I do Facebook / GameSparks authentication with the latest SDK
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");
			}
		}
	}
}

 It's different from the tutorial but it might give you an idea how it's done.

Thank you so much !! ill take it apart and see what I can do. Always good to look at someone elses implementation ! 

Login to post a comment