Created
September 14, 2019 19:22
-
-
Save Raziel619/a089bab2d6bf5a625bce3206e9998a81 to your computer and use it in GitHub Desktop.
Gist for 'Playfab First Impression' Medium post
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.Collections; | |
using System.Collections.Generic; | |
using PlayFab; | |
using PlayFab.ClientModels; | |
using UnityEngine; | |
using Facebook.Unity; | |
using LoginResult = PlayFab.ClientModels.LoginResult; | |
using System; | |
public class PlayFabFuncs : MonoBehaviour { | |
public bool IsRunning | |
{ | |
get | |
{ | |
return isRunning; | |
} | |
} | |
private static bool isLoaded = false; | |
private bool isRunning; | |
// Use this for initialization | |
void Awake () { | |
if (isLoaded) | |
{ | |
Destroy(this.gameObject); | |
} | |
isLoaded = true; | |
DontDestroyOnLoad(gameObject); | |
FB.Init(OnFacebookInitialized); | |
} | |
void Start() | |
{ | |
if (FB.IsLoggedIn) | |
{ | |
GetTop20Leaderboard(); | |
} | |
} | |
private void OnFacebookInitialized() | |
{ | |
if (FB.IsLoggedIn) | |
{ | |
PlayFabClientAPI.LoginWithFacebook(new PlayFab.ClientModels.LoginWithFacebookRequest | |
{ | |
CreateAccount = true, | |
AccessToken = AccessToken.CurrentAccessToken.TokenString | |
}, OnPlayfabFBAuthComplete, OnPlayfabFBAuthFailed); | |
} | |
} | |
public void LoginWithFacebook() | |
{ | |
isRunning = true; | |
FB.LogInWithReadPermissions(null, OnFacebookLoggedIn); | |
} | |
private void OnFacebookLoggedIn(ILoginResult result) | |
{ | |
if(result == null || string.IsNullOrEmpty(result.Error)){ | |
PlayFabClientAPI.LoginWithFacebook(new PlayFab.ClientModels.LoginWithFacebookRequest | |
{ | |
CreateAccount = true, AccessToken = AccessToken.CurrentAccessToken.TokenString | |
}, OnPlayfabFBAuthComplete, OnPlayfabFBAuthFailed); | |
} | |
else{ | |
DebugLog("Facebook Log In Failed: " + result.Error); | |
isRunning = false; | |
} | |
} | |
private void OnPlayfabFBAuthComplete(LoginResult result) | |
{ | |
isRunning = false; | |
//Perform any other actions here | |
} | |
private void OnPlayfabFBAuthFailed(PlayFabError error) | |
{ | |
isRunning = false; | |
//Debug error messages here | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment