Last active
August 29, 2015 14:07
-
-
Save ovidiubuligan/3d203e435125d8b1e3a5 to your computer and use it in GitHub Desktop.
Gamesparks sdk +unity sdk user management
This file contains 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 UnityEngine; | |
using System.Collections.Generic; | |
using GameSparks.Api; | |
using GameSparks.Api.Messages; | |
using GameSparks.Api.Requests; | |
using GameSparks.Api.Responses; | |
using GameSparks.Core; | |
using Facebook; | |
namespace MyApp | |
{ | |
public class UserManager : MonoBehaviour | |
{ | |
public UserManager () | |
{ | |
} | |
// Tries to silently log in user if already logged in before in another gaming session | |
public void Awake(){ | |
if (!FB.IsInitialized) { | |
FB.Init (()=>{}); | |
} else { | |
if (FB.IsLoggedIn) LoginFacebook(); | |
} | |
} | |
// initiates the action to login to facebook and forwards login to gamesparks. | |
// it is async and uses callback style instead of return value | |
public void LoginFacebook(Action<UserDetails> success = null, Action<FBResult> error=null){ | |
if (FB.IsLoggedIn) { | |
FacebookUserLoggedIn(null,success,error); | |
}else{ | |
FB.Login("email,user_friends", (result)=>{ | |
FacebookUserLoggedIn(result,success,error); | |
}); | |
} | |
} | |
private void FacebookUserLoggedIn(FBResult result=null,Action<UserDetails> success=null, Action<FBResult> error=null){ | |
if(result !=null && result.Error != null){ | |
Debug.Log("Error logging in to facebook"); | |
if(error!=null)error(result); | |
}else{ | |
new FacebookConnectRequest().SetAccessToken(FB.AccessToken).Send((response) => { | |
Debug.Log("OK! fb connected"); | |
var userDetails = new UserDetails(); | |
userDetails.id = response.UserId; | |
if(success!=null) success(userDetails); | |
}); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment